Class: ChefAPI::Schema

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#attributesHash (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_attributesObject (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

#validatorsArray (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, 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

#flavor(id, &block) ⇒ Proc

Create a lazy-loaded block for a given flavor.

Examples:

Create a block for Enterprise Chef

flavor :enterprise do
  attribute :custom_value
end


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_keySymbol

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