Module: Cardiac::Model::Attributes
Overview
Cardiac::Model attribute methods. Some of this has been “borrowed” from ActiveRecord.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#id ⇒ Object
This baseline implementation uses this model’s id_delimiter to join what is returned by to_key.
-
#remote_attributes ⇒ Object
Retrieves the most recently unpacked/decoded remote attributes.
-
#to_key ⇒ Object
Overridden to use this model’s key_attributes to build the key.
-
#typecast_attribute(typecaster, value) ⇒ Object
Overridden to convert empty strings to nil before typecasting.
Instance Method Details
#id ⇒ Object
This baseline implementation uses this model’s id_delimiter to join what is returned by to_key. If the id_delimiter is set to nil, it will simply return an array instead.
NOTE: Defining an :id attribute on your model will override this implementation.
61 62 63 64 |
# File 'lib/cardiac/model/attributes.rb', line 61 def id delim, values = id_delimiter, to_key (delim && values) ? values.join(delim) : values end |
#remote_attributes ⇒ Object
Retrieves the most recently unpacked/decoded remote attributes.
44 45 46 |
# File 'lib/cardiac/model/attributes.rb', line 44 def remote_attributes @remote_attributes.with_indifferent_access end |
#to_key ⇒ Object
Overridden to use this model’s key_attributes to build the key. Returns an array of the key values, if they are all present, otherwise, returns nil.
50 51 52 53 54 55 |
# File 'lib/cardiac/model/attributes.rb', line 50 def to_key keys = key_attributes.presence and keys.map do |key| return unless query_attribute(key) read_attribute(key) end end |
#typecast_attribute(typecaster, value) ⇒ Object
Overridden to convert empty strings to nil before typecasting.
67 68 69 70 |
# File 'lib/cardiac/model/attributes.rb', line 67 def typecast_attribute(typecaster, value) value = nil if value.acts_like?(:string) && value.empty? super(typecaster, value) end |