Module: StorageRoom::Accessors
- Extended by:
- ActiveSupport::Concern
- 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
-
#[](name) ⇒ Object
Shortcut to get an attribute.
-
#as_json(args = {}) ⇒ Object
:nodoc:.
-
#attributes ⇒ Object
The attributes as they were defined with key, one, many.
- #attributes=(args = {}) ⇒ Object
-
#eql?(object) ⇒ Boolean
(also: #==)
Compare Resources by comparing their attributes.
- #hash ⇒ Object
-
#initialize(hash = {}) ⇒ Object
Optionally pass attributes to set up the object.
-
#inspect ⇒ Object
:nodoc:.
-
#loaded? ⇒ Boolean
Has a Resource been loaded from the API.
-
#proxy? ⇒ Boolean
:nodoc:.
-
#reset! ⇒ Object
Reset an object to its initial state with all attributes unset.
-
#response_data ⇒ Object
Return all of the objects attributes.
-
#response_data=(hash = {}) ⇒ Object
Set the objects attributes with a hash.
-
#set_from_response_data(hash) ⇒ Object
Takes a response data hash, saves it in the record and initializes the class from the response data.
-
#to_hash(args = {}) ⇒ Object
ActiveSupport seemed to cause problems when just using as_json, so using to_hash.
Instance Method Details
#[](name) ⇒ Object
Shortcut to get an attribute.
102 103 104 |
# File 'lib/storage_room/accessors.rb', line 102 def [](name) self.response_data[name] end |
#as_json(args = {}) ⇒ Object
:nodoc:
132 133 134 |
# File 'lib/storage_room/accessors.rb', line 132 def as_json(args = {}) # :nodoc: to_hash(args) end |
#attributes ⇒ Object
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 |
#attributes=(args = {}) ⇒ Object
128 129 130 |
# File 'lib/storage_room/accessors.rb', line 128 def attributes=(args = {}) attributes.merge!(args) end |
#eql?(object) ⇒ Boolean 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 |
#hash ⇒ Object
184 185 186 |
# File 'lib/storage_room/accessors.rb', line 184 def hash self.class.hash ^ self.attributes.hash end |
#initialize(hash = {}) ⇒ Object
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 |
#inspect ⇒ Object
: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 |
#loaded? ⇒ Boolean
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 |
#proxy? ⇒ Boolean
:nodoc:
173 174 175 |
# File 'lib/storage_room/accessors.rb', line 173 def proxy? # :nodoc: false end |
#reset! ⇒ Object
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 |
#response_data ⇒ Object
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 |
#response_data=(hash = {}) ⇒ Object
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 |
#set_from_response_data(hash) ⇒ Object
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 |
#to_hash(args = {}) ⇒ Object
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 |