Class: TreeSL
- Inherits:
-
Object
- Object
- TreeSL
- Defined in:
- lib/treesl.rb
Instance Attribute Summary collapse
-
#matchers ⇒ Object
Returns the value of attribute matchers.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#post_procs ⇒ Object
Returns the value of attribute post_procs.
-
#root_node ⇒ Object
Returns the value of attribute root_node.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ TreeSL
constructor
A new instance of TreeSL.
- #match(string, matcher = nil) ⇒ Object
- #method_missing(method, *args) ⇒ Object
- #rule(key, *values) ⇒ Object
Constructor Details
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
#matchers ⇒ Object
Returns the value of attribute matchers.
5 6 7 |
# File 'lib/treesl.rb', line 5 def matchers @matchers end |
#namespace ⇒ Object
Returns the value of attribute namespace.
5 6 7 |
# File 'lib/treesl.rb', line 5 def namespace @namespace end |
#post_procs ⇒ Object
Returns the value of attribute post_procs.
5 6 7 |
# File 'lib/treesl.rb', line 5 def post_procs @post_procs end |
#root_node ⇒ Object
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
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 |