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
32
33
34
# File 'lib/hash_map/dsl.rb', line 29

def initialize
  @attributes = []
  @after_each = Set.new
  @transform_output = Set.new
  @transform_input = Set.new
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



53
54
55
# File 'lib/hash_map/dsl.rb', line 53

def _set_attributes(attrs)
  @attributes = attrs
end

#after_each(*middlewares) ⇒ Object



36
37
38
# File 'lib/hash_map/dsl.rb', line 36

def after_each(*middlewares)
  @after_each += middlewares
end

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



66
67
68
69
70
71
72
# File 'lib/hash_map/dsl.rb', line 66

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



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hash_map/dsl.rb', line 85

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



80
81
82
83
# File 'lib/hash_map/dsl.rb', line 80

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

#only_provided_keysObject



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

def only_provided_keys
  @transform_output << RemoveUnprovideds
  @after_each << MarkUnprovided
end

#properties(*args) ⇒ Object



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

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

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



57
58
59
60
61
62
63
64
# File 'lib/hash_map/dsl.rb', line 57

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



104
105
106
107
108
# File 'lib/hash_map/dsl.rb', line 104

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



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

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



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

def transforms_input(*middlewares)
  @transform_input += middlewares
end

#transforms_output(*middlewares) ⇒ Object



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

def transforms_output(*middlewares)
  @transform_output += middlewares
end