Class: RipperTags::DataReader

Inherits:
Object
  • Object
show all
Defined in:
lib/ripper-tags/data_reader.rb

Constant Summary collapse

READ_MODE =
'r:utf-8'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DataReader

Returns a new instance of DataReader.



122
123
124
125
126
# File 'lib/ripper-tags/data_reader.rb', line 122

def initialize(options)
  @options = options
  @file_count = 0
  @error_count = 0
end

Instance Attribute Details

#error_countObject (readonly)

Returns the value of attribute error_count.



118
119
120
# File 'lib/ripper-tags/data_reader.rb', line 118

def error_count
  @error_count
end

#file_countObject (readonly)

Returns the value of attribute file_count.



118
119
120
# File 'lib/ripper-tags/data_reader.rb', line 118

def file_count
  @file_count
end

#optionsObject (readonly)

Returns the value of attribute options.



118
119
120
# File 'lib/ripper-tags/data_reader.rb', line 118

def options
  @options
end

Instance Method Details

#debug_dump(obj) ⇒ Object



168
169
170
# File 'lib/ripper-tags/data_reader.rb', line 168

def debug_dump(obj)
  pp(obj, $stderr)
end

#each_tagObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ripper-tags/data_reader.rb', line 147

def each_tag
  return to_enum(__method__) unless block_given?

  file_finder.each_file do |file|
    begin
      $stderr.puts "Parsing file `#{file}'" if options.verbose
      extractor = tag_extractor(file)
    rescue => err
      # print the error and continue
      $stderr.puts "#{err.class.name} parsing `#{file}': #{err.message}"
      @error_count += 1
    else
      extractor.tags.each do |tag|
        yield tag
      end
    ensure
      @file_count += 1
    end
  end
end

#file_finderObject



128
129
130
# File 'lib/ripper-tags/data_reader.rb', line 128

def file_finder
  FileFinder.new(options)
end

#normalize_encoding(str) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/ripper-tags/data_reader.rb', line 137

def normalize_encoding(str)
  if str.respond_to?(:encode!)
    # strip invalid byte sequences
    str.encode!('utf-16', :invalid => :replace, :undef => :replace)
    str.encode!('utf-8')
  else
    str
  end
end

#parse_file(contents, filename) ⇒ Object



172
173
174
175
176
# File 'lib/ripper-tags/data_reader.rb', line 172

def parse_file(contents, filename)
  sexp = Parser.new(contents, filename).parse
  debug_dump(sexp) if options.debug
  sexp
end

#read_file(filename) ⇒ Object



132
133
134
135
# File 'lib/ripper-tags/data_reader.rb', line 132

def read_file(filename)
  str = File.open(filename, READ_MODE) {|f| f.read }
  normalize_encoding(str)
end

#tag_extractor(file) ⇒ Object



178
179
180
181
182
183
# File 'lib/ripper-tags/data_reader.rb', line 178

def tag_extractor(file)
  file_contents = read_file(file)
  debug_dump(Ripper.sexp(file_contents)) if options.verbose_debug
  sexp = parse_file(file_contents, file)
  Visitor.new(sexp, file, file_contents)
end