Top Level Namespace

Defined Under Namespace

Modules: AttrInit

Instance Method Summary collapse

Instance Method Details

#accessor_struct(*attrs) ⇒ Object



31
32
33
34
35
# File 'lib/attr_init.rb', line 31

def accessor_struct(*attrs)
  attr_accessor *attrs
  attr_init *attrs
  attr_hash *attrs
end

#attr_hash(*attrs) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/attr_init.rb', line 17

def attr_hash(*attrs)
  define_method(:to_h) do
    attrs.each_with_object({}) do |attr, hash|
      hash[attr] = send(attr)
    end
  end
end

#attr_init(*attrs) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/attr_init.rb', line 3

def attr_init(*attrs)
  define_method(:attr_init) do |params|
    attrs.each do |a|
      instance_variable_set "@#{a}", params[a]
    end
  end

  protected :attr_init

  define_method(:initialize) do |params|
    attr_init(params)
  end
end

#reader_struct(*attrs) ⇒ Object



25
26
27
28
29
# File 'lib/attr_init.rb', line 25

def reader_struct(*attrs)
  attr_reader *attrs
  attr_init *attrs
  attr_hash *attrs
end