Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/handshake/proxy_self.rb,
lib/handshake/inheritable_attributes.rb

Overview

Allows attributes to be shared within an inheritance hierarchy, but where each descendant gets a copy of their parents’ attributes, instead of just a pointer to the same. This means that the child can add elements to, for example, an array without those additions being shared with either their parent, siblings, or children, which is unlike the regular class-level attributes that are shared across the entire hierarchy.

Instance Method Summary collapse

Instance Method Details

#class_inheritable_accessor(*syms) ⇒ Object



83
84
85
86
# File 'lib/handshake/inheritable_attributes.rb', line 83

def class_inheritable_accessor(*syms)
  class_inheritable_reader(*syms)
  class_inheritable_writer(*syms)
end

#class_inheritable_array(*syms) ⇒ Object



88
89
90
91
# File 'lib/handshake/inheritable_attributes.rb', line 88

def class_inheritable_array(*syms)
  class_inheritable_reader(*syms)
  class_inheritable_array_writer(*syms)
end

#class_inheritable_array_writer(*syms) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/handshake/inheritable_attributes.rb', line 55

def class_inheritable_array_writer(*syms)
  syms.each do |sym|
    class_eval <<-EOS
      def self.#{sym}=(obj)
        write_inheritable_array(:#{sym}, obj)
      end

      def #{sym}=(obj)
        self.class.#{sym} = obj
      end
    EOS
  end
end

#class_inheritable_hash(*syms) ⇒ Object



93
94
95
96
# File 'lib/handshake/inheritable_attributes.rb', line 93

def class_inheritable_hash(*syms)
  class_inheritable_reader(*syms)
  class_inheritable_hash_writer(*syms)
end

#class_inheritable_hash_writer(*syms) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/handshake/inheritable_attributes.rb', line 69

def class_inheritable_hash_writer(*syms)
  syms.each do |sym|
    class_eval <<-EOS
      def self.#{sym}=(obj)
        write_inheritable_hash(:#{sym}, obj)
      end

      def #{sym}=(obj)
        self.class.#{sym} = obj
      end
    EOS
  end
end

#class_inheritable_reader(*syms) ⇒ Object

:nodoc:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/handshake/inheritable_attributes.rb', line 27

def class_inheritable_reader(*syms)
  syms.each do |sym|
    class_eval <<-EOS
      def self.#{sym}
        read_inheritable_attribute(:#{sym})
      end

      def #{sym}
        self.class.#{sym}
      end
    EOS
  end
end

#class_inheritable_writer(*syms) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/handshake/inheritable_attributes.rb', line 41

def class_inheritable_writer(*syms)
  syms.each do |sym|
    class_eval <<-EOS
      def self.#{sym}=(obj)
        write_inheritable_attribute(:#{sym}, obj)
      end

      def #{sym}=(obj)
        self.class.#{sym} = obj
      end
    EOS
  end
end

#inheritable_attributesObject



98
99
100
# File 'lib/handshake/inheritable_attributes.rb', line 98

def inheritable_attributes
  @inheritable_attributes ||= {}
end

#proxy_self(*meths) ⇒ Object

:nodoc:



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

def proxy_self(*meths)
  meths.each do |meth|
    class_eval <<-EOS
      def #{meth}(*args, &block)
        self.send(:#{meth}, *args, &block)
      end
    EOS
  end
  nil
end

#read_inheritable_attribute(key) ⇒ Object



116
117
118
# File 'lib/handshake/inheritable_attributes.rb', line 116

def read_inheritable_attribute(key)
  inheritable_attributes[key]
end

#reset_inheritable_attributesObject



120
121
122
# File 'lib/handshake/inheritable_attributes.rb', line 120

def reset_inheritable_attributes
  inheritable_attributes.clear
end

#write_inheritable_array(key, elements) ⇒ Object



106
107
108
109
# File 'lib/handshake/inheritable_attributes.rb', line 106

def write_inheritable_array(key, elements)
  write_inheritable_attribute(key, []) if read_inheritable_attribute(key).nil?
  write_inheritable_attribute(key, read_inheritable_attribute(key) + elements)
end

#write_inheritable_attribute(key, value) ⇒ Object



102
103
104
# File 'lib/handshake/inheritable_attributes.rb', line 102

def write_inheritable_attribute(key, value)
  inheritable_attributes[key] = value
end

#write_inheritable_hash(key, hash) ⇒ Object



111
112
113
114
# File 'lib/handshake/inheritable_attributes.rb', line 111

def write_inheritable_hash(key, hash)
  write_inheritable_attribute(key, {}) if read_inheritable_attribute(key).nil?
  write_inheritable_attribute(key, read_inheritable_attribute(key).merge(hash))
end