class Sequoia::Builder

Class: Config builder

Yields the block with definitions and then build keys and values Also works as a chainable builder

Attributes

attrs[R]

Storage for builded attributes

Public Class Methods

new(attrs=Store.new, &block) click to toggle source

Private: Initialize a new Builder

Params:

  • attrs {Sequoia::Store} For internal use only (optional)

Yields: block with key-value definitions

# File lib/sequoia/builder.rb, line 50
def initialize(attrs=Store.new, &block)
  @attrs = attrs
  instance_eval(&block) if ::Kernel.block_given?
end

Public Instance Methods

class() click to toggle source

Returns the class of object

This method must always be called with an explicit receiver, as ‘class` is also a reserved word in Ruby

# File lib/sequoia/builder.rb, line 20
def class
  Builder
end
inspect()
Alias for: to_s
pretty_inspect()
Alias for: to_s
respond_to?(*) click to toggle source

Of course we respond to any key name

# File lib/sequoia/builder.rb, line 27
def respond_to?(*)
  true
end
to_s() click to toggle source

Represent builder as string

# File lib/sequoia/builder.rb, line 34
def to_s
  "#<Sequoia::Builder attrs=#{attrs}>"
end
Also aliased as: inspect, pretty_inspect

Private Instance Methods

method_missing(method_name, *args, &block) click to toggle source

Private: Method missing handler

This is where all the magic happens

Params:

  • method_name {Symbol} Method name

  • args {Array} Array of arguments sended to the method

Yields: Block with nested definitions

# File lib/sequoia/builder.rb, line 66
def method_missing(method_name, *args, &block)
  key = normalize_method_name(method_name)

  if args.length > 0
    attrs[key] = args.length > 1 ? args : args[0]
  else
    attrs[key] ||= Store.new
    self.class.new(attrs[key], &block)
  end
end
normalize_method_name(method_name) click to toggle source

Private: Remove trailing ‘=` from getter name if exists

Params:

  • method_name {Symbol} Method name

Returns: {Symbol} Normalized method name

# File lib/sequoia/builder.rb, line 85
def normalize_method_name(method_name)
  method_string = method_name.to_s
  method_string.chop! if method_string.end_with?('=')
  method_string.to_sym
end