Module: Micro::Attributes::Features::Options
- Defined in:
- lib/micro/attributes/features.rb
Constant Summary collapse
- KEYS =
[ DIFF = 'Diff'.freeze, INIT = 'Initialize'.freeze, ACCEPT = 'Accept'.freeze, INIT_STRICT = 'InitializeStrict'.freeze, ACCEPT_STRICT = 'AcceptStrict'.freeze, KEYS_AS_SYMBOL = 'KeysAsSymbol'.freeze, AM_VALIDATIONS = 'ActiveModelValidations'.freeze ].sort.freeze
- KEYS_TO_MODULES =
begin combinations = (1..KEYS.size).map { |n| KEYS.combination(n).to_a }.flatten(1).sort_by { |i| "#{i.size}#{i.join}" } combinations.delete_if { |combination| combination.include?(INIT_STRICT) && !combination.include?(INIT) } combinations.delete_if { |combination| combination.include?(ACCEPT_STRICT) && !combination.include?(ACCEPT) } combinations.each_with_object({}) do |combination, features| included = [ 'def self.included(base)', ' base.send(:include, ::Micro::Attributes)', combination.map { |key| " base.send(:include, ::#{KEYS_TO_FEATURES[key].name})" }, 'end' ].flatten.join("\n") key = BuildKey.call(combination) With.const_set(key, Module.new.tap { |mod| mod.instance_eval(included) }) features[key] = With.const_get(key, false) end.freeze end
- ACTIVEMODEL_VALIDATION =
'activemodel_validation'.freeze
Class Method Summary collapse
- .fetch_key(arg) ⇒ Object
- .fetch_keys(args) {|keys| ... } ⇒ Object
- .fetch_module_by_keys(combination) ⇒ Object
- .remove_base_if_has_strict(keys) ⇒ Object
- .without_keys(keys_to_exclude) ⇒ Object
Class Method Details
.fetch_key(arg) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/micro/attributes/features.rb', line 89 def self.fetch_key(arg) if arg.is_a?(Hash) return ACCEPT_STRICT if arg[:accept] == :strict INIT_STRICT if arg[:initialize] == :strict else str = String(arg) name = str == ACTIVEMODEL_VALIDATION ? Name::ACTIVEMODEL_VALIDATIONS : str KEYS_TO_MODULES.key?(name) ? name : NAMES_TO_KEYS[name] end end |
.fetch_keys(args) {|keys| ... } ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/micro/attributes/features.rb', line 108 def self.fetch_keys(args) keys = Array(args).dup.map { |name| fetch_key(name) } raise ArgumentError, INVALID_NAME if keys.empty? || !(keys - KEYS).empty? yield(keys) end |
.fetch_module_by_keys(combination) ⇒ Object
128 129 130 131 132 |
# File 'lib/micro/attributes/features.rb', line 128 def self.fetch_module_by_keys(combination) key = BuildKey.call(combination) KEYS_TO_MODULES.fetch(key) end |
.remove_base_if_has_strict(keys) ⇒ Object
116 117 118 119 |
# File 'lib/micro/attributes/features.rb', line 116 def self.remove_base_if_has_strict(keys) keys.delete_if { |key| key == INIT } if keys.include?(INIT_STRICT) keys.delete_if { |key| key == ACCEPT } if keys.include?(ACCEPT_STRICT) end |
.without_keys(keys_to_exclude) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/micro/attributes/features.rb', line 121 def self.without_keys(keys_to_exclude) keys = (KEYS - keys_to_exclude) keys.delete_if { |key| key == INIT || key == INIT_STRICT } if keys_to_exclude.include?(INIT) keys.delete_if { |key| key == ACCEPT || key == ACCEPT_STRICT } if keys_to_exclude.include?(ACCEPT) keys end |