Module: Tire::Model::Persistence::Attributes::InstanceMethods

Defined in:
lib/tire/model/persistence/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



82
83
84
# File 'lib/tire/model/persistence/attributes.rb', line 82

def id
  @id
end

Instance Method Details

#__cast_value(name, value) ⇒ Object

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.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tire/model/persistence/attributes.rb', line 117

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 =~ /^\d{4}[\/\-]\d{2}[\/\-]\d{2}T\d{2}\:\d{2}\:\d{2}Z$/
      value
  end
end

#__update_attributes(attributes) ⇒ Object



109
110
111
# File 'lib/tire/model/persistence/attributes.rb', line 109

def __update_attributes(attributes)
  attributes.each { |name, value| send "#{name}=", __cast_value(name, value) }
end

#attribute_namesObject



100
101
102
# File 'lib/tire/model/persistence/attributes.rb', line 100

def attribute_names
  self.class.properties.sort
end

#attributesObject



95
96
97
98
# File 'lib/tire/model/persistence/attributes.rb', line 95

def attributes
  self.class.properties.
    inject( self.id ? {'id' => self.id} : {} ) {|attributes, key| attributes[key] = send(key); attributes}
end

#has_attribute?(name) ⇒ Boolean Also known as: has_property?

Returns:

  • (Boolean)


104
105
106
# File 'lib/tire/model/persistence/attributes.rb', line 104

def has_attribute?(name)
  properties.include?(name.to_s)
end

#initialize(attributes = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/tire/model/persistence/attributes.rb', line 84

def initialize(attributes={})
  # Make a copy of objects in the property defaults hash, so default values such as `[]` or `{ foo: [] }` are left intact
  property_defaults = self.class.property_defaults.inject({}) do |hash, item|
    key, value = item
    hash[key.to_s] = value.class.respond_to?(:new) ? value.clone : value
    hash
  end

  __update_attributes(property_defaults.merge(attributes))
end