Class: Rhino::Configuration
- Inherits:
-
Object
- Object
- Rhino::Configuration
- Defined in:
- lib/rhino/configuration.rb
Instance Attribute Summary collapse
-
#client_path ⇒ Object
Returns the value of attribute client_path.
-
#invitations ⇒ Object
Returns the value of attribute invitations.
-
#mobile_path ⇒ Object
Returns the value of attribute mobile_path.
-
#models ⇒ Object
Returns the value of attribute models.
-
#multi_tenant ⇒ Object
Returns the value of attribute multi_tenant.
-
#nested ⇒ Object
Returns the value of attribute nested.
-
#route_groups ⇒ Object
Returns the value of attribute route_groups.
-
#test_framework ⇒ Object
Returns the value of attribute test_framework.
Instance Method Summary collapse
-
#has_public_group? ⇒ Boolean
Whether a ‘public’ route group is configured.
-
#has_tenant_group? ⇒ Boolean
Whether a ‘tenant’ route group is configured.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#model(slug, klass_name) ⇒ Object
Register a model with its slug Usage: config.model :posts, ‘Post’.
-
#model_in_group?(slug, group_name) ⇒ Boolean
Check if a specific slug belongs to a specific group.
-
#models_for_group(group_name) ⇒ Object
Resolve the model slugs for a given route group.
-
#public_model?(slug) ⇒ Boolean
Check if a model belongs to the ‘public’ route group.
-
#resolve_model(slug) ⇒ Object
Resolve a model class from its slug.
-
#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.
-
#slug_for(model_class) ⇒ Object
Find the slug for a given model class.
Constructor Details
#initialize ⇒ Configuration
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_path ⇒ Object
Returns the value of attribute client_path.
5 6 7 |
# File 'lib/rhino/configuration.rb', line 5 def client_path @client_path end |
#invitations ⇒ Object
Returns the value of attribute invitations.
5 6 7 |
# File 'lib/rhino/configuration.rb', line 5 def invitations @invitations end |
#mobile_path ⇒ Object
Returns the value of attribute mobile_path.
5 6 7 |
# File 'lib/rhino/configuration.rb', line 5 def mobile_path @mobile_path end |
#models ⇒ Object
Returns the value of attribute models.
5 6 7 |
# File 'lib/rhino/configuration.rb', line 5 def models @models end |
#multi_tenant ⇒ Object
Returns the value of attribute multi_tenant.
5 6 7 |
# File 'lib/rhino/configuration.rb', line 5 def multi_tenant @multi_tenant end |
#nested ⇒ Object
Returns the value of attribute nested.
5 6 7 |
# File 'lib/rhino/configuration.rb', line 5 def nested @nested end |
#route_groups ⇒ Object
Returns the value of attribute route_groups.
5 6 7 |
# File 'lib/rhino/configuration.rb', line 5 def route_groups @route_groups end |
#test_framework ⇒ Object
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
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
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
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
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 |