Class: Class
- Inherits:
-
Object
- Object
- Class
- Defined in:
- lib/autumn/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.
Constant Summary collapse
- EMPTY_INHERITABLE_ATTRIBUTES =
Prevent this constant from being created multiple times
{}.freeze
Instance Method Summary collapse
- #class_inheritable_accessor(*syms) ⇒ Object
- #class_inheritable_array(*syms) ⇒ Object
- #class_inheritable_array_writer(*syms) ⇒ Object
- #class_inheritable_hash(*syms) ⇒ Object
- #class_inheritable_hash_writer(*syms) ⇒ Object
-
#class_inheritable_reader(*syms) ⇒ Object
:nodoc:.
- #class_inheritable_writer(*syms) ⇒ Object
- #inheritable_attributes ⇒ Object
- #read_inheritable_attribute(key) ⇒ Object
- #reset_inheritable_attributes ⇒ Object
- #write_inheritable_array(key, elements) ⇒ Object
- #write_inheritable_attribute(key, value) ⇒ Object
- #write_inheritable_hash(key, hash) ⇒ Object
Instance Method Details
#class_inheritable_accessor(*syms) ⇒ Object
98 99 100 101 |
# File 'lib/autumn/inheritable_attributes.rb', line 98 def class_inheritable_accessor(*syms) class_inheritable_reader(*syms) class_inheritable_writer(*syms) end |
#class_inheritable_array(*syms) ⇒ Object
103 104 105 106 |
# File 'lib/autumn/inheritable_attributes.rb', line 103 def class_inheritable_array(*syms) class_inheritable_reader(*syms) class_inheritable_array_writer(*syms) end |
#class_inheritable_array_writer(*syms) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/autumn/inheritable_attributes.rb', line 64 def class_inheritable_array_writer(*syms) = syms.last.is_a?(Hash) ? syms.pop : {} 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 " unless [:instance_writer] == false } EOS end end |
#class_inheritable_hash(*syms) ⇒ Object
108 109 110 111 |
# File 'lib/autumn/inheritable_attributes.rb', line 108 def class_inheritable_hash(*syms) class_inheritable_reader(*syms) class_inheritable_hash_writer(*syms) end |
#class_inheritable_hash_writer(*syms) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/autumn/inheritable_attributes.rb', line 81 def class_inheritable_hash_writer(*syms) = syms.last.is_a?(Hash) ? syms.pop : {} 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 " unless [:instance_writer] == false } EOS end end |
#class_inheritable_reader(*syms) ⇒ Object
:nodoc:
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/autumn/inheritable_attributes.rb', line 32 def class_inheritable_reader(*syms) syms.each do |sym| next if sym.is_a?(Hash) 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/autumn/inheritable_attributes.rb', line 47 def class_inheritable_writer(*syms) = syms.last.is_a?(Hash) ? syms.pop : {} 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 " unless [:instance_writer] == false } EOS end end |
#inheritable_attributes ⇒ Object
113 114 115 |
# File 'lib/autumn/inheritable_attributes.rb', line 113 def inheritable_attributes @inheritable_attributes ||= EMPTY_INHERITABLE_ATTRIBUTES end |
#read_inheritable_attribute(key) ⇒ Object
134 135 136 |
# File 'lib/autumn/inheritable_attributes.rb', line 134 def read_inheritable_attribute(key) inheritable_attributes[key] end |
#reset_inheritable_attributes ⇒ Object
138 139 140 |
# File 'lib/autumn/inheritable_attributes.rb', line 138 def reset_inheritable_attributes @inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES end |
#write_inheritable_array(key, elements) ⇒ Object
124 125 126 127 |
# File 'lib/autumn/inheritable_attributes.rb', line 124 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
117 118 119 120 121 122 |
# File 'lib/autumn/inheritable_attributes.rb', line 117 def write_inheritable_attribute(key, value) if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES) @inheritable_attributes = {} end inheritable_attributes[key] = value end |
#write_inheritable_hash(key, hash) ⇒ Object
129 130 131 132 |
# File 'lib/autumn/inheritable_attributes.rb', line 129 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 |