Class: ChefAPI::Resource::DataBagItem

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-api/resources/data_bag_item.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#associations

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_attributes, #_prefix, all, #attribute?, build, classname, collection, collection_path, connection, count, create, delete, #destroy, destroy, destroy_all, #diff, #dirty?, each, #errors, exists?, expanded_collection_path, fetch, from_json, from_url, has_many, #id, #ignore_attribute?, inspect, #inspect, list, #new_resource?, post, #primary_key, protect, #protected?, protected_resources, put, #reload!, #resource_path, resource_path, #save, #save!, schema, #to_json, to_s, #to_s, type, #update, update, #update_attribute, #valid?, #validate!, #validators

Constructor Details

#initialize(attributes = {}, prefix = {}, bag = nil) ⇒ DataBagItem

Override the initialize method to move any attributes into the data hash.



28
29
30
31
32
33
# File 'lib/chef-api/resources/data_bag_item.rb', line 28

def initialize(attributes = {}, prefix = {}, bag = nil)
  @bag = bag || Resource::DataBag.fetch(prefix[:bag])

  id = attributes.delete(:id) || attributes.delete('id')
  super({ id: id, data: attributes }, prefix)
end

Instance Attribute Details

#bagObject (readonly)

Returns the value of attribute bag.



22
23
24
# File 'lib/chef-api/resources/data_bag_item.rb', line 22

def bag
  @bag
end

Class Method Details

.from_file(path, bag = File.basename(File.dirname(path))) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/chef-api/resources/data_bag_item.rb', line 11

def from_file(path, bag = File.basename(File.dirname(path)))
  id, contents = Util.safe_read(path)
  data = JSON.parse(contents)
  data[:id] = id

  bag = bag.is_a?(Resource::DataBag) ? bag : Resource::DataBag.new(name: bag)

  new(data, { bag: bag.name }, bag)
end

Instance Method Details

#to_hashObject

Override the to_hash method to move data to the upper scope.

See Also:

  • ChefAPI::Resource::DataBagItem.(Resource(Resource::Base(Resource::Base#to_hash)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chef-api/resources/data_bag_item.rb', line 41

def to_hash
  {}.tap do |hash|
    _attributes.each do |key, value|
      if key == :data
        hash.merge!(value)
      else
        hash[key] = value.respond_to?(:to_hash) ? value.to_hash : value
      end
    end
  end
end