class ChefAPI::Resource::DataBag

Public Class Methods

each(&block) click to toggle source
# File lib/chef-api/resources/data_bag.rb, line 59
def each(&block)
  collection.each do |name, path|
    result = new(name: name)
    block.call(result) if block
  end
end
fetch(id, prefix = {}) click to toggle source
# File lib/chef-api/resources/data_bag.rb, line 46
def fetch(id, prefix = {})
  return nil if id.nil?

  path     = resource_path(id, prefix)
  response = connection.get(path)
  new(name: id)
rescue Error::HTTPNotFound
  nil
end
from_file(path, name = File.basename(path)) click to toggle source

Load the data bag from a collection of JSON files on disk. Just like knife, the basename of the folder is assumed to be the name of the data bag and all containing items a proper JSON data bag.

This will load *all* items in the data bag, returning an array of those items. To load an individual data bag item, see {DataBagItem.from_file}.

**This method does NOT return an instance of a {DataBag}!**

@param [String] path

the path to the data bag **folder** on disk

@param [String] name

the name of the data bag

@return [Array<DataBagItem>]

# File lib/chef-api/resources/data_bag.rb, line 28
def from_file(path, name = File.basename(path))
  path  = File.expand_path(path)

  raise Error::FileNotFound.new(path: path)  unless File.exists?(path)
  raise Error::NotADirectory.new(path: path) unless File.directory?(path)

  raise ArgumentError unless File.directory?(path)

  bag = new(name: name)

  Util.fast_collect(Dir["#{path}/*.json"]) do |item|
    DataBagItem.from_file(item, bag)
  end
end

Public Instance Methods

items() click to toggle source

This is the same as +has_many :items+, but creates a special collection for data bag items, which is mutable and handles some special edge cases that only data bags encounter.

@see Base.has_many

# File lib/chef-api/resources/data_bag.rb, line 74
def items
  associations[:items] ||= Resource::DataBagItemCollectionProxy.new(self)
end