Sha256: 91a19ca4d13d77a48163c4c0ad79e0cc8b66071c4a7766a6c4963dade63e6ce3

Contents?: true

Size: 940 Bytes

Versions: 2

Compression:

Stored size: 940 Bytes

Contents

module ChefAPI
  class Resource::DataBagItem < Resource::Base
    collection_path '/data/:bag'

    schema do
      attribute :id,   type: String, primary: true, required: true
      attribute :data, type: Hash,   default: {}
    end

    class << self
      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
    end

    attr_reader :bag

    #
    # Override the initialize method to move any attributes into the +data+
    # hash.
    #
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chef-api-0.2.1 lib/chef-api/resources/data_bag_item.rb
chef-api-0.2.0 lib/chef-api/resources/data_bag_item.rb