Class: RipperTags::DataReader
- Inherits:
-
Object
- Object
- RipperTags::DataReader
- Defined in:
- lib/ripper-tags/data_reader.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#read_mode ⇒ Object
Returns the value of attribute read_mode.
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.
94 95 96 97 |
# File 'lib/ripper-tags/data_reader.rb', line 94 def initialize() @options = @read_mode = defined?(::Encoding) ? 'r:utf-8' : 'r' end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
91 92 93 |
# File 'lib/ripper-tags/data_reader.rb', line 91 def @options end |
#read_mode ⇒ Object
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_tag ⇒ Object
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 .verbose extractor = tag_extractor(file) rescue => err if .force $stderr.puts "Error parsing `#{file}': #{err.}" else raise err end else extractor..each do |tag| yield tag end end end end |
#file_finder ⇒ Object
99 100 101 |
# File 'lib/ripper-tags/data_reader.rb', line 99 def file_finder FileFinder.new() 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 .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 |