class Magiq::Builder

Constants

CONSTRAINTS
END_RNG
LISTENERS
START_RNG

Attributes

constraints[R]
listeners[R]
params[R]

Public Class Methods

new() click to toggle source
# File lib/magiq/builder.rb, line 12
def initialize
  @listeners   = Hash.new { |h, k| h[k] = [] }
  @constraints = []
  @params      = {}
end

Public Instance Methods

add_constraint(op, params_arg, opts = {}) click to toggle source
# File lib/magiq/builder.rb, line 43
def add_constraint(op, params_arg, opts = {})
  if !CONSTRAINTS.include?(op)
    raise ArgumentError, "unknown constraint type: #{op.inspect}"
  end

  params = Array(params_arg)

  if params.size == 1 && !opts[:exclusive]
    raise ArgumentError, "a single parameter can't be mutual unless it " \
      "has an exclusive counterpart"
  end

  params.each do |p|
    constraints << [op, params, opts]
  end
end
add_listener(type, params, opts = {}, &block) click to toggle source
# File lib/magiq/builder.rb, line 18
def add_listener(type, params, opts = {}, &block)
  listeners_for(type) << [type, params, opts, block]
end
add_param(key, opts = {}) click to toggle source
# File lib/magiq/builder.rb, line 30
def add_param(key, opts = {})
  param = Param.new(key, opts)

  param.keys.each do |k|
    if params.key?(k.to_sym)
      raise ArgumentError, "already registered param under key/alias: " \
      "#{k}"
    end

    params[k] = param
  end
end
listeners_for(type) click to toggle source
# File lib/magiq/builder.rb, line 22
def listeners_for(type)
  if !LISTENERS.include?(type)
    raise ArgumentError, "unknown listener type: #{type.inspect}"
  end

  listeners[type]
end