Class: Microparser::Base
- Inherits:
-
Object
- Object
- Microparser::Base
- Defined in:
- lib/microparser/base.rb
Instance Attribute Summary collapse
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Instance Method Summary collapse
-
#initialize(pattern, &block) ⇒ Base
constructor
A new instance of Base.
-
#load(data) ⇒ Object
Loads the string into the microparser.
-
#maps ⇒ Object
Returns the array of mapping names that are registered with the microparser.
-
#matches?(str) ⇒ Boolean
Test to see if the pattern inside the microparser matches a given string.
-
#method_missing(method, *args, &block) ⇒ Object
:nodoc:.
Constructor Details
#initialize(pattern, &block) ⇒ Base
Returns a new instance of Base.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/microparser/base.rb', line 30 def initialize(pattern,&block) raise Errors::MissingMappingBlockError.new("The parser requires a mapping block!") unless block_given? @pattern = pattern @parsed = nil @maps = Hash.new @map_names = nil @data = nil create_maps(block) raise Errors::EmptyMappingBlockError.new("The parser requires at least one mapping!") if @maps.empty? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
:nodoc:
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/microparser/base.rb', line 77 def method_missing(method,*args,&block) #:nodoc: meth = method.to_sym if maps.include?(meth) raise Errors::EmptyDataError.new("The parser must be provided data before attempting to access attributes!") if @data.nil? construct_mapping_value(meth) else super(method,*args,&block) end end |
Instance Attribute Details
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
28 29 30 |
# File 'lib/microparser/base.rb', line 28 def pattern @pattern end |
Instance Method Details
#load(data) ⇒ Object
Loads the string into the microparser.
If the string is parseable by the internal regex, the string will be stored, the attributes will be made available and the method will return true.
If the string isn’t parseable, the microparser will remain unchanged and the method will return false.
67 68 69 70 71 72 73 74 75 |
# File 'lib/microparser/base.rb', line 67 def load(data) if matches?(data) @data = data @parsed = nil true else false end end |
#maps ⇒ Object
Returns the array of mapping names that are registered with the microparser
52 53 54 55 56 57 58 |
# File 'lib/microparser/base.rb', line 52 def maps @map_names ||= @maps.keys.map {|mapping| mapping.to_s }.sort.map {|mapping| mapping.to_sym } end |
#matches?(str) ⇒ Boolean
Test to see if the pattern inside the microparser matches a given string
47 48 49 |
# File 'lib/microparser/base.rb', line 47 def matches?(str) !str.match(@pattern).nil? end |