Module: Kinda::Core::Accessor::ClassMethods

Included in:
Kinda::Core::Accessor
Defined in:
lib/core/accessor.rb

Instance Method Summary collapse

Instance Method Details

#__attr_reader__(*attributes, with_filter, &block) ⇒ Object

attr_reader(:name, :age, :size) attr_reader(:name => :@name) is the default behaviour but you can use: attr_reader (:name => @the_name) to use a different instance variable attr_reader(:name => :first_name) or even an another attribute. But more interesting, you can delegate to another object: attr_reader(:name => [:father, :name]) attr_reader(:name => [:mother, :husband, :name])



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/core/accessor.rb', line 12

def __attr_reader__(*attributes, with_filter, &block)
  attributes.each do |attribute|
    if attribute.is_a?(Hash)
      attribute, source = attribute.first
    else
      source = "@#{attribute}"
    end
    proc = if with_filter || !block
      ::Proc.new do |*args|
        return send("#{attribute}=", *args) unless args.empty?
        value = if source[0] == '@'
           instance_variable_get(source)
        else
          [*source].inject(self) do |object, symbol|
            object.send(symbol)
          end
        end
        block ? instance_exec(value, &block) : value
      end
    else
      block
    end
    to_class.send(:define_method, attribute, &proc)
  end
end

#__attr_writer__(*attributes, with_filter, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/core/accessor.rb', line 46

def __attr_writer__(*attributes, with_filter, &block)
  attributes.each do |attribute|
    if attribute.is_a?(Hash)
      attribute, target = attribute.first
    else
      target = "@#{attribute}"
    end
    proc = if with_filter || !block
      ::Proc.new do |value|
        value = instance_exec(value, &block) if block
        if target[0] == '@'
          instance_variable_set(target, value)
        else
          [*target][0..-2].inject(self) do |object, symbol|
            object.send(symbol)
          end.send("#{[*target].last}=", value)
        end
      end
    else
      block
    end
    to_class.send(:define_method, "#{attribute}=", &proc)
  end
end

#attr(attribute, writable = false) ⇒ Object



89
90
91
92
# File 'lib/core/accessor.rb', line 89

def attr(attribute, writable=false)
  attr_reader(attribute)
  attr_writer(attribute) if writable
end

#attr_accessor(*attributes, &block) ⇒ Object



79
80
81
82
# File 'lib/core/accessor.rb', line 79

def attr_accessor(*attributes, &block)
  attr_reader(*attributes, &block)
  attr_writer(*attributes, &block)
end

#attr_accessor_with_default(attribute, default_value) ⇒ Object



100
101
102
103
# File 'lib/core/accessor.rb', line 100

def attr_accessor_with_default(attribute, default_value)
  attr_reader_with_default(attribute, default_value)
  attr_writer(attribute)
end

#attr_accessor_with_filter(*attributes, &block) ⇒ Object



84
85
86
87
# File 'lib/core/accessor.rb', line 84

def attr_accessor_with_filter(*attributes, &block)
  attr_reader_with_filter(*attributes, &block)
  attr_writer_with_filter(*attributes, &block)
end

#attr_reader(*attributes, &block) ⇒ Object



38
39
40
# File 'lib/core/accessor.rb', line 38

def attr_reader(*attributes, &block)
  __attr_reader__(*attributes, false, &block)
end

#attr_reader_with_default(attribute, default_value) ⇒ Object



94
95
96
97
98
# File 'lib/core/accessor.rb', line 94

def attr_reader_with_default(attribute, default_value)
  attr_reader_with_filter(attribute) do |value|
    value.nil? ? default_value : value
  end
end

#attr_reader_with_filter(*attributes, &block) ⇒ Object



42
43
44
# File 'lib/core/accessor.rb', line 42

def attr_reader_with_filter(*attributes, &block)
  __attr_reader__(*attributes, true, &block)
end

#attr_writer(*attributes, &block) ⇒ Object



71
72
73
# File 'lib/core/accessor.rb', line 71

def attr_writer(*attributes, &block)
  __attr_writer__(*attributes, false, &block)
end

#attr_writer_with_filter(*attributes, &block) ⇒ Object



75
76
77
# File 'lib/core/accessor.rb', line 75

def attr_writer_with_filter(*attributes, &block)
  __attr_writer__(*attributes, true, &block)
end