Module: Ridley::Resource
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Serializers::JSON, ActiveModel::Validations
- Defined in:
- lib/ridley/resource.rb
Overview
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #attribute(key) ⇒ Object (also: #[])
- #attribute=(key, value) ⇒ Object (also: #[]=)
- #attribute?(key) ⇒ Boolean
- #attributes ⇒ Hash
- #attributes=(new_attributes) ⇒ Hash
- #chef_id ⇒ String
- #eql?(other) ⇒ Boolean
- #from_hash(hash) ⇒ Object
- #from_json(json, options = {}) ⇒ Object
- #initialize(connection, attributes = {}) ⇒ Object
-
#save ⇒ Boolean
Creates a resource on the target remote or updates one if the resource already exists.
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ String (also: #as_json)
- #to_s ⇒ Object
Instance Method Details
#==(other) ⇒ Boolean
290 291 292 |
# File 'lib/ridley/resource.rb', line 290 def ==(other) self.attributes == other.attributes end |
#attribute(key) ⇒ Object Also known as: []
184 185 186 187 188 189 190 |
# File 'lib/ridley/resource.rb', line 184 def attribute(key) if instance_variable_defined?("@#{key}") instance_variable_get("@#{key}") else self.class.attribute_defaults[key] end end |
#attribute=(key, value) ⇒ Object Also known as: []=
197 198 199 |
# File 'lib/ridley/resource.rb', line 197 def attribute=(key, value) instance_variable_set("@#{key}", value) end |
#attribute?(key) ⇒ Boolean
205 206 207 |
# File 'lib/ridley/resource.rb', line 205 def attribute?(key) attribute(key).present? end |
#attributes ⇒ Hash
210 211 212 213 214 215 216 |
# File 'lib/ridley/resource.rb', line 210 def attributes {}.tap do |attrs| self.class.attributes.each do |attr| attrs[attr] = attribute(attr) end end end |
#attributes=(new_attributes) ⇒ Hash
221 222 223 224 225 226 227 |
# File 'lib/ridley/resource.rb', line 221 def attributes=(new_attributes) new_attributes.to_hash.symbolize_keys! self.class.attributes.each do |attr_name| send(:attribute=, attr_name, new_attributes[attr_name.to_sym]) end end |
#chef_id ⇒ String
248 249 250 |
# File 'lib/ridley/resource.rb', line 248 def chef_id attribute(self.class.chef_id) end |
#eql?(other) ⇒ Boolean
297 298 299 |
# File 'lib/ridley/resource.rb', line 297 def eql?(other) other.is_a?(self.class) && send(:==, other) end |
#from_hash(hash) ⇒ Object
265 266 267 268 |
# File 'lib/ridley/resource.rb', line 265 def from_hash(hash) self.attributes = hash.to_hash self end |
#from_json(json, options = {}) ⇒ Object
257 258 259 260 |
# File 'lib/ridley/resource.rb', line 257 def from_json(json, = {}) self.attributes = MultiJson.decode(json, ) self end |
#initialize(connection, attributes = {}) ⇒ Object
176 177 178 179 |
# File 'lib/ridley/resource.rb', line 176 def initialize(connection, attributes = {}) @connection = connection self.attributes = self.class.attribute_defaults.merge(attributes) end |
#save ⇒ Boolean
Creates a resource on the target remote or updates one if the resource already exists.
237 238 239 240 241 242 243 244 245 |
# File 'lib/ridley/resource.rb', line 237 def save raise Errors::InvalidResource.new(self.errors) unless valid? self.attributes = self.class.create(connection, self).attributes true rescue Errors::HTTPConflict self.attributes = self.class.update(connection, self).attributes true end |
#to_hash ⇒ Object
279 280 281 |
# File 'lib/ridley/resource.rb', line 279 def to_hash self.attributes end |
#to_json(options = {}) ⇒ String Also known as: as_json
274 275 276 |
# File 'lib/ridley/resource.rb', line 274 def to_json( = {}) MultiJson.encode(self.attributes, ) end |
#to_s ⇒ Object
283 284 285 |
# File 'lib/ridley/resource.rb', line 283 def to_s self.attributes end |