Class: Threatinator::Parsers::Getline::Parser
- Inherits:
-
Threatinator::Parser
- Object
- Threatinator::Parser
- Threatinator::Parsers::Getline::Parser
- Defined in:
- lib/threatinator/parsers/getline/parser.rb
Overview
Parses an IO, yielding each ‘line’ of data as deliniated by :separator. The text matching :separator will be included.
Instance Attribute Summary collapse
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(opts = {}) ⇒ Parser
constructor
A new instance of Parser.
- #run(io) {|line| ... } ⇒ Object
Methods inherited from Threatinator::Parser
Constructor Details
#initialize(opts = {}) ⇒ Parser
Returns a new instance of Parser.
15 16 17 18 19 20 21 |
# File 'lib/threatinator/parsers/getline/parser.rb', line 15 def initialize(opts = {}) @separator = opts.delete(:separator) || "\n" unless @separator.length == 1 raise ArgumentError.new(":separator must be exactly one character long") end super(opts) end |
Instance Attribute Details
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
10 11 12 |
# File 'lib/threatinator/parsers/getline/parser.rb', line 10 def separator @separator end |
Instance Method Details
#==(other) ⇒ Object
23 24 25 26 |
# File 'lib/threatinator/parsers/getline/parser.rb', line 23 def ==(other) @separator == other.separator && super(other) end |
#run(io) {|line| ... } ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/threatinator/parsers/getline/parser.rb', line 31 def run(io) return enum_for(:each) unless block_given? lineno = 1 while str = io.gets(@separator) return if str.nil? pos_start = io.pos - str.length yield Record.new(str, line_number: lineno, pos_start: pos_start, pos_end: io.pos) lineno += 1 end nil end |