Class: Toys::Parser
- Inherits:
-
Object
- Object
- Toys::Parser
- Defined in:
- lib/toys/parser.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_binding ⇒ Object
- #alias_as(word) ⇒ Object
- #alias_of(target) ⇒ Object
- #execute(&block) ⇒ Object
- #expand(template, opts = {}) {|opts| ... } ⇒ Object
- #helper(name, &block) ⇒ Object
- #helper_module(mod, &block) ⇒ Object
- #include(path) ⇒ Object
-
#initialize(path, tool, remaining_words, priority, lookup) ⇒ Parser
constructor
A new instance of Parser.
- #long_desc(desc) ⇒ Object
- #name(word, alias_of: nil, &block) ⇒ Object
- #optional_arg(key, accept: nil, default: nil, doc: nil) ⇒ Object
- #remaining_args(key, accept: nil, default: [], doc: nil) ⇒ Object
- #required_arg(key, accept: nil, doc: nil) ⇒ Object
- #short_desc(desc) ⇒ Object
- #switch(key, *switches, accept: nil, default: nil, doc: nil) ⇒ Object
Constructor Details
#initialize(path, tool, remaining_words, priority, lookup) ⇒ Parser
Returns a new instance of Parser.
3 4 5 6 7 8 9 |
# File 'lib/toys/parser.rb', line 3 def initialize(path, tool, remaining_words, priority, lookup) @path = path @tool = tool @remaining_words = remaining_words @priority = priority @lookup = lookup end |
Class Method Details
.parse(path, tool, remaining_words, priority, lookup, source) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/toys/parser.rb', line 130 def self.parse(path, tool, remaining_words, priority, lookup, source) parser = new(path, tool, remaining_words, priority, lookup) tool.defining_from(path) do if String === source eval(source, parser._binding, path, 1) elsif Proc === source parser.instance_eval(&source) end end tool end |
Instance Method Details
#_binding ⇒ Object
126 127 128 |
# File 'lib/toys/parser.rb', line 126 def _binding binding end |
#alias_as(word) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/toys/parser.rb', line 39 def alias_as(word) unless @tool.root? alias_tool = @lookup.get_tool(@tool.full_name.slice(0..-2) + [word], @priority) alias_tool.set_alias_target(@tool) if alias_tool end self end |
#alias_of(target) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/toys/parser.rb', line 47 def alias_of(target) target_tool = @lookup.lookup(target) unless target_tool.full_name == target raise Toys::ToysDefinitionError, "Alias target #{target.inspect} not found" end @tool.set_alias_target(target_tool) self end |
#execute(&block) ⇒ Object
107 108 109 110 |
# File 'lib/toys/parser.rb', line 107 def execute(&block) @tool.executor = block self end |
#expand(template, opts = {}) {|opts| ... } ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/toys/parser.rb', line 63 def (template, opts={}) if template.is_a?(Symbol) template = template.to_s file_name = template.gsub(/([a-zA-Z])([A-Z])/){ |m| "#{$1}_#{$2.downcase}" }.downcase require "toys/templates/#{file_name}" const_name = template.gsub(/(^|_)([a-zA-Z0-9])/){ |m| $2.upcase } template = Toys::Templates.const_get(const_name) end opts = template.opts_initializer.call(opts) yield opts if block_given? instance_exec(opts, &template.) self end |
#helper(name, &block) ⇒ Object
112 113 114 115 |
# File 'lib/toys/parser.rb', line 112 def helper(name, &block) @tool.add_helper(name, &block) self end |
#helper_module(mod, &block) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/toys/parser.rb', line 117 def helper_module(mod, &block) if block @tool.define_helper_module(mod, &block) else @tool.use_helper_module(mod) end self end |
#include(path) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/toys/parser.rb', line 56 def include(path) @tool.yield_definition do @lookup.include_path(path, @tool.full_name, @remaining_words, @priority) end self end |
#long_desc(desc) ⇒ Object
77 78 79 80 |
# File 'lib/toys/parser.rb', line 77 def long_desc(desc) @tool.long_desc = desc self end |
#name(word, alias_of: nil, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/toys/parser.rb', line 11 def name(word, alias_of: nil, &block) word = word.to_s subtool = @lookup.get_tool(@tool.full_name + [word], @priority) return self if subtool.nil? if alias_of if block raise Toys::ToysDefinitionError, "Cannot take a block with alias_of" end target = @tool.full_name + [alias_of.to_s] target_tool = @lookup.lookup(target) unless target_tool.full_name == target raise Toys::ToysDefinitionError, "Alias target #{target.inspect} not found" end subtool.set_alias_target(target) return self end next_remaining = @remaining_words if next_remaining && !next_remaining.empty? if next_remaining.first == word next_remaining = next_remaining.slice(1..-1) else next_remaining = nil end end Parser.parse(@path, subtool, next_remaining, @priority, @lookup, block) self end |
#optional_arg(key, accept: nil, default: nil, doc: nil) ⇒ Object
97 98 99 100 |
# File 'lib/toys/parser.rb', line 97 def optional_arg(key, accept: nil, default: nil, doc: nil) @tool.add_optional_arg(key, accept: accept, default: default, doc: doc) self end |
#remaining_args(key, accept: nil, default: [], doc: nil) ⇒ Object
102 103 104 105 |
# File 'lib/toys/parser.rb', line 102 def remaining_args(key, accept: nil, default: [], doc: nil) @tool.set_remaining_args(key, accept: accept, default: default, doc: doc) self end |
#required_arg(key, accept: nil, doc: nil) ⇒ Object
92 93 94 95 |
# File 'lib/toys/parser.rb', line 92 def required_arg(key, accept: nil, doc: nil) @tool.add_required_arg(key, accept: accept, doc: doc) self end |
#short_desc(desc) ⇒ Object
82 83 84 85 |
# File 'lib/toys/parser.rb', line 82 def short_desc(desc) @tool.short_desc = desc self end |
#switch(key, *switches, accept: nil, default: nil, doc: nil) ⇒ Object
87 88 89 90 |
# File 'lib/toys/parser.rb', line 87 def switch(key, *switches, accept: nil, default: nil, doc: nil) @tool.add_switch(key, *switches, accept: accept, default: default, doc: doc) self end |