Class: Ruport::Parser
- Inherits:
-
Object
- Object
- Ruport::Parser
- Defined in:
- lib/ruport/parser.rb
Overview
If you’re reporting on data, there is a good chance it’s going to need some munging. A 100% compatible modified version of JEG2’s Parse::Input, Ruport::Parser can help you do that.
This class provides a system that lets you specify regular expressions to identify patterns and extract the data you need from your input.
It implements a somewhat straightforward domain specific language to help you scrape and tear up your gnarly input and get something managable.
It is a bit of an expert tool, but is indispensible when messy data needs to be parsed. For now, you’ll probably want to read the source for this particular class. There may be dragons.
Sample usage:
path = "somefile"
data = Ruport::Parser.new(path, "") do
@state = :skip
stop_skipping_at("Save Ad")
skip(/\A\s*\Z/)
pre { @price = @miles = nil }
read(/\$([\d,]+\d)/) { |price| @price = price.delete(",").to_i }
read(/([\d,]*\d)\s*m/) { |miles| @miles = miles.delete(",").to_i }
read do |ad|
if @price and @price < 20_000 and @miles and @miles < 40_000
(@ads ||= Array.new) << ad.strip
end
end
end
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#initialize(path, separator = $/, &init) ⇒ Parser
constructor
A new instance of Parser.
- #method_missing(method, *args, &block) ⇒ Object
Constructor Details
#initialize(path, separator = $/, &init) ⇒ Parser
Returns a new instance of Parser.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ruport/parser.rb', line 71 def initialize( path, separator = $/, &init ) @path = path @separator = separator @state = :read @_pre_readers = Array.new @_readers = Array.new @_post_readers = Array.new @_skip_starters = Array.new @_skip_stoppers = Array.new @_skips = Array.new @_skip_searches = Array.new @_stops = Array.new @_origin = :file instance_eval(&init) unless init.nil? parse end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/ruport/parser.rb', line 99 def method_missing( method, *args, &block ) variable_name = "@#{method}" if instance_variables.include? variable_name instance_variable_get(variable_name) else super end end |
Instance Method Details
#[](name) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/ruport/parser.rb', line 90 def []( name ) variable_name = "@#{name}" if instance_variables.include? variable_name instance_variable_get(variable_name) else nil end end |