Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/functional_support/core_ext/class/attribute_accessors.rb

Instance Method Summary collapse

Instance Method Details

#attr_hash_accessor(*syms) ⇒ Object



32
33
34
35
# File 'lib/functional_support/core_ext/class/attribute_accessors.rb', line 32

def attr_hash_accessor(*syms)
  attr_hash_writer *syms
  attr_hash_reader *syms
end

#attr_hash_reader(*syms) ⇒ Object



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

def attr_hash_reader(*syms)
  options = syms.extract_options!
  hash_name = options[:store_in].to_s if options[:store_in].present?
  hash_name ||= "attributes"
  syms.each do |sym|
    raise NameError.new("invalid instance attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      def #{sym}
        (@#{hash_name} ||= {})[:#{sym}]
      end
    EOS
  end
end

#attr_hash_writer(*syms) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/functional_support/core_ext/class/attribute_accessors.rb', line 17

def attr_hash_writer(*syms)
  options = syms.extract_options!
  hash_name = options[:store_in].to_s if options[:store_in].present?
  hash_name ||= "attributes"
  syms.each do |sym|
    raise NameError.new("invalid instance attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      def #{sym}=(obj)
        @#{hash_name} ||= {}
        @#{hash_name}[:#{sym}] = obj
      end
    EOS
  end
end