Class: Bmg::Reader::TextFile

Inherits:
Object
  • Object
show all
Includes:
Bmg::Reader
Defined in:
lib/bmg/reader/text_file.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  strip: true,
  parse: nil
}

Instance Attribute Summary collapse

Attributes included from Bmg::Reader

#type

Instance Method Summary collapse

Methods included from Bmg::Relation

#_count, #bind, #count, #debug, #delete, empty, #empty?, #insert, new, #one, #one_or_nil, #to_ast, #to_csv, #to_json, #to_xlsx, #type, #update, #visit, #with_type, #with_type_attrlist, #with_typecheck, #without_typecheck, #y_by_x, #ys_by_x

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #left_join, #matching, #materialize, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #ungroup, #union, #unspied, #unwrap

Methods included from Algebra::Shortcuts

#exclude, #image, #images, #join, #left_join, #matching, #not_matching, #prefix, #rxmatch, #suffix, #ungroup, #unwrap, #where

Constructor Details

#initialize(type, path, options = {}) ⇒ TextFile

Returns a new instance of TextFile.



11
12
13
14
15
16
# File 'lib/bmg/reader/text_file.rb', line 11

def initialize(type, path, options = {})
  options = { parse: options } if options.is_a?(Regexp)
  @path = path
  @options = DEFAULT_OPTIONS.merge(options)
  @type = infer_type(type)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/bmg/reader/text_file.rb', line 17

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/bmg/reader/text_file.rb', line 17

def path
  @path
end

Instance Method Details

#eachObject



21
22
23
24
25
26
27
# File 'lib/bmg/reader/text_file.rb', line 21

def each
  path.each_line.each_with_index do |text, line|
    text = text.strip if strip?
    parsed = parse(text)
    yield({line: 1+line}.merge(parsed)) if parsed
  end
end