Module: HashAttrs

Defined in:
lib/hash_attrs.rb

Overview

A module which adds some generators for hash based accessors.

Instance Method Summary collapse

Instance Method Details

#hash_accessor(hash, *syms) ⇒ Object



29
30
31
32
# File 'lib/hash_attrs.rb', line 29

def hash_accessor(hash, *syms)
  hash_reader(hash, syms)
  hash_writer(hash, syms)
end

#hash_reader(hash_sym, syms) ⇒ Object



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

def hash_reader(hash_sym, syms)
  syms.each do |id|
    id = id.to_s.downcase
    func = Proc.new do
      hash = instance_variable_get(hash_sym)
      hash[id.to_sym] 
    end

    self.send(:define_method, id, func)
  end
end

#hash_writer(hash_sym, syms) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hash_attrs.rb', line 16

def hash_writer(hash_sym, syms)
  syms.each do |id|
    id = id.to_s.downcase

    func = Proc.new do |val| 
      hash = instance_variable_get(hash_sym)
      hash[id.to_sym] = val 
    end

    self.send(:define_method, id+'=', func)
  end
end