Class: Lab42::Rgxargs::SyntaxDefiner

Inherits:
Object
  • Object
show all
Defined in:
lib/lab42/rgxargs/syntax_definer.rb

Overview

Instanciated with an Rgxargs parser it can run a block in its own context so that the block accesses the parser’s functionality with syntactic sugar w/o cluttering ther parser itself with too many methods E.g.:

```ruby
    syntax

```

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#arg_nameObject (readonly)

Returns the value of attribute arg_name.



14
15
16
# File 'lib/lab42/rgxargs/syntax_definer.rb', line 14

def arg_name
  @arg_name
end

#parserObject (readonly)

Returns the value of attribute parser.



14
15
16
# File 'lib/lab42/rgxargs/syntax_definer.rb', line 14

def parser
  @parser
end

Instance Method Details

#run(code) ⇒ Object



16
17
18
# File 'lib/lab42/rgxargs/syntax_definer.rb', line 16

def run code
  instance_exec(&code)
end

#syntax(matcher, value = nil, &blk) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/lab42/rgxargs/syntax_definer.rb', line 20

def syntax(matcher, value=nil, &blk)
  if value
    parser.add_syntax(matcher, ->(){value}, as: arg_name )
  else
    parser.add_syntax(matcher, blk, as: arg_name)
  end
end