Class: DumpParser
- Inherits:
-
Object
- Object
- DumpParser
- Defined in:
- lib/dump-parser.rb
Overview
Implements a really dump value 2 value parser DSL
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- DEFAULT_MAP_BLOCK =
Proc.new { fetch_map }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .execute(name, value) ⇒ Object
- .has_parser?(name) ⇒ Boolean
- .lookup(name) ⇒ Object (also: [])
- .names ⇒ Object
- .register(name, map = nil, &block) ⇒ Object
- .reset! ⇒ Object
Instance Method Summary collapse
- #execute(value) ⇒ Object
-
#initialize(name, map = nil, block = nil) ⇒ DumpParser
constructor
A new instance of DumpParser.
Constructor Details
#initialize(name, map = nil, block = nil) ⇒ DumpParser
Returns a new instance of DumpParser.
10 11 12 13 14 15 16 17 18 |
# File 'lib/dump-parser.rb', line 10 def initialize(name,map=nil,block=nil) unless block || map raise ArgumentError,'need map block or both' end block ||= DEFAULT_MAP_BLOCK @name,@map,@block = name,map,block end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/dump-parser.rb', line 8 def name @name end |
Class Method Details
.execute(name, value) ⇒ Object
105 106 107 108 |
# File 'lib/dump-parser.rb', line 105 def execute(name,value) parser = lookup name parser.execute(value) end |
.has_parser?(name) ⇒ Boolean
126 127 128 |
# File 'lib/dump-parser.rb', line 126 def has_parser?(name) registry.key? name.to_s end |
.lookup(name) ⇒ Object Also known as: []
119 120 121 122 |
# File 'lib/dump-parser.rb', line 119 def lookup(name) name = name.to_s registry[name] || raise(ArgumentError,"a parser named #{name.inspect} is not registred") end |
.names ⇒ Object
134 135 136 |
# File 'lib/dump-parser.rb', line 134 def names registry.keys end |
.register(name, map = nil, &block) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/dump-parser.rb', line 110 def register(name,map=nil,&block) name = name.to_s if has_parser? name raise ArgumentError,"a parser named #{name.inspect} is already registred" end parser = self.new(name,map,block) registry[name]=parser end |
.reset! ⇒ Object
130 131 132 |
# File 'lib/dump-parser.rb', line 130 def reset! @registry = nil end |
Instance Method Details
#execute(value) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dump-parser.rb', line 20 def execute(value) unless value.kind_of? String raise ArgumentError.new("#{name}: parse input \"#{value.inspect}\" must be kind of String") end @value = value begin catch :done do @result = instance_eval(&@block) end returning @result do @result = nil end ensure reset! end end |