Module: Ridley::Resource::ClassMethods

Defined in:
lib/ridley/resource.rb

Instance Method Summary collapse

Instance Method Details

#all(connection) ⇒ Array<Object>

Parameters:

Returns:

  • (Array<Object>)


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

def all(connection)
  connection.get(self.resource_path).body.collect do |identity, location|
    new(connection, self.chef_id => identity)
  end
end

#attribute(name, options = {}) ⇒ Set

Parameters:

  • name (String, Symbol)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :default (Object)

    defines the default value for the attribute

Returns:

  • (Set)


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

def attribute(name, options = {})
  if options.has_key?(:default)
    default_for_attribute(name, options[:default])
  end
  define_attribute_method(name)
  attributes << name.to_sym
end

#attribute_defaultsHash

Returns:

  • (Hash)


70
71
72
# File 'lib/ridley/resource.rb', line 70

def attribute_defaults
  @attribute_defaults ||= Hash.new
end

#attributesSet

Returns:

  • (Set)


65
66
67
# File 'lib/ridley/resource.rb', line 65

def attributes
  @attributes ||= Set.new
end

#chef_idString?

Returns:

  • (String, nil)


15
16
17
# File 'lib/ridley/resource.rb', line 15

def chef_id
  @chef_id
end

#chef_json_classString?

Returns:

  • (String, nil)


52
53
54
# File 'lib/ridley/resource.rb', line 52

def chef_json_class
  @chef_json_class
end

#chef_typeString

Returns:

  • (String)


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

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

#create(connection, object) ⇒ Object

Parameters:

Returns:

  • (Object)


122
123
124
125
126
127
# File 'lib/ridley/resource.rb', line 122

def create(connection, object)
  resource = new(connection, object.to_hash)
  new_attributes = connection.post(self.resource_path, resource.to_json).body
  resource.attributes = resource.attributes.merge(new_attributes)
  resource
end

#delete(connection, object) ⇒ Object

Parameters:

Returns:

  • (Object)


133
134
135
136
# File 'lib/ridley/resource.rb', line 133

def delete(connection, object)
  chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
  new(connection, connection.delete("#{self.resource_path}/#{chef_id}").body)
end

#delete_all(connection) ⇒ Array<Object>

Parameters:

Returns:

  • (Array<Object>)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ridley/resource.rb', line 141

def delete_all(connection)
  mutex = Mutex.new
  deleted = []
  resources = all(connection)

  connection.thread_count.times.collect do
    Thread.new(connection, resources, deleted) do |connection, resources, deleted|
      while resource = mutex.synchronize { resources.pop }
        result = delete(connection, resource)
        mutex.synchronize { deleted << result }
      end
    end
  end.each(&:join)

  deleted
end

#find(connection, object) ⇒ nil, Object

Parameters:

Returns:

  • (nil, Object)


100
101
102
103
104
# File 'lib/ridley/resource.rb', line 100

def find(connection, object)
  find!(connection, object)
rescue Errors::HTTPNotFound
  nil
end

#find!(connection, object) ⇒ Object

Parameters:

Returns:

  • (Object)

Raises:



113
114
115
116
# File 'lib/ridley/resource.rb', line 113

def find!(connection, object)
  chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
  new(connection, connection.get("#{self.resource_path}/#{chef_id}").body)
end

#resource_pathString

Returns:

  • (String)


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

def resource_path
  @resource_path ||= self.chef_type.pluralize
end

#set_chef_id(identifier) ⇒ String

Parameters:

  • identifier (String, Symbol)

Returns:

  • (String)


22
23
24
# File 'lib/ridley/resource.rb', line 22

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

#set_chef_json_class(klass) ⇒ String

Parameters:

  • klass (String, Symbol)

Returns:

  • (String)


59
60
61
62
# File 'lib/ridley/resource.rb', line 59

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

#set_chef_type(type) ⇒ String

Parameters:

  • type (String, Symbol)

Returns:

  • (String)


46
47
48
49
# File 'lib/ridley/resource.rb', line 46

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

#set_resource_path(path) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


34
35
36
# File 'lib/ridley/resource.rb', line 34

def set_resource_path(path)
  @resource_path = path
end

#update(connection, object) ⇒ Object

Parameters:

Returns:

  • (Object)


162
163
164
165
# File 'lib/ridley/resource.rb', line 162

def update(connection, object)
  resource = new(connection, object.to_hash)
  new(connection, connection.put("#{self.resource_path}/#{resource.chef_id}", resource.to_json).body)
end