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



108
109
110
# File 'lib/ripper-tags/data_reader.rb', line 108

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



104
105
106
# File 'lib/ripper-tags/data_reader.rb', line 104

def options
  @options
end

Instance Method Details

#debug_dump(obj) ⇒ Object



151
152
153
# File 'lib/ripper-tags/data_reader.rb', line 151

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

#each_tagObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ripper-tags/data_reader.rb', line 131

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
      if options.force
        $stderr.puts "Error parsing `#{file}': #{err.message}"
      else
        raise err
      end
    else
      extractor.tags.each do |tag|
        yield tag
      end
    end
  end
end

#file_finderObject



112
113
114
# File 'lib/ripper-tags/data_reader.rb', line 112

def file_finder
  FileFinder.new(options)
end

#normalize_encoding(str) ⇒ Object



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

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



155
156
157
158
159
# File 'lib/ripper-tags/data_reader.rb', line 155

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

#read_file(filename) ⇒ Object



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

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

#tag_extractor(file) ⇒ Object



161
162
163
164
165
166
# File 'lib/ripper-tags/data_reader.rb', line 161

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