Class: Rhino::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rhino/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rhino/configuration.rb', line 8

def initialize
  @models = {}
  @route_groups = {}
  @multi_tenant = {
    organization_identifier_column: "id"
  }
  @invitations = {
    expires_days: 7,
    allowed_roles: nil
  }
  @nested = {
    path: "nested",
    max_operations: 50,
    allowed_models: nil
  }
  @test_framework = "rspec"
  @client_path = nil
  @mobile_path = nil
end

Instance Attribute Details

#client_pathObject

Returns the value of attribute client_path.



5
6
7
# File 'lib/rhino/configuration.rb', line 5

def client_path
  @client_path
end

#invitationsObject

Returns the value of attribute invitations.



5
6
7
# File 'lib/rhino/configuration.rb', line 5

def invitations
  @invitations
end

#mobile_pathObject

Returns the value of attribute mobile_path.



5
6
7
# File 'lib/rhino/configuration.rb', line 5

def mobile_path
  @mobile_path
end

#modelsObject

Returns the value of attribute models.



5
6
7
# File 'lib/rhino/configuration.rb', line 5

def models
  @models
end

#multi_tenantObject

Returns the value of attribute multi_tenant.



5
6
7
# File 'lib/rhino/configuration.rb', line 5

def multi_tenant
  @multi_tenant
end

#nestedObject

Returns the value of attribute nested.



5
6
7
# File 'lib/rhino/configuration.rb', line 5

def nested
  @nested
end

#route_groupsObject

Returns the value of attribute route_groups.



5
6
7
# File 'lib/rhino/configuration.rb', line 5

def route_groups
  @route_groups
end

#test_frameworkObject

Returns the value of attribute test_framework.



5
6
7
# File 'lib/rhino/configuration.rb', line 5

def test_framework
  @test_framework
end

Instance Method Details

#has_public_group?Boolean

Whether a ‘public’ route group is configured

Returns:

  • (Boolean)


72
73
74
# File 'lib/rhino/configuration.rb', line 72

def has_public_group?
  @route_groups.key?(:public)
end

#has_tenant_group?Boolean

Whether a ‘tenant’ route group is configured

Returns:

  • (Boolean)


67
68
69
# File 'lib/rhino/configuration.rb', line 67

def has_tenant_group?
  @route_groups.key?(:tenant)
end

#model(slug, klass_name) ⇒ Object

Register a model with its slug Usage: config.model :posts, ‘Post’



30
31
32
# File 'lib/rhino/configuration.rb', line 30

def model(slug, klass_name)
  @models[slug.to_sym] = klass_name.to_s
end

#model_in_group?(slug, group_name) ⇒ Boolean

Check if a specific slug belongs to a specific group

Returns:

  • (Boolean)


97
98
99
# File 'lib/rhino/configuration.rb', line 97

def model_in_group?(slug, group_name)
  models_for_group(group_name).include?(slug.to_sym)
end

#models_for_group(group_name) ⇒ Object

Resolve the model slugs for a given route group



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rhino/configuration.rb', line 77

def models_for_group(group_name)
  group = @route_groups[group_name.to_sym]
  return [] unless group

  group_models = group[:models]
  if group_models == :all || group_models == "*"
    @models.keys
  else
    Array(group_models).map(&:to_sym) & @models.keys
  end
end

#public_model?(slug) ⇒ Boolean

Check if a model belongs to the ‘public’ route group

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/rhino/configuration.rb', line 90

def public_model?(slug)
  return false unless has_public_group?

  models_for_group(:public).include?(slug.to_sym)
end

#resolve_model(slug) ⇒ Object

Resolve a model class from its slug



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rhino/configuration.rb', line 45

def resolve_model(slug)
  klass_name = @models[slug.to_sym]
  raise ActiveRecord::RecordNotFound, "The #{slug} model does not exist" unless klass_name

  klass = klass_name.constantize
  raise ActiveRecord::RecordNotFound, "The #{slug} model does not exist" unless klass

  klass
rescue NameError
  raise ActiveRecord::RecordNotFound, "The #{slug} model does not exist"
end

#route_group(name, prefix: "", middleware: [], models: :all) ⇒ Object

Register a route group with its configuration Usage: config.route_group :tenant, prefix: ‘:organization’, middleware: [Rhino::Middleware::ResolveOrganizationFromRoute], models: :all



36
37
38
39
40
41
42
# File 'lib/rhino/configuration.rb', line 36

def route_group(name, prefix: "", middleware: [], models: :all)
  @route_groups[name.to_sym] = {
    prefix: prefix.to_s,
    middleware: Array(middleware),
    models: models
  }
end

#slug_for(model_class) ⇒ Object

Find the slug for a given model class



58
59
60
61
62
63
64
# File 'lib/rhino/configuration.rb', line 58

def slug_for(model_class)
  class_name = model_class.is_a?(Class) ? model_class.name : model_class.class.name
  @models.each do |slug, klass_name|
    return slug if klass_name == class_name
  end
  nil
end