Class: ConfigBuilder::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/config_builder/model/base.rb

Overview

A ConfigBuilder model implements a logic-less interface to a component of Vagrant.

A model should implement the following methods:

self.new_from_hash

This method takes an arbitrarily nested data structure of basic data types (Arrays, Hashes, Numerics, Strings, etc) and instantiates a new object with attributes set based on that data structure.

#to_proc

This method takes the object attributes and generates a lambda that will create a Vagrant config with the state specified by the attributes. The lambda should have an arity of one and should be passed a config object. The generated block will generate the Vagrant config that implements the behavior specified by the object attributes.

If the Model delegates certain configuration to other models, the generated lambda should be able to evaluate lambdas from the delegated models.

Implementing classes do not need to inherit from ConfigBuilder::Model::Base, but it makes life easier.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.def_model_attribute(identifier) ⇒ Object

Parameters:

  • identifier (Symbol)


93
94
95
# File 'lib/config_builder/model/base.rb', line 93

def def_model_attribute(identifier)
  model_attributes << identifier
end

.model_attributesObject



97
98
99
# File 'lib/config_builder/model/base.rb', line 97

def model_attributes
  (@identifiers ||= [])
end

.new_from_hash(attributes) ⇒ Object < ConfigBuilder::Model]

Deserialize a hash into a configbuilder model

Parameters:

  • attributes (Hash)

    The model attributes as represented in a hash.

Returns:



31
32
33
34
35
# File 'lib/config_builder/model/base.rb', line 31

def self.new_from_hash(attributes)
  obj = new()
  obj.attrs = attributes
  obj
end

Instance Method Details

#attrs=(config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
41
42
43
44
45
46
# File 'lib/config_builder/model/base.rb', line 38

def attrs=(config)
  hash = config.inject({}) { |hash, (key, value)| hash[key.to_sym] = value; hash }

  if @defaults
    @attrs = @defaults.merge(hash)
  else
    @attrs = hash
  end
end

#call(config) ⇒ void

This method returns an undefined value.

Generate a block based on the attribute configuration and call it with the given config.

Parameters:

  • config (Vagrant.plugin('2', :config))


61
62
63
# File 'lib/config_builder/model/base.rb', line 61

def call(config)
  to_proc.call(config)
end

#to_procProc

This method is abstract.

Generate a block based on configuration specified by the attributes

Returns:

  • (Proc)

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/config_builder/model/base.rb', line 52

def to_proc
  raise NotImplementedError
end