Module: StorageRoom::Accessors

Extended by:
ActiveSupport::Concern
Included in:
Embedded, Resource
Defined in:
lib/storage_room/accessors.rb

Overview

Module that contains attributes methods shared between StorageRoom::Resource and StorageRoom::Embedded

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Instance Method Details

- (Object) [](name)

Shortcut to get an attribute.



102
103
104
# File 'lib/storage_room/accessors.rb', line 102

def [](name)
  self.response_data[name]
end

- (Object) as_json(args = {})

:nodoc:



132
133
134
# File 'lib/storage_room/accessors.rb', line 132

def as_json(args = {}) # :nodoc:
  to_hash(args)
end

- (Object) attributes

The attributes as they were defined with key, one, many



124
125
126
# File 'lib/storage_room/accessors.rb', line 124

def attributes
  @_attributes ||= Hash.new.with_indifferent_access
end

- (Object) attributes=(args = {})



128
129
130
# File 'lib/storage_room/accessors.rb', line 128

def attributes=(args = {})
  attributes.merge!(args)
end

- (Boolean) eql?(object) Also known as: ==

Compare Resources by comparing their attributes



178
179
180
# File 'lib/storage_room/accessors.rb', line 178

def eql?(object)
  self.class.equal?(object.class) && attributes == object.attributes
end

- (Object) hash



184
185
186
# File 'lib/storage_room/accessors.rb', line 184

def hash
  self.class.hash ^ self.attributes.hash
end

- (Object) initialize(hash = {})

Optionally pass attributes to set up the object



97
98
99
# File 'lib/storage_room/accessors.rb', line 97

def initialize(hash={})
  self.attributes = hash
end

- (Object) inspect

:nodoc:



156
157
158
159
# File 'lib/storage_room/accessors.rb', line 156

def inspect # :nodoc:
  body = attributes.map{|k, v| "#{k}: #{attribute_for_inspect(v)}"}.join(', ')
  "#<#{self.class} #{body}>"
end

- (Boolean) loaded?

Has a Resource been loaded from the API



169
170
171
# File 'lib/storage_room/accessors.rb', line 169

def loaded?
  self.response_data.present?
end

- (Boolean) proxy?

:nodoc:



173
174
175
# File 'lib/storage_room/accessors.rb', line 173

def proxy? # :nodoc:
  false
end

- (Object) reset!

Reset an object to its initial state with all attributes unset



162
163
164
165
166
# File 'lib/storage_room/accessors.rb', line 162

def reset!
  @_response_data = Hash.new.with_indifferent_access
  @_attributes = Hash.new.with_indifferent_access
  true
end

- (Object) response_data

Return all of the objects attributes



114
115
116
# File 'lib/storage_room/accessors.rb', line 114

def response_data
  @_response_data ||= Hash.new.with_indifferent_access
end

- (Object) response_data=(hash = {})

Set the objects attributes with a hash. Only attributes passed in the hash are changed, existing ones are not overridden.



119
120
121
# File 'lib/storage_room/accessors.rb', line 119

def response_data=(hash = {})
  response_data.merge!(hash)
end

- (Object) set_from_response_data(hash)

Takes a response data hash, saves it in the record and initializes the class from the response data



107
108
109
110
111
# File 'lib/storage_room/accessors.rb', line 107

def set_from_response_data(hash)
  self.response_data = hash
  self.initialize_from_response_data
  self
end

- (Object) to_hash(args = {})

ActiveSupport seemed to cause problems when just using as_json, so using to_hash



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/storage_room/accessors.rb', line 137

def to_hash(args = {}) # :nodoc:
  args ||= {}
  hash = {}

  self.attributes.each do |name, value|
    hash[name] = if value.is_a?(::Array)
      value.map {|x| x.respond_to?(:to_hash) ? call_method_with_optional_parameters(x, :to_hash, :nested => true) : x}
    elsif value.respond_to?(:to_hash)
      call_method_with_optional_parameters(value, :to_hash, :nested => true)
    elsif value.respond_to?(:as_json)
      value.as_json(:nested => true)
    else
      value
    end
  end

  hash
end