Class: Declarative::Defaults
- Inherits:
-
Object
- Object
- Declarative::Defaults
- Defined in:
- lib/declarative/defaults.rb
Overview
Class Method Summary collapse
-
.wrap_arrays(variables) ⇒ Object
Wrap arrays in ‘variables` with Variables::Append so they get appended to existing same-named arrays.
Instance Method Summary collapse
-
#call(name, given_options) ⇒ Object
Evaluate defaults and merge given_options into them.
- #handle_array_and_deprecate(variables) ⇒ Object
-
#initialize ⇒ Defaults
constructor
A new instance of Defaults.
-
#merge!(hash = {}, &block) ⇒ Object
Set default values.
Constructor Details
#initialize ⇒ Defaults
Returns a new instance of Defaults.
5 6 7 8 |
# File 'lib/declarative/defaults.rb', line 5 def initialize @static_options = {} @dynamic_options = ->(*) { {} } end |
Class Method Details
.wrap_arrays(variables) ⇒ Object
Wrap arrays in ‘variables` with Variables::Append so they get appended to existing same-named arrays.
38 39 40 41 42 43 |
# File 'lib/declarative/defaults.rb', line 38 def self.wrap_arrays(variables) Hash[ variables. find_all { |k,v| v.instance_of?(Array) }. collect { |k,v| [k, Variables::Append(v)] } ] end |
Instance Method Details
#call(name, given_options) ⇒ Object
Evaluate defaults and merge given_options into them.
20 21 22 23 24 25 26 |
# File 'lib/declarative/defaults.rb', line 20 def call(name, ) # TODO: allow to receive rest of options/block in dynamic block. or, rather, test it as it was already implemented. = @dynamic_options.(name, ) = Variables.merge( @static_options, handle_array_and_deprecate() ) Variables.merge( , handle_array_and_deprecate() ) # FIXME: given_options is not tested! end |
#handle_array_and_deprecate(variables) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/declarative/defaults.rb', line 28 def handle_array_and_deprecate(variables) wrapped = Defaults.wrap_arrays(variables) warn "[Declarative] Defaults#merge! and #call still accept arrays and automatically prepend those. This is now deprecated, you should replace `ary` with `Declarative::Variables::Append(ary)`." if wrapped.any? variables.merge(wrapped) end |
#merge!(hash = {}, &block) ⇒ Object
Set default values. Usually called in Schema::defaults. This can be called multiple times and will “deep-merge” arrays, e.g. ‘_features: []`.
12 13 14 15 16 17 |
# File 'lib/declarative/defaults.rb', line 12 def merge!(hash={}, &block) @static_options = Variables.merge( @static_options, handle_array_and_deprecate(hash) ) @dynamic_options = block if block_given? self end |