Class: ConfigBuilder::Model::Base
- Inherits:
-
Object
- Object
- ConfigBuilder::Model::Base
- 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.
Direct Known Subclasses
NFS, Network::ForwardedPort, Network::PrivateNetwork, Network::PublicNetwork, Provider::Base, Provider::SoftLayer::LoadBalancer, Provisioner::Base, Root, SSH, SyncedFolder, VM, WinRM
Class Attribute Summary collapse
-
.model_id ⇒ Symbol
readonly
Fetch the model identifier.
Class Method Summary collapse
-
.def_model_attribute(identifier) ⇒ Symbol
Define a new model attribute.
-
.def_model_delegator(identifier) ⇒ Symbol
Define a new model delegator.
-
.def_model_id(identifier) ⇒ Symbol
Define the model identifier.
-
.def_model_option(identifier) ⇒ Symbol
Define a new model option.
-
.model_attributes ⇒ Array<Symbol>
Return all attributes defined for this model.
-
.model_delegators ⇒ Array<Symbol>
Return all delegators defined for this model.
-
.model_options ⇒ Array<Symbol>
Return all options defined for this model.
-
.new_from_hash(attributes) ⇒ Object < ConfigBuilder::Model]
Deserialize a hash into a configbuilder model.
Instance Method Summary collapse
-
#attr(identifier) ⇒ Object
The value of the model attribute specified by
identifier
. - #attrs=(config) ⇒ Object private
-
#call(config) ⇒ void
Generate a block based on the attribute configuration and call it with the given config.
-
#configure!(config) ⇒ void
Copy attributes to a Vagrant configuration object.
- #eval_models(config) ⇒ Object
-
#instance_id ⇒ Object
Return the identifier value for this model instance.
-
#instance_options ⇒ Hash
Return a hash of all options which have been given a value.
- #model_delegators ⇒ Object
-
#to_proc ⇒ Proc
abstract
Generate a block based on configuration specified by the attributes.
-
#with_attr(identifier) ⇒ void
Conditionally evaluate a block with a model attribute if it's defined.
Class Attribute Details
.model_id ⇒ Symbol (readonly)
Fetch the model identifier
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.
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.
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.
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.
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_attributes ⇒ Array<Symbol>
Return all attributes defined for this model
This method also returns inherited attributes.
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_delegators ⇒ Array<Symbol>
Return all delegators defined for this model
This method also returns inherited delegators.
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_options ⇒ Array<Symbol>
Return all options defined for this model
This method also returns inherited options.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/config_builder/model/base.rb', line 103 def @model_options ||= [] if (self < ::ConfigBuilder::Model::Base) # This is a subclass of Model::Base superclass. + @model_options else @model_options end end |
.new_from_hash(attributes) ⇒ Object < ConfigBuilder::Model]
Deserialize a hash into a configbuilder model
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
validate identifier
Returns 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.
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.
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_id ⇒ Object
Return the identifier value for this model instance
228 229 230 |
# File 'lib/config_builder/model/base.rb', line 228 def instance_id attr(self.class.model_id) end |
#instance_options ⇒ Hash
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.
241 242 243 244 245 246 247 248 249 |
# File 'lib/config_builder/model/base.rb', line 241 def result = Hash.new self.class..each do |id| with_attr(id) {|val| result[id] = val} end result end |
#model_delegators ⇒ Object
212 213 214 |
# File 'lib/config_builder/model/base.rb', line 212 def model_delegators self.class.model_delegators end |
#to_proc ⇒ Proc
Generate a block based on configuration specified by the attributes
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
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 |