Class: FixtureReader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/textutils/reader/fixture_reader.rb

Overview

use ManifestReader ?? why? why not? - reuse in manifest gem (or manman e.g. manifest manger) ??

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ FixtureReader

Returns a new instance of FixtureReader.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/textutils/reader/fixture_reader.rb', line 41

def initialize( arg )

  if arg.is_a?( String )  ## old style (deprecated) - pass in filepath as string
    path = arg
    logger.info "FixtureReader.new - deprecated API - use FixtureReader.from_file() instead"
    text = File.read_utf8( path )
  else   ## assume it's a hash
    opts = arg
    text = opts[:text]
  end

  @ary = []

  ## nb: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
  ## - see textutils/utils.rb

  @ary = plain_text_reader( text )

  logger.debug "fixture setup:"
  logger.debug @ary.to_json
end

Class Method Details

.from_file(path) ⇒ Object



29
30
31
32
33
34
# File 'lib/textutils/reader/fixture_reader.rb', line 29

def self.from_file( path )
  ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
  ## - see textutils/utils.rb
  text = File.read_utf8( path )
  self.from_string( text )
end

.from_string(text) ⇒ Object



36
37
38
# File 'lib/textutils/reader/fixture_reader.rb', line 36

def self.from_string( text )
  FixtureReader.new( { text: text } )
end

.from_zip(zip_file, name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/textutils/reader/fixture_reader.rb', line 16

def self.from_zip( zip_file, name )

  ## fix: check if name ends in .txt ?? if not add .txt
  ##  change name to path ?? e.g. make it required to pass in full entry path??
  ### fix -fix -fix => change name to path

  path = name.end_with?('.txt') ? name : "#{name}.txt"

  ## get text content from zip
  text = zip_file.read( path )
  self.from_string( text )
end

Instance Method Details

#eachObject



105
106
107
108
109
# File 'lib/textutils/reader/fixture_reader.rb', line 105

def each
  @ary.each do |fixture|
    yield( fixture )
  end
end

#plain_text_reader(text) ⇒ Object

find a better name - just read?



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/textutils/reader/fixture_reader.rb', line 64

def plain_text_reader( text )     ## find a better name - just read?

  ## use LineReader ?? for (re)use of comment processing - why? why not???
  ### build up array for fixtures from hash
  ary = []

  text.each_line do |line|

    # comments allow:
    # 1) #####  (shell/ruby style)
    # 2) --  comment here (haskel/?? style)
    # 3) % comment here (tex/latex style)

    if line =~ /^\s*#/ || line =~ /^\s*--/ || line =~ /^\s*%/
      # skip komments and do NOT copy to result (keep comments secret!)
      logger.debug 'skipping comment line'
      next
    end

    if line =~ /^\s*$/ 
      # skip blank lines
      logger.debug 'skipping blank line'
      next
    end

    # pass 1) remove possible trailing eol comment
    ##  e.g    -> nyc, New York   # Sample EOL Comment Here (with or without commas,,,,)
    ## becomes -> nyc, New York

    line = line.sub( /\s+#.+$/, '' )

    # pass 2) remove leading and trailing whitespace
    
    line = line.strip
 
    ary << line
  end # each lines
  ary  # return fixture ary
end