Class: Ridley::ChefObject

Inherits:
Object
  • Object
show all
Includes:
Comparable, VariaModel
Defined in:
lib/ridley/chef_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, new_attrs = {}) ⇒ ChefObject

Returns a new instance of ChefObject.

Parameters:



50
51
52
53
# File 'lib/ridley/chef_object.rb', line 50

def initialize(resource, new_attrs = {})
  @resource = resource
  mass_assign(new_attrs)
end

Class Method Details

.chef_idString?

Returns:

  • (String, nil)


7
8
9
# File 'lib/ridley/chef_object.rb', line 7

def chef_id
  @chef_id
end

.chef_json_classString?

Returns:

  • (String, nil)


32
33
34
# File 'lib/ridley/chef_object.rb', line 32

def chef_json_class
  @chef_json_class
end

.chef_typeString

Returns:

  • (String)


19
20
21
# File 'lib/ridley/chef_object.rb', line 19

def chef_type
  @chef_type ||= self.class.name.underscore
end

.set_chef_id(identifier) ⇒ String

Parameters:

  • identifier (#to_sym)

Returns:

  • (String)


14
15
16
# File 'lib/ridley/chef_object.rb', line 14

def set_chef_id(identifier)
  @chef_id = identifier.to_sym
end

.set_chef_json_class(klass) ⇒ String

Parameters:

  • klass (String, Symbol)

Returns:

  • (String)


39
40
41
42
# File 'lib/ridley/chef_object.rb', line 39

def set_chef_json_class(klass)
  @chef_json_class = klass
  attribute(:json_class, default: klass)
end

.set_chef_type(type) ⇒ String

Parameters:

  • type (#to_s)

Returns:

  • (String)


26
27
28
29
# File 'lib/ridley/chef_object.rb', line 26

def set_chef_type(type)
  @chef_type = type.to_s
  attribute(:chef_type, default: type)
end

Instance Method Details

#<=>(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


108
109
110
# File 'lib/ridley/chef_object.rb', line 108

def <=>(other)
  self.chef_id <=> other.chef_id
end

#==(other) ⇒ Object



112
113
114
# File 'lib/ridley/chef_object.rb', line 112

def ==(other)
  self.chef_id == other.chef_id
end

#chef_idString

Returns:

  • (String)


97
98
99
# File 'lib/ridley/chef_object.rb', line 97

def chef_id
  get_attribute(self.class.chef_id)
end

#eql?(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


119
120
121
# File 'lib/ridley/chef_object.rb', line 119

def eql?(other)
  self.class == other.class && self == other
end

#hashObject



123
124
125
# File 'lib/ridley/chef_object.rb', line 123

def hash
  self.chef_id.hash
end

#inspectObject



101
102
103
# File 'lib/ridley/chef_object.rb', line 101

def inspect
  "#<#{self.class} chef_id:#{self.chef_id}, attributes:#{self._attributes_}>"
end

#reloadObject

Reload the attributes of the instantiated resource

Returns:

  • (Object)


89
90
91
92
93
94
# File 'lib/ridley/chef_object.rb', line 89

def reload
  new_attributes = resource.find(self)._attributes_
  @_attributes_  = nil
  mass_assign(new_attributes)
  self
end

#saveBoolean

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

Returns:

  • (Boolean)

Raises:



62
63
64
65
66
67
68
69
70
# File 'lib/ridley/chef_object.rb', line 62

def save
  raise Errors::InvalidResource.new(self.errors) unless valid?

  mass_assign(resource.create(self)._attributes_)
  true
rescue Errors::HTTPConflict
  self.update
  true
end

#updateBoolean

Updates the instantiated resource on the target remote with any changes made to self

Returns:

  • (Boolean)

Raises:



79
80
81
82
83
84
# File 'lib/ridley/chef_object.rb', line 79

def update
  raise Errors::InvalidResource.new(self.errors) unless valid?

  mass_assign(resource.update(self)._attributes_)
  true
end