Class: Magiq::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/magiq/builder.rb

Constant Summary collapse

END_RNG =
/\}([a-z0-9_]+)\Z/
START_RNG =
/\A([a-z0-9_]+)\{/
CONSTRAINTS =
[:mutual, :exclusive]
LISTENERS =
[:check, :apply]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



12
13
14
15
16
# File 'lib/magiq/builder.rb', line 12

def initialize
  @listeners   = Hash.new { |h, k| h[k] = [] }
  @constraints = []
  @params      = {}
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



10
11
12
# File 'lib/magiq/builder.rb', line 10

def constraints
  @constraints
end

#listenersObject (readonly)

Returns the value of attribute listeners.



10
11
12
# File 'lib/magiq/builder.rb', line 10

def listeners
  @listeners
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/magiq/builder.rb', line 10

def params
  @params
end

Instance Method Details

#add_constraint(op, params_arg, opts = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



18
19
20
# 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 = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



22
23
24
25
26
27
28
# 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