Class: RipperTags::DataReader
- Inherits:
-
Object
- Object
- RipperTags::DataReader
- Defined in:
- lib/ripper-tags/data_reader.rb
Constant Summary collapse
- READ_MODE =
'r:utf-8'
Instance Attribute Summary collapse
-
#error_count ⇒ Object
readonly
Returns the value of attribute error_count.
-
#file_count ⇒ Object
readonly
Returns the value of attribute file_count.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #debug_dump(obj) ⇒ Object
- #each_tag ⇒ Object
- #file_finder ⇒ Object
-
#initialize(options) ⇒ DataReader
constructor
A new instance of DataReader.
- #normalize_encoding(str) ⇒ Object
- #parse_file(contents, filename) ⇒ Object
- #read_file(filename) ⇒ Object
- #tag_extractor(file) ⇒ Object
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 = @file_count = 0 @error_count = 0 end |
Instance Attribute Details
#error_count ⇒ Object (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_count ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
118 119 120 |
# File 'lib/ripper-tags/data_reader.rb', line 118 def @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_tag ⇒ Object
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 .verbose extractor = tag_extractor(file) rescue => err # print the error and continue $stderr.puts "#{err.class.name} parsing `#{file}': #{err.}" @error_count += 1 else extractor..each do |tag| yield tag end ensure @file_count += 1 end end end |
#file_finder ⇒ Object
128 129 130 |
# File 'lib/ripper-tags/data_reader.rb', line 128 def file_finder FileFinder.new() 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 .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 |