Class: Ridley::DataBagItem
- Inherits:
-
Object
- Object
- Ridley::DataBagItem
- Includes:
- ActiveModel::Serialization, ActiveModel::Validations
- Defined in:
- lib/ridley/resources/data_bag_item.rb
Overview
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#data_bag ⇒ Object
readonly
Returns the value of attribute data_bag.
Class Method Summary collapse
- .all(connection, data_bag) ⇒ Array<Object>
- .create(connection, data_bag, object) ⇒ Ridley::DataBagItem
- .delete(connection, data_bag, object) ⇒ Ridley::DataBagItem
- .delete_all(connection, data_bag) ⇒ Array<Ridley::DataBagItem>
- .find(connection, data_bag, object) ⇒ nil, Ridley::DataBagItem
- .find!(connection, data_bag, object) ⇒ Ridley::DataBagItem
- .update(connection, data_bag, object) ⇒ Ridley::DataBagItem
Instance Method Summary collapse
- #attribute(key) ⇒ Object (also: #[])
- #attribute=(key, value) ⇒ Object (also: #[]=)
-
#chef_id ⇒ String
(also: #id)
Alias for accessing the value of the ‘id’ attribute.
- #from_hash(hash) ⇒ Object
-
#initialize(connection, data_bag, attributes = {}) ⇒ DataBagItem
constructor
A new instance of DataBagItem.
-
#save ⇒ Boolean
Creates a resource on the target remote or updates one if the resource already exists.
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ String (also: #as_json)
- #to_s ⇒ Object
Constructor Details
#initialize(connection, data_bag, attributes = {}) ⇒ DataBagItem
106 107 108 109 110 |
# File 'lib/ridley/resources/data_bag_item.rb', line 106 def initialize(connection, data_bag, attributes = {}) @connection = connection @data_bag = data_bag @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
103 104 105 |
# File 'lib/ridley/resources/data_bag_item.rb', line 103 def attributes @attributes end |
#data_bag ⇒ Object (readonly)
Returns the value of attribute data_bag.
101 102 103 |
# File 'lib/ridley/resources/data_bag_item.rb', line 101 def data_bag @data_bag end |
Class Method Details
.all(connection, data_bag) ⇒ Array<Object>
11 12 13 14 15 |
# File 'lib/ridley/resources/data_bag_item.rb', line 11 def all(connection, data_bag) connection.get("#{data_bag.class.resource_path}/#{data_bag.name}").body.collect do |id, location| new(connection, data_bag, id: id) end end |
.create(connection, data_bag, object) ⇒ Ridley::DataBagItem
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ridley/resources/data_bag_item.rb', line 46 def create(connection, data_bag, object) resource = new(connection, data_bag, object.to_hash) unless resource.valid? raise Errors::InvalidResource.new(resource.errors) end new_attributes = connection.post("#{data_bag.class.resource_path}/#{data_bag.name}", resource.to_json).body resource.from_hash(resource.attributes.merge(new_attributes)) resource end |
.delete(connection, data_bag, object) ⇒ Ridley::DataBagItem
62 63 64 65 |
# File 'lib/ridley/resources/data_bag_item.rb', line 62 def delete(connection, data_bag, object) chef_id = object.respond_to?(:chef_id) ? object.chef_id : object new(connection, data_bag).from_hash(connection.delete("#{data_bag.class.resource_path}/#{data_bag.name}/#{chef_id}").body) end |
.delete_all(connection, data_bag) ⇒ Array<Ridley::DataBagItem>
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ridley/resources/data_bag_item.rb', line 71 def delete_all(connection, data_bag) mutex = Mutex.new deleted = [] resources = all(connection, data_bag) connection.thread_count.times.collect do Thread.new(connection, data_bag, resources, deleted) do |connection, data_bag, resources, deleted| while resource = mutex.synchronize { resources.pop } result = delete(connection, data_bag, resource) mutex.synchronize { deleted << result } end end end.each(&:join) deleted end |
.find(connection, data_bag, object) ⇒ nil, Ridley::DataBagItem
22 23 24 25 26 |
# File 'lib/ridley/resources/data_bag_item.rb', line 22 def find(connection, data_bag, object) find!(connection, data_bag, object) rescue Errors::HTTPNotFound nil end |
.find!(connection, data_bag, object) ⇒ Ridley::DataBagItem
36 37 38 39 |
# File 'lib/ridley/resources/data_bag_item.rb', line 36 def find!(connection, data_bag, object) chef_id = object.respond_to?(:chef_id) ? object.chef_id : object new(connection, data_bag).from_hash(connection.get("#{data_bag.class.resource_path}/#{data_bag.name}/#{chef_id}").body) end |
.update(connection, data_bag, object) ⇒ Ridley::DataBagItem
93 94 95 96 97 98 |
# File 'lib/ridley/resources/data_bag_item.rb', line 93 def update(connection, data_bag, object) resource = new(connection, data_bag, object.to_hash) new(connection, data_bag).from_hash( connection.put("#{data_bag.class.resource_path}/#{data_bag.name}/#{resource.chef_id}", resource.to_json).body ) end |
Instance Method Details
#attribute(key) ⇒ Object Also known as: []
123 124 125 |
# File 'lib/ridley/resources/data_bag_item.rb', line 123 def attribute(key) @attributes[key] end |
#attribute=(key, value) ⇒ Object Also known as: []=
132 133 134 |
# File 'lib/ridley/resources/data_bag_item.rb', line 132 def attribute=(key, value) @attributes[key] = value end |
#chef_id ⇒ String Also known as: id
Alias for accessing the value of the ‘id’ attribute
115 116 117 |
# File 'lib/ridley/resources/data_bag_item.rb', line 115 def chef_id @attributes[:id] end |
#from_hash(hash) ⇒ Object
158 159 160 161 162 163 |
# File 'lib/ridley/resources/data_bag_item.rb', line 158 def from_hash(hash) hash = hash.to_hash self.attributes = hash.has_key?(:raw_data) ? hash[:raw_data] : hash self end |
#save ⇒ Boolean
Creates a resource on the target remote or updates one if the resource already exists.
145 146 147 148 149 150 151 152 153 |
# File 'lib/ridley/resources/data_bag_item.rb', line 145 def save raise Errors::InvalidResource.new(self.errors) unless valid? self.attributes = self.class.create(connection, data_bag, self).attributes true rescue Errors::HTTPConflict self.attributes = self.class.update(connection, data_bag, self).attributes true end |
#to_hash ⇒ Object
174 175 176 |
# File 'lib/ridley/resources/data_bag_item.rb', line 174 def to_hash self.attributes end |
#to_json(options = {}) ⇒ String Also known as: as_json
169 170 171 |
# File 'lib/ridley/resources/data_bag_item.rb', line 169 def to_json( = {}) MultiJson.encode(self.attributes, ) end |
#to_s ⇒ Object
178 179 180 |
# File 'lib/ridley/resources/data_bag_item.rb', line 178 def to_s self.attributes end |