class ChefAPI::Resource::DataBagItem

Attributes

bag[R]

Public Class Methods

from_file(path, bag = File.basename(File.dirname(path))) click to toggle source
# 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
new(attributes = {}, prefix = {}, bag = nil) click to toggle source

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

Calls superclass method ChefAPI::Resource::Base::new
# 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

Public Instance Methods

to_hash() click to toggle source

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

@see (Resource::Base#to_hash)

# File lib/chef-api/resources/data_bag_item.rb, line 40
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