Class: Declarative::Defaults

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative/defaults.rb

Defined Under Namespace

Classes: Merge

Instance Method Summary collapse

Constructor Details

#initializeDefaults

Returns a new instance of Defaults.



3
4
5
6
# File 'lib/declarative/defaults.rb', line 3

def initialize
  @static_options  = {}
  @dynamic_options = ->(*) { Hash.new }
end

Instance Method Details

#call(name, given_options) ⇒ Object

Evaluate defaults and merge given_options into them.



18
19
20
21
22
23
24
# File 'lib/declarative/defaults.rb', line 18

def call(name, given_options)
  # TODO: allow to receive rest of options/block in dynamic block. or, rather, test it as it was already implemented.
  evaluated_options = @dynamic_options.(name, given_options)

  options = Merge.(@static_options, evaluated_options)
  options = options.merge(given_options)
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: []`.



10
11
12
13
14
15
# File 'lib/declarative/defaults.rb', line 10

def merge!(hash={}, &block)
  @static_options = Merge.(@static_options, hash)

  @dynamic_options = block if block_given?
  self
end