Class: RipperTags::DataReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DataReader

Returns a new instance of DataReader.



94
95
96
97
# File 'lib/ripper-tags/data_reader.rb', line 94

def initialize(options)
  @options = options
  @read_mode = defined?(::Encoding) ? 'r:utf-8' : 'r'
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



91
92
93
# File 'lib/ripper-tags/data_reader.rb', line 91

def options
  @options
end

#read_modeObject

Returns the value of attribute read_mode.



92
93
94
# File 'lib/ripper-tags/data_reader.rb', line 92

def read_mode
  @read_mode
end

Instance Method Details

#debug_dump(obj) ⇒ Object



138
139
140
# File 'lib/ripper-tags/data_reader.rb', line 138

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

#each_tagObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ripper-tags/data_reader.rb', line 118

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



99
100
101
# File 'lib/ripper-tags/data_reader.rb', line 99

def file_finder
  FileFinder.new(options)
end

#normalize_encoding(str) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/ripper-tags/data_reader.rb', line 108

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



142
143
144
145
146
# File 'lib/ripper-tags/data_reader.rb', line 142

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

#read_file(filename) ⇒ Object



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

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

#tag_extractor(file) ⇒ Object



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

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