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>)


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

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)


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

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)


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

def attribute_defaults
  @attribute_defaults ||= HashWithIndifferentAccess.new
end

#attributesSet

Returns:

  • (Set)


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

def attributes
  @attributes ||= Set.new
end

#chef_idString?

Returns:

  • (String, nil)


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

def chef_id
  @chef_id
end

#chef_json_classString?

Returns:

  • (String, nil)


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

def chef_json_class
  @chef_json_class
end

#chef_typeString

Returns:

  • (String)


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

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

#create(connection, object) ⇒ Object

Parameters:

Returns:

  • (Object)


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

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.deep_merge(new_attributes)
  resource
end

#delete(connection, object) ⇒ Object

Parameters:

Returns:

  • (Object)


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

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>)


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

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)


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

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

#find!(connection, object) ⇒ Object

Parameters:

Returns:

  • (Object)

Raises:



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

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)


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

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

#set_chef_id(identifier) ⇒ String

Parameters:

  • identifier (String, Symbol)

Returns:

  • (String)


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

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

#set_chef_json_class(klass) ⇒ String

Parameters:

  • klass (String, Symbol)

Returns:

  • (String)


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

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)


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

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)


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

def set_resource_path(path)
  @resource_path = path
end

#update(connection, object) ⇒ Object

Parameters:

Returns:

  • (Object)


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

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