Module: PropertySets::ActiveRecordExtension::AssociationExtensions
- Defined in:
- lib/property_sets/active_record_extension.rb
Instance Method Summary collapse
- #association_class ⇒ Object
- #build_default(arg) ⇒ Object
- #disable(arg) ⇒ Object
- #enable(arg) ⇒ Object
-
#get(keys = []) ⇒ Object
Accepts an array of names as strings or symbols and returns a hash.
-
#lookup(arg) ⇒ Object
The finder method which returns the property if present, otherwise a new instance with defaults.
-
#lookup_or_default(arg) ⇒ Object
This finder method returns the property if present, otherwise a new instance with the default value.
- #lookup_value(type, key) ⇒ Object
- #lookup_without_default(arg) ⇒ Object
- #protected?(arg) ⇒ Boolean
- #save(*args) ⇒ Object
- #save!(*args) ⇒ Object
-
#set(property_pairs, with_protection = false) ⇒ Object
Accepts a name value pair hash { :name => ‘value’, :pairs => true } and builds a property for each key.
Instance Method Details
#association_class ⇒ Object
159 160 161 162 163 164 165 166 167 |
# File 'lib/property_sets/active_record_extension.rb', line 159 def association_class @association_class ||= begin if ActiveRecord::VERSION::STRING >= "3.1.0" proxy_association.klass else @reflection.klass end end end |
#build_default(arg) ⇒ Object
114 115 116 |
# File 'lib/property_sets/active_record_extension.rb', line 114 def build_default(arg) build(:name => arg.to_s, :value => raw_default(arg)) end |
#disable(arg) ⇒ Object
110 111 112 |
# File 'lib/property_sets/active_record_extension.rb', line 110 def disable(arg) send("#{arg}=", "0") end |
#enable(arg) ⇒ Object
106 107 108 |
# File 'lib/property_sets/active_record_extension.rb', line 106 def enable(arg) send("#{arg}=", "1") end |
#get(keys = []) ⇒ Object
Accepts an array of names as strings or symbols and returns a hash.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/property_sets/active_record_extension.rb', line 68 def get(keys = []) property_keys = if keys.empty? association_class.keys else association_class.keys & keys.map(&:to_s) end property_pairs = property_keys.map do |name| value = lookup_value(association_class.type(name), name) [name, value] end.flatten(1) HashWithIndifferentAccess[*property_pairs] end |
#lookup(arg) ⇒ Object
The finder method which returns the property if present, otherwise a new instance with defaults
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/property_sets/active_record_extension.rb', line 139 def lookup(arg) instance = lookup_without_default(arg) instance ||= build_default(arg) instance.value_serialized = property_serialized?(arg) owner = proxy_association.owner instance.send("#{owner_class_sym}=", owner) if owner.new_record? instance end |
#lookup_or_default(arg) ⇒ Object
This finder method returns the property if present, otherwise a new instance with the default value. It does not have the side effect of adding a new setting object.
152 153 154 155 156 157 |
# File 'lib/property_sets/active_record_extension.rb', line 152 def lookup_or_default(arg) instance = lookup_without_default(arg) instance ||= association_class.new(:value => raw_default(arg)) instance.value_serialized = property_serialized?(arg) instance end |
#lookup_value(type, key) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/property_sets/active_record_extension.rb', line 122 def lookup_value(type, key) serialized = property_serialized?(key) if instance = lookup_without_default(key) instance.value_serialized = serialized PropertySets::Casting.read(type, instance.value) else value = default(key) if serialized PropertySets::Casting.deserialize(value) else PropertySets::Casting.read(type, value) end end end |
#lookup_without_default(arg) ⇒ Object
118 119 120 |
# File 'lib/property_sets/active_record_extension.rb', line 118 def lookup_without_default(arg) detect { |property| property.name.to_sym == arg.to_sym } end |
#protected?(arg) ⇒ Boolean
102 103 104 |
# File 'lib/property_sets/active_record_extension.rb', line 102 def protected?(arg) lookup(arg).protected? end |
#save(*args) ⇒ Object
94 95 96 |
# File 'lib/property_sets/active_record_extension.rb', line 94 def save(*args) each { |p| p.save(*args) } end |
#save!(*args) ⇒ Object
98 99 100 |
# File 'lib/property_sets/active_record_extension.rb', line 98 def save!(*args) each { |p| p.save!(*args) } end |
#set(property_pairs, with_protection = false) ⇒ Object
Accepts a name value pair hash { :name => ‘value’, :pairs => true } and builds a property for each key
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/property_sets/active_record_extension.rb', line 83 def set(property_pairs, with_protection = false) property_pairs.keys.each do |name| record = lookup(name) if with_protection && record.protected? logger.warn("Someone tried to update the protected #{name} property to #{property_pairs[name]}") else send("#{name}=", property_pairs[name]) end end end |