Module: Ridley::Resource

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods, ActiveModel::Serializers::JSON, ActiveModel::Validations
Included in:
Client, Cookbook, DataBag, Environment, Node, Role
Defined in:
lib/ridley/resource.rb

Overview

Author:

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

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

#attributesHash



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_idString



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

Options Hash (options):

  • :symbolize_keys (Boolean)
  • :adapter (Class, Symbol, String)


257
258
259
260
# File 'lib/ridley/resource.rb', line 257

def from_json(json, options = {})
  self.attributes = MultiJson.decode(json, options)
  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

#saveBoolean

Creates a resource on the target remote or updates one if the resource already exists.

Raises:



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_hashObject



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

Options Hash (options):

  • :symbolize_keys (Boolean)
  • :adapter (Class, Symbol, String)


274
275
276
# File 'lib/ridley/resource.rb', line 274

def to_json(options = {})
  MultiJson.encode(self.attributes, options)
end

#to_sObject



283
284
285
# File 'lib/ridley/resource.rb', line 283

def to_s
  self.attributes
end