Module: ApiResource::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods, ActiveModel::Dirty
Included in:
Base
Defined in:
lib/api_resource/attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/api_resource/attributes.rb', line 205

def attribute?(name)
  self.class.attribute?(name)
end

#attributesObject



143
144
145
146
147
# File 'lib/api_resource/attributes.rb', line 143

def attributes
  attrs = {}
  self.attribute_names.each{|name| attrs[name] = read_attribute(name)}
  attrs
end

#attributes=(new_attrs) ⇒ Object

set new attributes



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/api_resource/attributes.rb', line 150

def attributes=(new_attrs)
  new_attrs.each_pair do |k,v|
    if self.protected_attribute?(k)
      raise Exception.new(
        "#{k} is a protected attribute and cannot be mass-assigned"
      )
    end
    self.send("#{k}=",v) unless k.to_sym == :id
  end
  new_attrs
end

#initialize(*args) ⇒ Object

override the initializer to set up some default values



139
140
141
# File 'lib/api_resource/attributes.rb', line 139

def initialize(*args)
  @attributes = @attributes_cache = HashWithIndifferentAccess.new
end

#protected_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/api_resource/attributes.rb', line 209

def protected_attribute?(name)
  self.class.protected_attribute?(name)
end

#read_attribute(name) ⇒ Object



188
189
190
# File 'lib/api_resource/attributes.rb', line 188

def read_attribute(name)
  self.typecasted_attribute(name.to_sym)
end

#reset_attribute_changes(*attrs) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/api_resource/attributes.rb', line 179

def reset_attribute_changes(*attrs)
  attrs = self.class.public_attribute_names if attrs.blank?
  attrs.each do |attr|
    self.send("reset_#{attr}!")
  end
  
  set_attributes_as_current(*attrs)
end

#respond_to?(sym, include_private_methods = false) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
221
222
# File 'lib/api_resource/attributes.rb', line 213

def respond_to?(sym, include_private_methods = false)
  if sym =~ /\?$/
    return true if self.attribute?($`)
  elsif sym =~ /=$/
    return true if self.class.public_attribute_names.include?($`)
  else
    return true if self.attribute?(sym.to_sym)
  end
  super
end

#save_with_dirty_tracking(*args) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/api_resource/attributes.rb', line 162

def save_with_dirty_tracking(*args)
  if save_without_dirty_tracking(*args)
    @previously_changed = self.changes
    @changed_attributes.clear
    return true
  else
    return false
  end
end

#set_attributes_as_current(*attrs) ⇒ Object



172
173
174
175
176
177
# File 'lib/api_resource/attributes.rb', line 172

def set_attributes_as_current(*attrs)
  @changed_attributes.clear and return if attrs.blank?
  attrs.each do |attr|
    @changed_attributes.delete(attr.to_s)
  end
end

#write_attribute(name, val) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/api_resource/attributes.rb', line 192

def write_attribute(name, val)
  old_val = read_attribute(name)
  new_val = val.nil? ? nil : self.typecast_attribute(name, val)

  unless old_val == new_val
    self.send("#{name}_will_change!")
  end
  # delete the old cached value and assign new val to both
  # @attributes and @attributes_cache
  @attributes_cache.delete(name.to_sym)
  @attributes[name.to_sym] = @attributes_cache[name.to_sym] = new_val
end