module Tire::Model::Persistence::Attributes::InstanceMethods

Attributes

id[RW]

Public Class Methods

new(attributes={}) click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 89
def initialize(attributes={})
  # Make a copy of objects in the property defaults hash, so default values
  # such as `[]` or `{ foo: [] }` are preserved.
  #
  property_defaults = self.class.property_defaults.inject({}) do |hash, item|
    key, value = item

    hash[key.to_sym] = if value.respond_to?(:call)
      value.call
    elsif value.class.respond_to?(:new)
      value.clone
    else
      value
    end

    hash
  end
  __update_attributes(property_defaults.merge(attributes))
end

Public Instance Methods

__cast_value(name, value) click to toggle source

Casts the values according to the :class option set when defining the property, cast Hashes as Hashr instances and automatically convert UTC formatted strings to Time.

# File lib/tire/model/persistence/attributes.rb, line 131
def __cast_value(name, value)
  case

    when klass = self.class.property_types[name.to_sym]
      if klass.is_a?(Array) && value.is_a?(Array)
        value.map { |v| klass.first.new(v) }
      else
        klass.new(value)
      end

    when value.is_a?(Hash)
      Hashr.new(value)

    else
      # Strings formatted as <http://en.wikipedia.org/wiki/ISO8601> are automatically converted to Time
      value = Time.parse(value).utc if value.is_a?(String) && value =~ %r^\d{4}[\/\-]\d{2}[\/\-]\d{2}T\d{2}\:\d{2}\:\d{2}/
      value
  end
end
__update_attributes(attributes) click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 123
def __update_attributes(attributes)
  attributes.each { |name, value| send "#{name}=", __cast_value(name, value) }
end
attribute_names() click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 114
def attribute_names
  self.class.properties.sort
end
attributes() click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 109
def attributes
  self.class.properties.
    inject( self.id ? {'id' => self.id} : {} ) {|attributes, key| attributes[key] = send(key); attributes}
end
has_attribute?(name) click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 118
def has_attribute?(name)
  properties.include?(name.to_s)
end
Also aliased as: has_property?
has_property?(name) click to toggle source
Alias for: has_attribute?