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.



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

#attributesHash (readonly)

The full list of attributes defined on this schema.

Returns:

  • (Hash)


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

#transformationsObject (readonly)

Returns the value of attribute transformations.



16
17
18
# File 'lib/chef-api/schema.rb', line 16

def transformations
  @transformations
end

#validatorsArray (readonly)

The list of defined validators for this schema.

Returns:

  • (Array)


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.

Parameters:

  • key (Symbol)

    the key to use

  • options (Hash) (defaults to: {})

    a list of options to create the attribute with

Returns:

  • (Symbol)

    the 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, 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

#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).

Parameters:

  • keys (Array<Symbol>)

    the list of attributes to ignore



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_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.

Returns:

  • (Symbol)


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.

Examples:

Transform the :bacon attribute onto the :ham attribute

transform :bacon, ham: true

Transform an attribute with a complex transformation

transform :bacon, ham: ->(value) { value.split('__', 2).last }

Parameters:

  • key (Symbol)

    the attribute to transform

  • options (Hash) (defaults to: {})

    the key-value pair of the transformations to make



108
109
110
# File 'lib/chef-api/schema.rb', line 108

def transform(key, options = {})
  @transformations[key.to_sym] = options
end