Class: Class
- Defined in:
- lib/blather/core_ext/active_support/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
53 54 55 56 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 53 def class_inheritable_accessor(*syms) class_inheritable_reader(*syms) class_inheritable_writer(*syms) end |
#class_inheritable_array(*syms) ⇒ Object
58 59 60 61 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 58 def class_inheritable_array(*syms) class_inheritable_reader(*syms) class_inheritable_array_writer(*syms) end |
#class_inheritable_array_writer(*syms) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 33 def class_inheritable_array_writer(*syms) syms.each do |sym| class_eval <<-EOS def self.#{sym}=(obj) write_inheritable_array(:#{sym}, obj) end EOS end end |
#class_inheritable_hash(*syms) ⇒ Object
63 64 65 66 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 63 def class_inheritable_hash(*syms) class_inheritable_reader(*syms) class_inheritable_hash_writer(*syms) end |
#class_inheritable_hash_writer(*syms) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 43 def class_inheritable_hash_writer(*syms) syms.each do |sym| class_eval <<-EOS def self.#{sym}=(obj) write_inheritable_hash(:#{sym}, obj) end EOS end end |
#class_inheritable_reader(*syms) ⇒ Object
:nodoc:
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 8 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
23 24 25 26 27 28 29 30 31 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 23 def class_inheritable_writer(*syms) syms.each do |sym| class_eval <<-EOS def self.#{sym}=(obj) write_inheritable_attribute(:#{sym}, obj) end EOS end end |
#inheritable_attributes ⇒ Object
68 69 70 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 68 def inheritable_attributes @inheritable_attributes ||= EMPTY_INHERITABLE_ATTRIBUTES end |
#read_inheritable_attribute(key) ⇒ Object
89 90 91 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 89 def read_inheritable_attribute(key) inheritable_attributes[key] end |
#reset_inheritable_attributes ⇒ Object
93 94 95 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 93 def reset_inheritable_attributes @inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES end |
#write_inheritable_array(key, elements) ⇒ Object
79 80 81 82 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 79 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
72 73 74 75 76 77 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 72 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
84 85 86 87 |
# File 'lib/blather/core_ext/active_support/inheritable_attributes.rb', line 84 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 |