Module: InitializeWith

Defined in:
lib/initialize_with.rb

Instance Method Summary collapse

Instance Method Details

#initialize_with(*args) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/initialize_with.rb', line 2

def initialize_with(*args)
  case a = args.shift
  when Array then h = args.pop || {}
  when Hash then h, a = [], h
  end

  unless a.is_a?(Array) && h.is_a?(Hash)
    raise TypeError,
      "wrong type of arguments for initialize_with" <<
      " expected [Array, Hash], but have [#{a.class}, #{h.class}]"
  end

  h.reject! { |k,_| a.any? { |name| name.to_s == k.to_s} }

  required = a.join(', ')

  optional = h.map do |k, v|
    "#{k} = #{v.to_s}"
  end.join(', ')

  optional = ", #{optional}" unless optional.empty?

  assign = (a + h.keys).map do |name|
    "@#{name} = #{name}"
  end.join("\n")

  class_eval <<-MTD, __FILE__, __LINE__
    def initialize(#{required}#{optional})    #   def initialize(a, b, c = 1, d = 2)
      #{assign}                                 #     @a = a
    end                                         #     @b = b
                                                #     @c = c
                                                #     @d = d
                                                #   end
  MTD
end