Class: AgentCode::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



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

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"
end

Instance Attribute Details

#invitationsObject

Returns the value of attribute invitations.



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

def invitations
  @invitations
end

#modelsObject

Returns the value of attribute models.



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

def models
  @models
end

#multi_tenantObject

Returns the value of attribute multi_tenant.



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

def multi_tenant
  @multi_tenant
end

#nestedObject

Returns the value of attribute nested.



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

def nested
  @nested
end

#route_groupsObject

Returns the value of attribute route_groups.



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

def route_groups
  @route_groups
end

#test_frameworkObject

Returns the value of attribute test_framework.



5
6
7
# File 'lib/agentcode/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)


69
70
71
# File 'lib/agentcode/configuration.rb', line 69

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

#has_tenant_group?Boolean

Whether a 'tenant' route group is configured

Returns:

  • (Boolean)


64
65
66
# File 'lib/agentcode/configuration.rb', line 64

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'



27
28
29
# File 'lib/agentcode/configuration.rb', line 27

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)


94
95
96
# File 'lib/agentcode/configuration.rb', line 94

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



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/agentcode/configuration.rb', line 74

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)


87
88
89
90
91
# File 'lib/agentcode/configuration.rb', line 87

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



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/agentcode/configuration.rb', line 42

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: [AgentCode::Middleware::ResolveOrganizationFromRoute], models: :all



33
34
35
36
37
38
39
# File 'lib/agentcode/configuration.rb', line 33

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



55
56
57
58
59
60
61
# File 'lib/agentcode/configuration.rb', line 55

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