class RabbitMQSpec::DSL::Builder::Base

Base class for each builder. A Builder is a class where the DSL evaluator will run the DSL files. It is where we define our DSL syntax. For more info to know how to use it, see the specs related to the builders.

Public Class Methods

build(default_entity_values = {}, &block) click to toggle source

@public This method evaluates the given blog inside a new instance of the Builder class and asks for the class to build the correlated entity

# File lib/rabbitmq-spec/dsl/builder/base.rb, line 42
def self.build(default_entity_values = {}, &block)
  builder = new(default_entity_values)
  builder.instance_eval(&block)
  builder.build_entity
end
define_dsl_attribute(attribute_name) click to toggle source

@private Helper method to inhereted classes. We declare witch methods will be allowed to be called inside a builder instance in order to store the value passed to the method

# File lib/rabbitmq-spec/dsl/builder/base.rb, line 14
def self.define_dsl_attribute(attribute_name)
  @allowed_dsl_attributes ||= []
  @allowed_dsl_attributes << attribute_name.to_sym
end
define_entity_class(entity_class) click to toggle source

@private Helper method to inhereted classes. The child class must call this method Passing and entity to be builded after all the the attributes was filled

# File lib/rabbitmq-spec/dsl/builder/base.rb, line 24
def self.define_entity_class(entity_class)
  @entity_class = entity_class
end
get_entity_class() click to toggle source

@private

# File lib/rabbitmq-spec/dsl/builder/base.rb, line 29
def self.get_entity_class
  @entity_class
end
has_dsl_attribute?(attribute_name) click to toggle source

@private

# File lib/rabbitmq-spec/dsl/builder/base.rb, line 34
def self.has_dsl_attribute?(attribute_name)
  @allowed_dsl_attributes.include?(attribute_name.to_sym)
end
new(default_entity_values = {}) click to toggle source

instance methods

# File lib/rabbitmq-spec/dsl/builder/base.rb, line 49
def initialize(default_entity_values = {})
  @builded_attributes = {}.merge(default_entity_values)
end

Public Instance Methods

build_entity() click to toggle source
# File lib/rabbitmq-spec/dsl/builder/base.rb, line 68
def build_entity
  raise "Entity class is not defined for #{self.class}" if self.class.get_entity_class.nil?
  entity = self.class.get_entity_class.new
  @builded_attributes.each_pair do |k, v|
    entity.send("#{k}=", v)
  end
  entity
end
method_missing(method_name, *args, &block) click to toggle source

For each method called we verify if it's an defined attribute for the builder. If it is then we store the argument as value of the attribute

# File lib/rabbitmq-spec/dsl/builder/base.rb, line 56
def method_missing(method_name, *args, &block)
  if self.class.has_dsl_attribute?(method_name.to_sym)
    @builded_attributes[method_name.to_sym] = if block_given?
                                                build_hash_from_block(&block)
                                              else
                                                args[0]
    end
  else
    raise "Configuration '#{method_name}' is not allowed for #{self}"
  end
end

Private Instance Methods

build_hash_from_block(&block) click to toggle source
# File lib/rabbitmq-spec/dsl/builder/base.rb, line 101
def build_hash_from_block(&block)
  builder = HashBuilder.new
  builder.instance_eval(&block)
  builder.build
end