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 ⇒ Object
- #save! ⇒ 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
186 187 188 |
# File 'lib/property_sets/active_record_extension.rb', line 186 def association_class @association_class ||= proxy_association.klass end |
#build_default(arg) ⇒ Object
141 142 143 |
# File 'lib/property_sets/active_record_extension.rb', line 141 def build_default(arg) build(name: arg.to_s, value: association_class.raw_default(arg)) end |
#disable(arg) ⇒ Object
137 138 139 |
# File 'lib/property_sets/active_record_extension.rb', line 137 def disable(arg) send(:"#{arg}=", "0") end |
#enable(arg) ⇒ Object
133 134 135 |
# File 'lib/property_sets/active_record_extension.rb', line 133 def enable(arg) send(:"#{arg}=", "1") end |
#get(keys = []) ⇒ Object
Accepts an array of names as strings or symbols and returns a hash.
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/property_sets/active_record_extension.rb', line 95 def get(keys = []) property_keys = if keys.empty? association_class.keys else association_class.keys & keys.map(&:to_s) end property_pairs = property_keys.flat_map do |name| value = lookup_value(association_class.type(name), name) [name, value] end HashWithIndifferentAccess[*property_pairs] end |
#lookup(arg) ⇒ Object
The finder method which returns the property if present, otherwise a new instance with defaults
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/property_sets/active_record_extension.rb', line 166 def lookup(arg) instance = lookup_without_default(arg) instance ||= build_default(arg) instance.value_serialized = property_serialized?(arg) owner = proxy_association.owner instance.send(:"#{association_class.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.
179 180 181 182 183 184 |
# File 'lib/property_sets/active_record_extension.rb', line 179 def lookup_or_default(arg) instance = lookup_without_default(arg) instance ||= association_class.new(value: association_class.raw_default(arg)) instance.value_serialized = property_serialized?(arg) instance end |
#lookup_value(type, key) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/property_sets/active_record_extension.rb', line 149 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 = association_class.default(key) if serialized PropertySets::Casting.deserialize(value) else PropertySets::Casting.read(type, value) end end end |
#lookup_without_default(arg) ⇒ Object
145 146 147 |
# File 'lib/property_sets/active_record_extension.rb', line 145 def lookup_without_default(arg) detect { |property| property.name.to_sym == arg.to_sym } end |
#protected?(arg) ⇒ Boolean
129 130 131 |
# File 'lib/property_sets/active_record_extension.rb', line 129 def protected?(arg) lookup(arg).protected? end |
#save ⇒ Object
121 122 123 |
# File 'lib/property_sets/active_record_extension.rb', line 121 def save(...) each { |p| p.save(...) } end |
#save! ⇒ Object
125 126 127 |
# File 'lib/property_sets/active_record_extension.rb', line 125 def save!(...) each { |p| p.save!(...) } 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
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/property_sets/active_record_extension.rb', line 110 def set(property_pairs, with_protection = false) property_pairs.keys.each do |name| record = lookup(name) if with_protection && record.protected? association_class.logger.warn("Someone tried to update the protected #{name} property to #{property_pairs[name]}") else send(:"#{name}=", property_pairs[name]) end end end |