Module: FlexibleEnum::Mixin::ClassMethods

Defined in:
lib/flexible_enum/mixin.rb

Instance Method Summary collapse

Instance Method Details

#flexible_enum(attribute_name, attribute_options = {}, &config) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flexible_enum/mixin.rb', line 13

def flexible_enum(attribute_name, attribute_options = {}, &config)
  # Methods are defined on the feature module which in turn is mixed in to the target class
  feature_module = Module.new do |m|
    extend ActiveSupport::Concern
    const_set :ClassMethods, Module.new
    def m.inspect
      "FlexibleEnum(#{self})"
    end
  end

  # The module that will hold references to value constants
  module_for_elements = attribute_options[:namespace] ? self.const_set(attribute_options[:namespace], Module.new) : feature_module

  # Read configuration
  elements = Configuration.load(&config).elements

  # Configure the target object for the given attribute
  configurators = [ConstantConfigurator,
                   IdentityConfigurator,
                   QuestionMethodConfigurator,
                   SetterMethodConfigurator,
                   ScopeConfigurator,
                   PotentialValuesConfigurator]
  configurators.each do |configurator|
    configurator.new(feature_module, attribute_name, module_for_elements, elements).apply
  end

  # Add functionality to target inheritance chain
  send(:include, feature_module)

  flexible_enums[attribute_name] = public_send("#{attribute_name.to_s.pluralize}_by_sym")
end

#flexible_enumsObject



9
10
11
# File 'lib/flexible_enum/mixin.rb', line 9

def flexible_enums
  @flexible_enums ||= {}
end