Class: Lomic::Rule

Inherits:
Lomic
  • Object
show all
Defined in:
lib/lomic/Rule.rb

Constant Summary

Constants included from Lomic

VERSION

Instance Method Summary collapse

Methods included from Lomic

parse

Constructor Details

#initialize(number) ⇒ Rule

Returns a new instance of Rule.


4
5
6
7
# File 'lib/lomic/Rule.rb', line 4

def initialize(number)
  @number = number
  @event_bag = {} # "event_name" => [Event]
end

Instance Method Details

#event(event_name, options = {}, &block) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lomic/Rule.rb', line 10

def event(event_name, options={}, &block)
  options[:priority] ||= 5
  options[:name] = event_name
  options[:block] = block
  
  event = Event.new(options)
  if @event_bag[event_name].nil?
    @event_bag[event_name] = [event]
  else
    # insert into sorted spot
    arr = @event_bag[event_name]
    i = 0
    arr.each do |e|
      if event.priority > e.priority
        arr.insert(i,event)
        break
      elsif i == arr.size-1
        arr.insert(i+1,event)
        break
      end
      i += 1
    end
  end
end

#event_bagObject


35
36
37
# File 'lib/lomic/Rule.rb', line 35

def event_bag
  @event_bag
end

#inspectObject


39
40
41
# File 'lib/lomic/Rule.rb', line 39

def inspect
  "Rule #{@number}: #{@event_bag.inspect}"
end