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
29
30
31
32
# File 'lib/attribute_cartographer.rb', line 12

def map *args
  @mapper ||= {}

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

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

  if Array === from
    if f1.arity == 1
      from.each { |k| @mapper[k] = [k, f1] }
    else
      from.each { |k| @mapper[k] = f1 }
    end
  else
    raise AttributeCartographer::InvalidArgumentError if to && f1.arity == 2

    to ||= from
    @mapper[from] = (f1.arity == 1 ? [to, f1] : f1)
    @mapper[to] = [from, f2] if f2
  end
end