Class: ChefAPI::Schema
- Inherits:
-
Object
- Object
- ChefAPI::Schema
- Defined in:
- lib/chef-api/schema.rb
Overview
A wrapper class that describes a remote schema (such as the Chef Server API layer), with validation and other magic spinkled on top.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
The full list of attributes defined on this schema.
-
#ignored_attributes ⇒ Object
readonly
Returns the value of attribute ignored_attributes.
-
#validators ⇒ Array
readonly
The list of defined validators for this schema.
Instance Method Summary collapse
-
#attribute(key, options = {}) ⇒ Symbol
DSL method for defining an attribute.
-
#flavor(id, &block) ⇒ Proc
Create a lazy-loaded block for a given flavor.
-
#ignore(*keys) ⇒ Object
Ignore an attribute.
-
#initialize(&block) ⇒ Schema
constructor
Create a new schema and evaulte the block contents in a clean room.
-
#load_flavor(id) ⇒ true, false
Load the flavor block for the given id.
-
#primary_key ⇒ Symbol
The defined primary key for this schema.
Constructor Details
#initialize(&block) ⇒ Schema
Create a new schema and evaulte the block contents in a clean room.
27 28 29 30 31 32 33 34 |
# File 'lib/chef-api/schema.rb', line 27 def initialize(&block) @attributes = {} @ignored_attributes = {} @flavor_attributes = {} @validators = [] unlock { instance_eval(&block) } if block end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
The full list of attributes defined on this schema.
13 14 15 |
# File 'lib/chef-api/schema.rb', line 13 def attributes @attributes end |
#ignored_attributes ⇒ Object (readonly)
Returns the value of attribute ignored_attributes.
15 16 17 |
# File 'lib/chef-api/schema.rb', line 15 def ignored_attributes @ignored_attributes end |
#validators ⇒ Array (readonly)
The list of defined validators for this schema.
22 23 24 |
# File 'lib/chef-api/schema.rb', line 22 def validators @validators end |
Instance Method Details
#attribute(key, options = {}) ⇒ Symbol
DSL method for defining an attribute.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/chef-api/schema.rb', line 96 def attribute(key, = {}) if primary_key = .delete(:primary) @primary_key = key.to_sym end @attributes[key] = .delete(:default) # All remaining options are assumed to be validations .each do |validation, | if @validators << Validator.find(validation).new(key, ) end end key end |
#flavor(id, &block) ⇒ Proc
Create a lazy-loaded block for a given flavor.
62 63 64 65 |
# File 'lib/chef-api/schema.rb', line 62 def flavor(id, &block) @flavor_attributes[id] = block block end |
#ignore(*keys) ⇒ Object
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).
121 122 123 124 125 |
# File 'lib/chef-api/schema.rb', line 121 def ignore(*keys) keys.each do |key| @ignored_attributes[key.to_sym] = true end end |
#load_flavor(id) ⇒ true, false
Load the flavor block for the given id.
76 77 78 79 80 81 82 83 |
# 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 |
#primary_key ⇒ Symbol
The defined primary key for this schema. If no primary key is given, it is assumed to be the first item in the list.
42 43 44 |
# File 'lib/chef-api/schema.rb', line 42 def primary_key @primary_key ||= @attributes.first[0] end |