Class: TreeSL

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTreeSL

Returns a new instance of TreeSL.



6
7
8
9
10
# File 'lib/treesl.rb', line 6

def initialize
  @matchers = {}
  @post_procs = {}
  @namespace = TreeSL
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



71
72
73
# File 'lib/treesl.rb', line 71

def method_missing(method, *args)
  rule method, *args
end

Instance Attribute Details

#matchersObject

Returns the value of attribute matchers.



5
6
7
# File 'lib/treesl.rb', line 5

def matchers
  @matchers
end

#namespaceObject

Returns the value of attribute namespace.



5
6
7
# File 'lib/treesl.rb', line 5

def namespace
  @namespace
end

#post_procsObject

Returns the value of attribute post_procs.



5
6
7
# File 'lib/treesl.rb', line 5

def post_procs
  @post_procs
end

#root_nodeObject

Returns the value of attribute root_node.



5
6
7
# File 'lib/treesl.rb', line 5

def root_node
  @root_node
end

Class Method Details

.define(namespace = nil, options = {}, &blk) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/treesl.rb', line 12

def self.define(namespace = nil, options = {}, &blk)
  rdp = TreeSL.new
  rdp.root_node = options[:root] || 'root'
  rdp.namespace = namespace if namespace
  rdp.instance_eval &blk
  rdp
end

Instance Method Details

#match(string, matcher = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/treesl.rb', line 20

def match(string, matcher = nil)
  matcher ||= root_node
  string.strip!
  # p [string,matcher]
  result = if matcher.is_a? Array
    matcher.each do |alternative|
      dup_string = string.dup
      if alternative_match = match(dup_string, alternative)
        string.replace dup_string
        return {alternative => alternative_match} 
      end
    end
    nil
  elsif matcher.is_a? String
    if matcher =~ / /
      matches = matcher.split(' ').inject({}) do |hash, atom|
        hash.merge atom => match(string, atom)
      end
      matches.values.any?{|v| v.nil?} ? nil : matches
    elsif new_matcher = matchers[matcher.to_sym]
      match string, new_matcher
    else
      match string, Regexp.new("^#{matcher}")
    end
  elsif matcher.is_a? Regexp
    if string =~ matcher
      string.gsub!(matcher, '');
      $&
    end
  end
  
  if result && matcher.is_a?(String) && matcher =~ /^[a-z_]+$/ && namespace.const_defined?(matcher.camelcase.to_sym)
    "#{namespace}::#{matcher.to_s.camelcase}".constantize.new(result) rescue result
  else
    result
  end
end

#rule(key, *values) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/treesl.rb', line 58

def rule(key, *values)
  key = key.to_sym
  matchers[key] = if matchers[key].nil?
    if values.length == 1
      values.first
    else
      values
    end
  else
    [matchers[key]].flatten + values
  end
end