Module: Ridley::Resource

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods, ActiveModel::Serializers::JSON, ActiveModel::Validations, Comparable
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

Parameters:

  • other (Object)

Returns:

  • (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: []

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)


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: []=

Parameters:

  • key (String, Symbol)
  • value (Object)

Returns:

  • (Object)


198
199
200
# File 'lib/ridley/resource.rb', line 198

def attribute=(key, value)
  instance_variable_set("@#{key}", value)
end

#attribute?(key) ⇒ Boolean

Parameters:

  • key (String, Symbol)

Returns:

  • (Boolean)


206
207
208
# File 'lib/ridley/resource.rb', line 206

def attribute?(key)
  attribute(key).present?
end

#attributesHash

Returns:

  • (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

Parameters:

Returns:

  • (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_idString

Returns:

  • (String)


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

def chef_id
  attribute(self.class.chef_id)
end

#eql?(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (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

Parameters:

Returns:

  • (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

Parameters:

  • json (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

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

Returns:

  • (Object)


266
267
268
269
# File 'lib/ridley/resource.rb', line 266

def from_json(json, options = {})
  self.attributes = MultiJson.decode(json, options)
  self
end

#hashObject



314
315
316
# File 'lib/ridley/resource.rb', line 314

def hash
  self.chef_id.hash
end

#initialize(connection, attributes = {}) ⇒ Object

Parameters:



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

#reloadObject

Reload the attributes of the instantiated resource

Returns:

  • (Object)


251
252
253
254
# File 'lib/ridley/resource.rb', line 251

def reload
  self.attributes = self.class.find(connection, self).attributes
  self
end

#saveBoolean

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

Returns:

  • (Boolean)

    true if successful and false for failure

Raises:



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_hashObject



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

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

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

Returns:

  • (String)


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

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

#to_sObject



292
293
294
# File 'lib/ridley/resource.rb', line 292

def to_s
  self.attributes
end