Module: AttributeCartographer::ClassMethods

Defined in:
lib/attribute_cartographer.rb

Instance Method Summary collapse

Instance Method Details

#map(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/attribute_cartographer.rb', line 12

def map *args
  @mapper ||= {}

  (from, to), (f1, f2) = args.partition { |a| !(Proc === a) }

  raise AttributeCartographer::InvalidArgumentError if [f1,f2].compact.any? { |f| f.arity > 1 }

  f1 ||= ->(v) { v }
  to ||= from

  if Array === from
    from.each { |key| @mapper.merge! key => [key, f1] }
  else
    @mapper.merge! from => [to, f1]
    @mapper.merge! to => [from, f2] if f2
  end
end