Class: HashMap::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_map/dsl.rb

Defined Under Namespace

Classes: InvalidOptionsForProperty, NoMapperForCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



29
30
31
# File 'lib/hash_map/dsl.rb', line 29

def initialize
  @attributes = []
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



27
28
29
# File 'lib/hash_map/dsl.rb', line 27

def attributes
  @attributes
end

Instance Method Details

#_set_attributes(attrs) ⇒ Object



48
49
50
# File 'lib/hash_map/dsl.rb', line 48

def _set_attributes(attrs)
  @attributes = attrs
end

#after_each(*middlewares) ⇒ Object



33
34
35
36
# File 'lib/hash_map/dsl.rb', line 33

def after_each(*middlewares)
  @after_each ||= []
  @after_each += middlewares
end

#collection(key, opts = {}, &block) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/hash_map/dsl.rb', line 61

def collection(key, opts = {}, &block)
  unless opts[:mapper]
    fail NoMapperForCollection, "[HashMap Error] Called 'collection' without the ':mapper' option"
  end
  opts.merge!(is_collection: true)
  property(key, opts, &block)
end

#from_child(*args, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hash_map/dsl.rb', line 80

def from_child(*args, &block)
  options = args.last.is_a?(::Hash) ? args.pop : {}
  key = args
  flat = _nested(key, options, &block)
  keys = Fusu::Array.wrap(key)
  flat.each do |attr|
    keys.reverse.each do |k|
      attr[:from].unshift(k)
      attr[:from_child] ? attr[:from_child].unshift(k) : attr[:from_child] = [k]
    end
  end
  @attributes += flat
end

#from_children(key, opts = {}, &block) ⇒ Object



75
76
77
78
# File 'lib/hash_map/dsl.rb', line 75

def from_children(key, opts = {}, &block)
  puts "[HashMap Deprecation Warning] using: #{__callee__} use from_child instead"
  from_child(key, opts, &block)
end

#properties(*args) ⇒ Object



69
70
71
72
73
# File 'lib/hash_map/dsl.rb', line 69

def properties(*args)
  args.each do |arg|
    property(*arg)
  end
end

#property(key, opts = {}, &block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/hash_map/dsl.rb', line 52

def property(key, opts = {}, &block)
  fail InvalidOptionsForProperty, "[HashMap Error] using: `#{__callee__}` with wrong options, second argument must be a `Hash" unless opts.is_a? Hash
  new_hash = {}.tap { |h| h[:key] = single_to_ary(key) }
  new_hash[:proc] = block if block
  new_hash[:from] = generate_from(new_hash, opts)
  attributes << new_hash.merge!(Fusu::Hash.except(opts, :from))
  new_hash
end

#to_child(key, opts = {}, &block) ⇒ Object



99
100
101
102
103
# File 'lib/hash_map/dsl.rb', line 99

def to_child(key, opts = {}, &block)
  flat = _nested(key, opts, &block)
  flat.each { |attr| attr[:key].unshift(key) }
  @attributes += flat
end

#to_children(key, opts = {}, &block) ⇒ Object



94
95
96
97
# File 'lib/hash_map/dsl.rb', line 94

def to_children(key, opts = {}, &block)
  puts "[HashMap Deprecation Warning] using: #{__callee__} use to_child instead"
  to_child(key, opts, &block)
end

#transforms_input(*middlewares) ⇒ Object



43
44
45
46
# File 'lib/hash_map/dsl.rb', line 43

def transforms_input(*middlewares)
  @transform_input ||= []
  @transform_input += middlewares
end

#transforms_output(*middlewares) ⇒ Object



38
39
40
41
# File 'lib/hash_map/dsl.rb', line 38

def transforms_output(*middlewares)
  @transform_output ||= []
  @transform_output += middlewares
end