module PropertySets::ActiveRecordExtension::AssociationExtensions
Public Instance Methods
association_class()
click to toggle source
# File lib/property_sets/active_record_extension.rb, line 186 def association_class @association_class ||= proxy_association.klass end
build_default(arg)
click to toggle source
# 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)
click to toggle source
# File lib/property_sets/active_record_extension.rb, line 137 def disable(arg) send(:"#{arg}=", "0") end
enable(arg)
click to toggle source
# File lib/property_sets/active_record_extension.rb, line 133 def enable(arg) send(:"#{arg}=", "1") end
get(keys = [])
click to toggle source
Accepts an array of names as strings or symbols and returns a hash.
# 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)
click to toggle source
The finder method which returns the property if present, otherwise a new instance with defaults
# 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)
click to toggle source
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.
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/property_sets/active_record_extension.rb, line 129 def protected?(arg) lookup(arg).protected? end
save(...)
click to toggle source
# File lib/property_sets/active_record_extension.rb, line 121 def save(...) each { |p| p.save(...) } end
save!(...)
click to toggle source
# File lib/property_sets/active_record_extension.rb, line 125 def save!(...) each { |p| p.save!(...) } end
set(property_pairs, with_protection = false)
click to toggle source
Accepts a name value pair hash { :name => ‘value’, :pairs => true } and builds a property for each key
# 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