Class: Lita::ConfigurationBuilder

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

Overview

Provides a DSL for building Configuration objects.

Since:

  • 4.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigurationBuilder

Returns a new instance of ConfigurationBuilder

Since:

  • 4.0.0


61
62
63
64
# File 'lib/lita/configuration_builder.rb', line 61

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

Instance Attribute Details

#childrenArray<Lita::ConfigurationBuilder> (readonly)

An array of any nested configuration builders.

Returns:

Since:

  • 4.0.0


11
12
13
# File 'lib/lita/configuration_builder.rb', line 11

def children
  @children
end

#nameString, Symbol

The name of the configuration attribute.

Returns:

  • (String, Symbol)

    The attribute's name.

Since:

  • 4.0.0


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

def name
  @name
end

#requiredBoolean Also known as: required?

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

Returns:

  • (Boolean)

    Whether or not the attribute is required.

Since:

  • 4.0.0


31
32
33
# File 'lib/lita/configuration_builder.rb', line 31

def required
  @required
end

#typesArray<Object>

An array of valid types for the attribute.

Returns:

  • (Array<Object>)

    The array of valid types.

Since:

  • 4.0.0


15
16
17
# File 'lib/lita/configuration_builder.rb', line 15

def types
  @types
end

#validatorProc (readonly)

A block used to validate the attribute.

Returns:

  • (Proc)

    The validation block.

Since:

  • 4.0.0


19
20
21
# File 'lib/lita/configuration_builder.rb', line 19

def validator
  @validator
end

#valueObject

The value of the configuration attribute.

Returns:

  • (Object)

    The attribute's value.

Since:

  • 4.0.0


27
28
29
# File 'lib/lita/configuration_builder.rb', line 27

def value
  @value
end

Class Method Details

.freeze_config(config) ⇒ void

This method returns an undefined value.

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

Parameters:

Since:

  • 4.0.0


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

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

.load_user_config(config_path = nil) ⇒ void

This method returns an undefined value.

Loads configuration from a user configuration file.

Parameters:

  • config_path (String) (defaults to: nil)

    The path to the configuration file.

Since:

  • 4.0.0


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lita/configuration_builder.rb', line 45

def load_user_config(config_path = nil)
  config_path = "lita_config.rb" unless config_path

  begin
    load(config_path)
  rescue Exception => e
    Lita.logger.fatal I18n.t(
      "lita.config.exception",
      message: e.message,
      backtrace: e.backtrace.join("\n")
    )
    abort
  end if File.exist?(config_path)
end

Instance Method Details

#build(object = Configuration.new) ⇒ Lita::Confirmation

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

Parameters:

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

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

Returns:

  • (Lita::Confirmation)

    The fully built configuration object.

Since:

  • 4.0.0


70
71
72
73
74
75
76
77
78
# File 'lib/lita/configuration_builder.rb', line 70

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

  container.public_send(name)
end

#children?Boolean

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:

  • 4.0.0


82
83
84
# File 'lib/lita/configuration_builder.rb', line 82

def children?
  !children.empty?
end

#combine(name, attribute) ⇒ void

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 (Lita::ConfigurationBuilder)

    The configuration builder that should be its value.

Since:

  • 4.0.0


91
92
93
94
95
# File 'lib/lita/configuration_builder.rb', line 91

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.

Declares a configuration attribute.

Parameters:

  • name (String, Symbol)

    The attribute's name.

  • types (Object, Array<Object>)

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

  • type (Object, Array<Object>)

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

  • required (Boolean)

    Whether or not this attribute must be set. If required, and Lita is run without it set, Lita will abort on start up with a message about it.

  • default (Object)

    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:

  • 4.0.0


109
110
111
112
113
114
115
116
117
118
# File 'lib/lita/configuration_builder.rb', line 109

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

  • 4.0.0


132
133
134
135
136
137
138
139
140
141
# File 'lib/lita/configuration_builder.rb', line 132

def validate(&block)
  validator = block

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

  @validator = block
end