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
187 188 189 |
# File 'lib/property_sets/active_record_extension.rb', line 187 def association_class @association_class ||= proxy_association.klass end |
#build_default(arg) ⇒ Object
142 143 144 |
# File 'lib/property_sets/active_record_extension.rb', line 142 def build_default(arg) build(name: arg.to_s, value: association_class.raw_default(arg)) end |
#disable(arg) ⇒ Object
138 139 140 |
# File 'lib/property_sets/active_record_extension.rb', line 138 def disable(arg) send(:"#{arg}=", "0") end |
#enable(arg) ⇒ Object
134 135 136 |
# File 'lib/property_sets/active_record_extension.rb', line 134 def enable(arg) send(:"#{arg}=", "1") end |
#get(keys = []) ⇒ Object
Accepts an array of names as strings or symbols and returns a hash.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/property_sets/active_record_extension.rb', line 96 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
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/property_sets/active_record_extension.rb', line 167 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.
180 181 182 183 184 185 |
# File 'lib/property_sets/active_record_extension.rb', line 180 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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/property_sets/active_record_extension.rb', line 150 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
146 147 148 |
# File 'lib/property_sets/active_record_extension.rb', line 146 def lookup_without_default(arg) detect { |property| property.name.to_sym == arg.to_sym } end |
#protected?(arg) ⇒ Boolean
130 131 132 |
# File 'lib/property_sets/active_record_extension.rb', line 130 def protected?(arg) lookup(arg).protected? end |
#save ⇒ Object
122 123 124 |
# File 'lib/property_sets/active_record_extension.rb', line 122 def save(...) each { |p| p.save(...) } end |
#save! ⇒ Object
126 127 128 |
# File 'lib/property_sets/active_record_extension.rb', line 126 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
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/property_sets/active_record_extension.rb', line 111 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 |