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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.model_idSymbol (readonly)

Fetch the model identifier

Returns:

Since:

  • 0.16.0



46
47
48
# File 'lib/config_builder/model/base.rb', line 46

def model_id
  @model_id
end

Class Method Details

.def_model_attribute(identifier) ⇒ Symbol

Define a new model attribute

Model attributes are used to configure Vagrant objects.

Parameters:

  • identifier (Symbol)

Returns:

  • (Symbol)

    The identifier passed to def_model_attribute.



55
56
57
58
59
60
61
# File 'lib/config_builder/model/base.rb', line 55

def def_model_attribute(identifier)
  @model_attributes ||= []

  @model_attributes << identifier

  identifier
end

.def_model_delegator(identifier) ⇒ Symbol

Define a new model delegator

Model delegators are used to hand configuration tasks off to other model classes.

Parameters:

  • identifier (Symbol)

Returns:

  • (Symbol)

    The identifier passed to def_model_delegator.

Since:

  • 0.16.0



124
125
126
127
128
129
130
# File 'lib/config_builder/model/base.rb', line 124

def def_model_delegator(identifier)
  @model_delegators ||= []

  @model_delegators << identifier

  identifier
end

.def_model_id(identifier) ⇒ Symbol

Define the model identifier

This method defines an entry in the data structure which is to be used as an identifier for generated model instances.

Parameters:

  • identifier (Symbol)

Returns:

  • (Symbol)

    The identifier passed to def_model_id.

Since:

  • 0.16.0



37
38
39
# File 'lib/config_builder/model/base.rb', line 37

def def_model_id(identifier)
  @model_id = identifier
end

.def_model_option(identifier) ⇒ Symbol

Define a new model option

Model options are used when building new Vagrant objects.

Parameters:

  • identifier (Symbol)

Returns:

  • (Symbol)

    The identifier passed to def_model_option.

Since:

  • 0.16.0



88
89
90
91
92
93
94
# File 'lib/config_builder/model/base.rb', line 88

def def_model_option(identifier)
  @model_options ||= []

  @model_options << identifier

  identifier
end

.model_attributesArray<Symbol>

Return all attributes defined for this model

This method also returns inherited attributes.

Returns:

  • (Array<Symbol>)

    A list of model atttributes.



68
69
70
71
72
73
74
75
76
77
# File 'lib/config_builder/model/base.rb', line 68

def model_attributes
  @model_attributes ||= []

  if (self < ::ConfigBuilder::Model::Base)
    # This is a subclass of Model::Base
    superclass.model_attributes + @model_attributes
  else
    @model_attributes
  end
end

.model_delegatorsArray<Symbol>

Return all delegators defined for this model

This method also returns inherited delegators.

Returns:

  • (Array<Symbol>)

    A list of model delegators.

Since:

  • 0.16.0



139
140
141
142
143
144
145
146
147
148
# File 'lib/config_builder/model/base.rb', line 139

def model_delegators
  @model_delegators ||= []

  if (self < ::ConfigBuilder::Model::Base)
    # This is a subclass of Model::Base
    superclass.model_delegators + @model_delegators
  else
    @model_delegators
  end
end

.model_optionsArray<Symbol>

Return all options defined for this model

This method also returns inherited options.

Returns:

  • (Array<Symbol>)

    A list of model options.

Since:

  • 0.16.0



103
104
105
106
107
108
109
110
111
112
# File 'lib/config_builder/model/base.rb', line 103

def model_options
  @model_options ||= []

  if (self < ::ConfigBuilder::Model::Base)
    # This is a subclass of Model::Base
    superclass.model_options + @model_options
  else
    @model_options
  end
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:



155
156
157
158
159
# File 'lib/config_builder/model/base.rb', line 155

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

Instance Method Details

#attr(identifier) ⇒ Object

TODO:

validate identifier

Returns The value of the model attribute specified by identifier.

Parameters:

  • identifier (Symbol)

Returns:

  • (Object)

    The value of the model attribute specified by identifier



194
195
196
# File 'lib/config_builder/model/base.rb', line 194

def attr(identifier)
  @attrs[identifier]
end

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



162
163
164
165
166
167
168
169
170
# File 'lib/config_builder/model/base.rb', line 162

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


185
186
187
# File 'lib/config_builder/model/base.rb', line 185

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

#configure!(config) ⇒ void

This method returns an undefined value.

Copy attributes to a Vagrant configuration object

This method iterates over each attribute defined via

def_model_attribute and copies data to a Vagrant configuration object.

By default, config.attributename = value is used. To provide custom behavior, define a configure_attributename method. This method will be passed the vagrant configuration object and the attribute value.

Parameters:

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

    Vagrant configuration object.

Since:

  • 0.16.0



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/config_builder/model/base.rb', line 264

def configure!(config)
  self.class.model_attributes.each do |id|
    if self.respond_to?("configure_#{id}")
      # Call custom configuration method if defined.
      with_attr(id) {|val| send("configure_#{id}", config, val)}
    else
      # 99% of the time, it's just config.thing = val
      with_attr(id) {|val| config.send("#{id}=", val)}
    end
  end
end

#eval_models(config) ⇒ Object



216
217
218
219
220
221
# File 'lib/config_builder/model/base.rb', line 216

def eval_models(config)
  model_delegators.each do |model|
    meth = "eval_#{model}"
    self.send(meth, config)
  end
end

#instance_idObject

Return the identifier value for this model instance

Returns:

  • (Object)

Since:

  • 0.16.0



228
229
230
# File 'lib/config_builder/model/base.rb', line 228

def instance_id
  attr(self.class.model_id)
end

#instance_optionsHash

Return a hash of all options which have been given a value

This method returns a hash of options and their values. Options that were not present in the data used to create a model instance will not be returned.

Returns:

  • (Hash)

Since:

  • 0.16.0



241
242
243
244
245
246
247
248
249
# File 'lib/config_builder/model/base.rb', line 241

def instance_options
  result = Hash.new

  self.class.model_options.each do |id|
    with_attr(id) {|val| result[id] = val}
  end

  result
end

#model_delegatorsObject



212
213
214
# File 'lib/config_builder/model/base.rb', line 212

def model_delegators
  self.class.model_delegators
end

#to_procProc

This method is abstract.

Generate a block based on configuration specified by the attributes

Returns:

  • (Proc)

Raises:

  • (NotImplementedError)


176
177
178
# File 'lib/config_builder/model/base.rb', line 176

def to_proc
  raise NotImplementedError
end

#with_attr(identifier) ⇒ void

This method returns an undefined value.

Conditionally evaluate a block with a model attribute if it's defined

Parameters:

  • identifier (Symbol)

    The attribute identifier

Since:

  • 0.6.0



205
206
207
208
209
210
# File 'lib/config_builder/model/base.rb', line 205

def with_attr(identifier)
  val = @attrs[identifier]
  unless val.nil?
    yield val
  end
end