# File lib/chef-api/schema.rb, line 42 def primary_key @primary_key ||= @attributes.first[0] end
class ChefAPI::Schema
A wrapper class that describes a remote schema (such as the Chef Server API layer), with validation and other magic spinkled on top.
Attributes
The full list of attributes defined on this schema.
@return [Hash]
The list of defined validators for this schema.
@return [Array]
Public Class Methods
Create a new schema and evaulte the block contents in a clean room.
# File lib/chef-api/schema.rb, line 27 def initialize(&block) @attributes = {} @ignored_attributes = {} @flavor_attributes = {} @validators = [] unlock { instance_eval(&block) } if block end
Public Instance Methods
DSL method for defining an attribute.
@param [Symbol] key
the key to use
@param [Hash] options
a list of options to create the attribute with
@return [Symbol]
the attribute
# File lib/chef-api/schema.rb, line 96 def attribute(key, options = {}) if primary_key = options.delete(:primary) @primary_key = key.to_sym end @attributes[key] = options.delete(:default) # All remaining options are assumed to be validations options.each do |validation, options| if options @validators << Validator.find(validation).new(key, options) end end key end
Create a lazy-loaded block for a given flavor.
@example Create a block for Enterprise Chef
flavor :enterprise do attribute :custom_value end
@param [Symbol] id
the id of the flavor to target
@param [Proc] block
the block to capture
@return [Proc]
the given block
# File lib/chef-api/schema.rb, line 62 def flavor(id, &block) @flavor_attributes[id] = block block end
Ignore an attribute. This is handy if you know there's an attribute that the remote server will return, but you don't want that information exposed to the user (or the data is sensitive).
@param [Array<Symbol>] keys
the list of attributes to ignore
# File lib/chef-api/schema.rb, line 121 def ignore(*keys) keys.each do |key| @ignored_attributes[key.to_sym] = true end end
Load the flavor block for the given id.
@param [Symbol] id
the id of the flavor to target
@return [true, false]
true if the flavor existed and was evaluted, false otherwise
# File lib/chef-api/schema.rb, line 76 def load_flavor(id) if block = @flavor_attributes[id] unlock { instance_eval(&block) } true else false end end
The defined primary key for this schema. If no primary key is given, it is assumed to be the first item in the list.
@return [Symbol]
Private Instance Methods
@private
Helper method to duplicate and unfreeze all the attributes in the schema, yield control to the user for modification in the current context, and then re-freeze the variables for modification.
# File lib/chef-api/schema.rb, line 136 def unlock @attributes = @attributes.dup @ignored_attributes = @ignored_attributes.dup @flavor_attributes = @flavor_attributes.dup @validators = @validators.dup yield @attributes.freeze @ignored_attributes.freeze @flavor_attributes.freeze @validators.freeze end