Class: ChefAPI::Resource::DataBag

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

Instance Attribute Summary

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?, #errors, exists?, expanded_collection_path, from_json, from_url, has_many, #id, #ignore_attribute?, #initialize, inspect, #inspect, list, #new_resource?, post, #primary_key, protect, #protected?, protected_resources, put, #reload!, resource_path, #resource_path, #save, #save!, schema, #to_hash, #to_json, to_s, #to_s, type, #update, update, #update_attribute, #valid?, #validate!, #validators

Constructor Details

This class inherits a constructor from ChefAPI::Resource::Base

Class Method Details

.each(&block) ⇒ Object



59
60
61
62
63
64
# 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 = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
# 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)) ⇒ Array<DataBagItem>

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 ChefAPI::Resource::DataBagItem.from_file.

**This method does NOT return an instance of a ChefAPI::Resource::DataBag!**

Parameters:

  • path (String)

    the path to the data bag folder on disk

  • name (String) (defaults to: File.basename(path))

    the name of the data bag

Returns:

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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.exist?(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|
    Resource::DataBagItem.from_file(item, bag)
  end
end

Instance Method Details

#itemsObject

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 Also:



74
75
76
# File 'lib/chef-api/resources/data_bag.rb', line 74

def items
  associations[:items] ||= Resource::DataBagItemCollectionProxy.new(self)
end