Class: PollEverywhere::Serializable::Property::Value::Set
- Inherits:
-
CoreExt::HashWithIndifferentAccess
- Object
- Hash
- CoreExt::HashWithIndifferentAccess
- PollEverywhere::Serializable::Property::Value::Set
- Defined in:
- lib/polleverywhere/serializable.rb
Overview
Manage a collection of values so that you can validate, commit, detect changes, etc in bulk
Instance Method Summary collapse
-
#[](prop_name) ⇒ Object
Access the current value.
-
#[]=(prop_name, value) ⇒ Object
Set the current value so that we can track dirty changes.
-
#changed? ⇒ Boolean
Detects if all properties have been changed.
-
#changes ⇒ Object
Returns a hash with a => [‘original’, ‘changed’].
-
#commit ⇒ Object
Commit changes.
-
#initialize(*values, &block) ⇒ Set
constructor
A new instance of Set.
-
#prop(prop_name) ⇒ Object
Prettier way to get at a prop so it doesn’t feel so hashy.
-
#props ⇒ Object
Our collection of property values…
Methods inherited from CoreExt::HashWithIndifferentAccess
#delete, #merge, #merge!, #values_at
Constructor Details
#initialize(*values, &block) ⇒ Set
Returns a new instance of Set.
53 54 55 56 57 58 |
# File 'lib/polleverywhere/serializable.rb', line 53 def initialize(*values, &block) # Copy the values and property names into the hash values.each do |value| props[value.property.name] = value end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class PollEverywhere::CoreExt::HashWithIndifferentAccess
Instance Method Details
#[](prop_name) ⇒ Object
Access the current value
66 67 68 |
# File 'lib/polleverywhere/serializable.rb', line 66 def [](prop_name) prop(prop_name).current end |
#[]=(prop_name, value) ⇒ Object
Set the current value so that we can track dirty changes
61 62 63 |
# File 'lib/polleverywhere/serializable.rb', line 61 def []=(prop_name, value) prop(prop_name).current = value end |
#changed? ⇒ Boolean
Detects if all properties have been changed
79 80 81 |
# File 'lib/polleverywhere/serializable.rb', line 79 def changed? props.values.any?(&:changed?) end |
#changes ⇒ Object
Returns a hash with a => [‘original’, ‘changed’].
71 72 73 74 75 76 |
# File 'lib/polleverywhere/serializable.rb', line 71 def changes props.inject(CoreExt::HashWithIndifferentAccess.new) do |hash, (prop_name, value)| hash[prop_name] = value.changes if value.changed? hash end end |
#commit ⇒ Object
Commit changes
84 85 86 |
# File 'lib/polleverywhere/serializable.rb', line 84 def commit props.values.each(&:commit) end |
#prop(prop_name) ⇒ Object
Prettier way to get at a prop so it doesn’t feel so hashy
96 97 98 |
# File 'lib/polleverywhere/serializable.rb', line 96 def prop(prop_name) props[prop_name] end |
#props ⇒ Object
Our collection of property values… I call them props under the assumption that they’re returning values.
89 90 91 92 93 |
# File 'lib/polleverywhere/serializable.rb', line 89 def props @props ||= CoreExt::HashWithIndifferentAccess.new do |hash, key| hash[key] = Property.new(key).value end end |