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 84
def initialize(attributes={})
  __update_attributes(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 110
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) if value.is_a?(String) && value =~ %r^\d{4}[\/\-]\d{2}[\/\-]\d{2}T\d{2}\:\d{2}\:\d{2}Z$/
      value
  end
end
__update_attributes(attributes) click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 102
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 93
def attribute_names
  self.class.properties.sort
end
attributes() click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 88
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 97
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?