Module: Configurable::ClassMethods

Includes:
ConfigClasses, ConfigTypes
Defined in:
lib/configurable/class_methods.rb

Overview

ClassMethods extends classes that include Configurable and provides methods for declaring configurations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config_registryObject (readonly)

A hash of (key, Config) pairs tracking configs defined on self. See the configs method for all configs declared across all ancestors.



25
26
27
# File 'lib/configurable/class_methods.rb', line 25

def config_registry
  @config_registry
end

#config_type_registryObject (readonly)

A hash of (key, ConfigType) pairs tracking config_types defined on self. See the config_types method for all config_types declared across all ancestors.



30
31
32
# File 'lib/configurable/class_methods.rb', line 30

def config_type_registry
  @config_type_registry
end

Class Method Details

.initialize(base) ⇒ Object

:nodoc:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/configurable/class_methods.rb', line 32

def self.initialize(base) # :nodoc:
  base.reset_configs
  unless base.instance_variable_defined?(:@config_registry)
    base.instance_variable_set(:@config_registry, {})
  end
  
  base.reset_config_types
  unless base.instance_variable_defined?(:@config_type_registry)
    base.instance_variable_set(:@config_type_registry, {})
  end
  
  unless base.instance_variable_defined?(:@config_type_context)
    base.instance_variable_set(:@config_type_context, base)
  end
end

Instance Method Details

#config_typesObject

A hash of (key, ConfigType) pairs representing all config_types defined on this class or inherited from ancestors. The config_types hash is memoized for performance. Call reset_config_types if config_types needs to be recalculated for any reason.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/configurable/class_methods.rb', line 83

def config_types
  @config_types ||= begin
    config_types = {}
    
    registries = []
    each_registry do |ancestor, registry|
      registries.unshift(registry)
    end
    
    registries.each do |registry|
      registry.each_pair do |key, value|
        if value.nil?
          config_types.delete(key)
        else
          config_types[key] = value
        end
      end
    end
  
    config_types
  end
end

#configsObject

A hash of (key, Config) pairs representing all configs defined on this class or inherited from ancestors. The configs hash is memoized for performance. Call reset_configs if configs needs to be recalculated for any reason.

Configs is extended with the Conversions module.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/configurable/class_methods.rb', line 54

def configs
  @configs ||= begin
    configs = {}
  
    ancestors.reverse.each do |ancestor|
      next unless ancestor.kind_of?(ClassMethods)
      ancestor.config_registry.each_pair do |key, value|
        if value.nil?
          configs.delete(key)
        else
          configs[key] = value
        end
      end
    end
    
    configs.extend Conversions
    configs
  end
end

#reset_config_typesObject

Resets config_types such that they will be recalculated.



107
108
109
# File 'lib/configurable/class_methods.rb', line 107

def reset_config_types
  @config_types = nil
end

#reset_configsObject

Resets configs such that they will be recalculated.



75
76
77
# File 'lib/configurable/class_methods.rb', line 75

def reset_configs
  @configs = nil
end