class HashMap::DSL

Attributes

attributes[R]

Public Class Methods

new() click to toggle source
# File lib/hash_map/dsl.rb, line 29
def initialize
  @attributes = []
  @after_each = Set.new
  @transform_output = Set.new
  @transform_input = Set.new
end

Public Instance Methods

_set_attributes(attrs) click to toggle source
# File lib/hash_map/dsl.rb, line 53
def _set_attributes(attrs)
  @attributes = attrs
end
after_each(*middlewares) click to toggle source
# File lib/hash_map/dsl.rb, line 36
def after_each(*middlewares)
  @after_each += middlewares
end
collection(key, opts = {}, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_keys() click to toggle source
# File lib/hash_map/dsl.rb, line 48
def only_provided_keys
  @transform_output << RemoveUnprovideds
  @after_each << MarkUnprovided
end
properties(*args) click to toggle source
# File lib/hash_map/dsl.rb, line 74
def properties(*args)
  args.each do |arg|
    property(*arg)
  end
end
property(key, opts = {}, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/hash_map/dsl.rb, line 44
def transforms_input(*middlewares)
  @transform_input += middlewares
end
transforms_output(*middlewares) click to toggle source
# File lib/hash_map/dsl.rb, line 40
def transforms_output(*middlewares)
  @transform_output += middlewares
end

Private Instance Methods

_nested(_key, _opts = {}, &block) click to toggle source
# File lib/hash_map/dsl.rb, line 117
def _nested(_key, _opts = {}, &block)
  klass = self.class.new
  klass.instance_exec(&block)
  klass.attributes.flatten
end
generate_from(hash, opts) click to toggle source
# File lib/hash_map/dsl.rb, line 112
def generate_from(hash, opts)
  from = opts[:from]
  from ? single_to_ary(from) : hash[:key].dup
end
single_to_ary(elem) click to toggle source
# File lib/hash_map/dsl.rb, line 123
def single_to_ary(elem)
  Fusu::Array.wrap(elem)
end