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.
-
#transformations ⇒ Object
readonly
Returns the value of attribute transformations.
-
#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.
-
#ignore(*keys) ⇒ Object
Ignore an attribute.
-
#initialize(&block) ⇒ Schema
constructor
Create a new schema and evaulte the block contents in a clean room.
-
#primary_key ⇒ Symbol
The defined primary key for this schema.
-
#transform(key, options = {}) ⇒ Object
Transform an attribute onto another.
Constructor Details
#initialize(&block) ⇒ Schema
Create a new schema and evaulte the block contents in a clean room.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/chef-api/schema.rb', line 28 def initialize(&block) @attributes = {} @ignored_attributes = {} @transformations = {} @validators = [] instance_eval(&block) if block @attributes.freeze @ignored_attributes.freeze @transformations.freeze @validators.freeze 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 |
#transformations ⇒ Object (readonly)
Returns the value of attribute transformations.
16 17 18 |
# File 'lib/chef-api/schema.rb', line 16 def transformations @transformations end |
#validators ⇒ Array (readonly)
The list of defined validators for this schema.
23 24 25 |
# File 'lib/chef-api/schema.rb', line 23 def validators @validators end |
Instance Method Details
#attribute(key, options = {}) ⇒ Symbol
DSL method for defining an attribute.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/chef-api/schema.rb', line 63 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 |
#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).
88 89 90 91 92 |
# File 'lib/chef-api/schema.rb', line 88 def ignore(*keys) keys.each do |key| @ignored_attributes[key.to_sym] = true 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.
48 49 50 |
# File 'lib/chef-api/schema.rb', line 48 def primary_key @primary_key ||= @attributes.first[0] end |
#transform(key, options = {}) ⇒ Object
Transform an attribute onto another.
108 109 110 |
# File 'lib/chef-api/schema.rb', line 108 def transform(key, = {}) @transformations[key.to_sym] = end |