Class: Liquidscript::Scanner::Base::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/liquidscript/scanner/base/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Context

Returns a new instance of Context.



8
9
10
11
12
13
# File 'lib/liquidscript/scanner/base/context.rb', line 8

def initialize(name)
  @name    = name
  @matches = {}
  @temps   = {}
  @actions = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/liquidscript/scanner/base/context.rb', line 6

def name
  @name
end

Instance Method Details

#action(name, &block) ⇒ Object



15
16
17
# File 'lib/liquidscript/scanner/base/context.rb', line 15

def action(name, &block)
  @actions[name] = block
end

#exitObject



30
31
32
# File 'lib/liquidscript/scanner/base/context.rb', line 30

def exit
  EXIT
end

#find_matcher(with) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/liquidscript/scanner/base/context.rb', line 19

def find_matcher(with)
  best_match = [0, nil, nil]
  @matches.each do |k, v|
    if with.match?(k) && with.matched_size > best_match[0]
      best_match = [with.matched_size, k, v]
    end
  end

  best_match[1..-1]
end

#get(key) ⇒ Object



46
47
48
# File 'lib/liquidscript/scanner/base/context.rb', line 46

def get(key)
  normalize_matcher @temps[key]
end

#initObject



34
35
36
37
38
39
40
# File 'lib/liquidscript/scanner/base/context.rb', line 34

def init
  if block_given?
    @init = Proc.new
  else
    @init || proc {}
  end
end

#on(matcher, action = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/liquidscript/scanner/base/context.rb', line 50

def on(matcher, action = nil)
  key = nil
  value = nil

  if block_given?
    key = normalize_matcher matcher
    value = Proc.new
  elsif action
    key = normalize_matcher matcher
    value = @actions.fetch(action)
  else
    key = normalize_matcher matcher.keys.first
    value = matcher.values.first
  end

  @matches[key] = value
end

#set(key, matcher) ⇒ Object



42
43
44
# File 'lib/liquidscript/scanner/base/context.rb', line 42

def set(key, matcher)
  @temps[key] = matcher
end