Class: SmartProperties::PropertyCollection
- Inherits:
-
Object
- Object
- SmartProperties::PropertyCollection
- Includes:
- Enumerable
- Defined in:
- lib/smart_properties/property_collection.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
protected
Returns the value of attribute children.
-
#collection ⇒ Object
protected
Returns the value of attribute collection.
-
#collection_with_parent_collection ⇒ Object
protected
Returns the value of attribute collection_with_parent_collection.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize ⇒ PropertyCollection
constructor
A new instance of PropertyCollection.
- #key?(name) ⇒ Boolean
- #keys ⇒ Object
- #notify_children ⇒ Object protected
- #refresh(parent_collection) ⇒ Object protected
- #register(child) ⇒ Object
- #to_hash ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ PropertyCollection
Returns a new instance of PropertyCollection.
23 24 25 26 27 |
# File 'lib/smart_properties/property_collection.rb', line 23 def initialize @collection = {} @collection_with_parent_collection = {} @children = [] end |
Instance Attribute Details
#children ⇒ Object (protected)
Returns the value of attribute children.
70 71 72 |
# File 'lib/smart_properties/property_collection.rb', line 70 def children @children end |
#collection ⇒ Object (protected)
Returns the value of attribute collection.
71 72 73 |
# File 'lib/smart_properties/property_collection.rb', line 71 def collection @collection end |
#collection_with_parent_collection ⇒ Object (protected)
Returns the value of attribute collection_with_parent_collection.
72 73 74 |
# File 'lib/smart_properties/property_collection.rb', line 72 def collection_with_parent_collection @collection_with_parent_collection end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/smart_properties/property_collection.rb', line 5 def parent @parent end |
Class Method Details
.for(scope) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/smart_properties/property_collection.rb', line 7 def self.for(scope) parents = scope.ancestors[1..-1].select do |ancestor| ancestor.ancestors.include?(SmartProperties) && ancestor != scope && ancestor != SmartProperties end collection = new parents.reverse.each do |parent| parent.properties.register(collection) end collection end |
Instance Method Details
#[](name) ⇒ Object
37 38 39 |
# File 'lib/smart_properties/property_collection.rb', line 37 def [](name) collection_with_parent_collection[name.to_s] end |
#[]=(name, value) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/smart_properties/property_collection.rb', line 29 def []=(name, value) name = name.to_s collection[name] = value collection_with_parent_collection[name] = value notify_children value end |
#each(&block) ⇒ Object
53 54 55 56 |
# File 'lib/smart_properties/property_collection.rb', line 53 def each(&block) return to_enum(:each) if block.nil? collection_with_parent_collection.each { |name, value| block.call([name.to_sym, value]) } end |
#key?(name) ⇒ Boolean
41 42 43 |
# File 'lib/smart_properties/property_collection.rb', line 41 def key?(name) collection_with_parent_collection.key?(name.to_s) end |
#keys ⇒ Object
45 46 47 |
# File 'lib/smart_properties/property_collection.rb', line 45 def keys collection_with_parent_collection.keys.map(&:to_sym) end |
#notify_children ⇒ Object (protected)
74 75 76 |
# File 'lib/smart_properties/property_collection.rb', line 74 def notify_children @children.each { |child| child.refresh(collection_with_parent_collection) } end |
#refresh(parent_collection) ⇒ Object (protected)
78 79 80 81 82 |
# File 'lib/smart_properties/property_collection.rb', line 78 def refresh(parent_collection) @collection_with_parent_collection.merge!(parent_collection) notify_children nil end |
#register(child) ⇒ Object
62 63 64 65 66 |
# File 'lib/smart_properties/property_collection.rb', line 62 def register(child) children.push(child) child.refresh(collection_with_parent_collection) nil end |
#to_hash ⇒ Object
58 59 60 |
# File 'lib/smart_properties/property_collection.rb', line 58 def to_hash Hash[each.to_a] end |
#values ⇒ Object
49 50 51 |
# File 'lib/smart_properties/property_collection.rb', line 49 def values collection_with_parent_collection.values end |