Module: Ridley::Resource
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Serializers::JSON, ActiveModel::Validations, Comparable
- Defined in:
- lib/ridley/resource.rb
Overview
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #<=>(other) ⇒ Boolean
- #==(other) ⇒ Object
- #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
- #hash ⇒ Object
- #initialize(connection, attributes = {}) ⇒ Object
-
#reload ⇒ Object
Reload the attributes of the instantiated resource.
-
#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
299 300 301 |
# File 'lib/ridley/resource.rb', line 299 def <=>(other) self.chef_id <=> other.chef_id end |
#==(other) ⇒ Object
303 304 305 |
# File 'lib/ridley/resource.rb', line 303 def ==(other) self.chef_id == other.chef_id end |
#attribute(key) ⇒ Object Also known as: []
185 186 187 188 189 190 191 |
# File 'lib/ridley/resource.rb', line 185 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: []=
198 199 200 |
# File 'lib/ridley/resource.rb', line 198 def attribute=(key, value) instance_variable_set("@#{key}", value) end |
#attribute?(key) ⇒ Boolean
206 207 208 |
# File 'lib/ridley/resource.rb', line 206 def attribute?(key) attribute(key).present? end |
#attributes ⇒ Hash
211 212 213 214 215 216 217 |
# File 'lib/ridley/resource.rb', line 211 def attributes {}.tap do |attrs| self.class.attributes.each do |attr| attrs[attr] = attribute(attr) end end end |
#attributes=(new_attributes) ⇒ Hash
222 223 224 225 226 227 228 |
# File 'lib/ridley/resource.rb', line 222 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
257 258 259 |
# File 'lib/ridley/resource.rb', line 257 def chef_id attribute(self.class.chef_id) end |
#eql?(other) ⇒ Boolean
310 311 312 |
# File 'lib/ridley/resource.rb', line 310 def eql?(other) self.class == other.class && self == other end |
#from_hash(hash) ⇒ Object
274 275 276 277 |
# File 'lib/ridley/resource.rb', line 274 def from_hash(hash) self.attributes = hash.to_hash self end |
#from_json(json, options = {}) ⇒ Object
266 267 268 269 |
# File 'lib/ridley/resource.rb', line 266 def from_json(json, = {}) self.attributes = MultiJson.decode(json, ) self end |
#hash ⇒ Object
314 315 316 |
# File 'lib/ridley/resource.rb', line 314 def hash self.chef_id.hash end |
#initialize(connection, attributes = {}) ⇒ Object
177 178 179 180 |
# File 'lib/ridley/resource.rb', line 177 def initialize(connection, attributes = {}) @connection = connection self.attributes = self.class.attribute_defaults.deep_merge(attributes) end |
#reload ⇒ Object
Reload the attributes of the instantiated resource
251 252 253 254 |
# File 'lib/ridley/resource.rb', line 251 def reload self.attributes = self.class.find(connection, self).attributes self end |
#save ⇒ Boolean
Creates a resource on the target remote or updates one if the resource already exists.
238 239 240 241 242 243 244 245 246 |
# File 'lib/ridley/resource.rb', line 238 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
288 289 290 |
# File 'lib/ridley/resource.rb', line 288 def to_hash self.attributes end |
#to_json(options = {}) ⇒ String Also known as: as_json
283 284 285 |
# File 'lib/ridley/resource.rb', line 283 def to_json( = {}) MultiJson.encode(self.attributes, ) end |
#to_s ⇒ Object
292 293 294 |
# File 'lib/ridley/resource.rb', line 292 def to_s self.attributes end |