Class: Topping::ConfigurationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/topping/configuration_builder.rb

Overview

Provides a DSL for building Configuration objects.

Since:

  • 0.0.1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigurationBuilder

Returns a new instance of ConfigurationBuilder.

Since:

  • 0.0.1



57
58
59
60
# File 'lib/topping/configuration_builder.rb', line 57

def initialize
  @children = []
  @name = :root
end

Instance Attribute Details

#childrenArray<ConfigurationBuilder> (readonly)

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.

An array of any nested configuration builders.

Returns:

Since:

  • 0.0.1



13
14
15
# File 'lib/topping/configuration_builder.rb', line 13

def children
  @children
end

#configurationBoolean (readonly)

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.

A object that stores user settings

Returns:

  • (Boolean)

    Whether or not the attribute is required.

Since:

  • 0.0.1



18
19
20
# File 'lib/topping/configuration_builder.rb', line 18

def configuration
  @configuration
end

#nameString, Symbol

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.

The name of the configuration attribute.

Returns:

  • (String, Symbol)

    The attribute’s name.

Since:

  • 0.0.1



33
34
35
# File 'lib/topping/configuration_builder.rb', line 33

def name
  @name
end

#requiredBoolean Also known as: required?

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.

A boolean indicating whether or not the attribute must be set.

Returns:

  • (Boolean)

    Whether or not the attribute is required.

Since:

  • 0.0.1



43
44
45
# File 'lib/topping/configuration_builder.rb', line 43

def required
  @required
end

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

An array of valid types for the attribute.

Returns:

  • (Array<Object>)

    The array of valid types.

Since:

  • 0.0.1



23
24
25
# File 'lib/topping/configuration_builder.rb', line 23

def types
  @types
end

#validatorProc (readonly)

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.

A block used to validate the attribute.

Returns:

  • (Proc)

    The validation block.

Since:

  • 0.0.1



28
29
30
# File 'lib/topping/configuration_builder.rb', line 28

def validator
  @validator
end

#valueObject

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.

The value of the configuration attribute.

Returns:

  • (Object)

    The attribute’s value.

Since:

  • 0.0.1



38
39
40
# File 'lib/topping/configuration_builder.rb', line 38

def value
  @value
end

Class Method Details

.freeze_config(config) ⇒ void

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.

This method returns an undefined value.

Deeply freezes a configuration object so that it can no longer be modified.

Parameters:

Since:

  • 0.0.1



52
53
54
# File 'lib/topping/configuration_builder.rb', line 52

def freeze_config(config)
  IceNine.deep_freeze!(config)
end

Instance Method Details

#build(object = Configuration.new) ⇒ Configuration

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.

Builds a Topping::Configuration object from the attributes defined on the builder.

Parameters:

  • object (Configuration) (defaults to: Configuration.new)

    The empty configuration object that will be extended to create the final form.

Returns:

Since:

  • 0.0.1



73
74
75
76
77
78
79
80
81
# File 'lib/topping/configuration_builder.rb', line 73

def build(object = Configuration.new)
  container = if children.empty?
                build_leaf(object)
              else
                build_nested(object)
              end

  @configuration = container.public_send(name)
end

#children?Boolean

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.

Returns a boolean indicating whether or not the attribute has any child attributes.

Returns:

  • (Boolean)

    Whether or not the attribute has any child attributes.

Since:

  • 0.0.1



86
87
88
# File 'lib/topping/configuration_builder.rb', line 86

def children?
  !children.empty?
end

#combine(name, attribute) ⇒ void

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.

This method returns an undefined value.

Merges two configuration builders by making one an attribute on the other.

Parameters:

  • name (String, Symbol)

    The name of the new attribute.

  • attribute (ConfigurationBuilder)

    The configuration builder that should be its value.

Since:

  • 0.0.1



96
97
98
99
100
# File 'lib/topping/configuration_builder.rb', line 96

def combine(name, attribute)
  attribute.name = name

  children << attribute
end

#config(name, types: nil, type: nil, required: false, default: nil) { ... } ⇒ void

This method returns an undefined value.

rubocop:disable Metrics1/ParameterLists Declares a configuration attribute.

Parameters:

  • name (String, Symbol)

    The attribute’s name.

  • types (Object, Array<Object>) (defaults to: nil)

    Optional: One or more types that the attribute’s value must be.

  • type (Object, Array<Object>) (defaults to: nil)

    Optional: One or more types that the attribute’s value must be.

  • required (Boolean) (defaults to: false)

    Whether or not this attribute must be set. If required

  • default (Object) (defaults to: nil)

    An optional default value for the attribute.

Yields:

  • A block to be evaluated in the context of the new attribute. Used for defining nested configuration attributes and validators.

Since:

  • 0.0.1



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/topping/configuration_builder.rb', line 114

def config(name, types: nil, type: nil, required: false, default: nil, &block)
  attribute = self.class.new
  attribute.name = name
  attribute.types = types || type
  attribute.required = required
  attribute.value = default
  attribute.instance_exec(&block) if block

  children << attribute

  # FIXME: I want remove Topping.build process.
  Topping.build
  attribute
end

#configure { ... } ⇒ Object

Merges two configuration builders by making one an attribute on the other.

Yields:

  • The configuration object

Since:

  • 0.0.1



64
65
66
# File 'lib/topping/configuration_builder.rb', line 64

def configure
  yield configuration
end

#validate! { ... } ⇒ void

This method returns an undefined value.

Declares a block to be used to validate the value of an attribute whenever it’s set. Validation blocks should return any object to indicate an error, or nil/false if validation passed.

Yields:

  • The code that performs validation.

Since:

  • 0.0.1



142
143
144
145
146
147
148
149
150
151
# File 'lib/topping/configuration_builder.rb', line 142

def validate!(&block)
  validator = block

  unless value.nil?
    error = validator.call(value)
    raise ValidationError, error if error
  end

  @validator = block
end