Class: Masticate::Base
- Inherits:
-
Object
- Object
- Masticate::Base
- Defined in:
- lib/masticate/base.rb
Direct Known Subclasses
Cook, Csvify, Datify, Exclude, Gsubber, Include, MaxRows, Mender, Plucker, Relabel, Sniffer, Transform
Instance Attribute Summary collapse
-
#csv_options ⇒ Object
readonly
Returns the value of attribute csv_options.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#input_count ⇒ Object
readonly
Returns the value of attribute input_count.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#output_count ⇒ Object
readonly
Returns the value of attribute output_count.
Instance Method Summary collapse
- #emit(line) ⇒ Object
- #execute(opts) ⇒ Object
- #get ⇒ Object
-
#initialize(args) ⇒ Base
constructor
A new instance of Base.
- #standard_options(opts) ⇒ Object
- #with_input ⇒ Object
Constructor Details
#initialize(args) ⇒ Base
Returns a new instance of Base.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/masticate/base.rb', line 7 def initialize(args) case args when String, nil @filename = args when Hash configure(args) else raise "invalid initialization: #{args}" end end |
Instance Attribute Details
#csv_options ⇒ Object (readonly)
Returns the value of attribute csv_options.
5 6 7 |
# File 'lib/masticate/base.rb', line 5 def @csv_options end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
2 3 4 |
# File 'lib/masticate/base.rb', line 2 def filename @filename end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
3 4 5 |
# File 'lib/masticate/base.rb', line 3 def input @input end |
#input_count ⇒ Object (readonly)
Returns the value of attribute input_count.
4 5 6 |
# File 'lib/masticate/base.rb', line 4 def input_count @input_count end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
3 4 5 |
# File 'lib/masticate/base.rb', line 3 def output @output end |
#output_count ⇒ Object (readonly)
Returns the value of attribute output_count.
4 5 6 |
# File 'lib/masticate/base.rb', line 4 def output_count @output_count end |
Instance Method Details
#emit(line) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/masticate/base.rb', line 38 def emit(line) @output_count ||= 0 @output_count += 1 begin case line when Array @output.puts line.to_csv else @output.puts line end rescue Errno::EPIPE # output was closed, e.g. ran piped into `head` # silently ignore this condition, it's not fatal and doesn't need a warning end end |
#execute(opts) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/masticate/base.rb', line 63 def execute(opts) configure(opts) @output_count = 0 with_input do |input| while line = get row = CSV.parse_line(line, ) #.map {|s| s && s.strip} if row row = row.map {|s| s && s.strip} end if self.class == Masticate::Mender output = crunch(row, line, ) else output = crunch(row) end emit(output) if output end end crunch(nil) {|row| emit(row)} @output.close if opts[:output] { :input_count => input_count, :output_count => @output_count, :headers => @headers } end |
#get ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/masticate/base.rb', line 26 def get line = @input.gets return nil if line.nil? line.chomp! if line.empty? get else @input_count += 1 line end end |
#standard_options(opts) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/masticate/base.rb', line 54 def (opts) @output = opts[:output] ? File.open(opts[:output], "w") : $stdout @csv_options = {} @csv_options[:col_sep] = opts[:col_sep] if opts[:col_sep] if opts[:col_sep] @csv_options[:quote_char] = opts[:quote_char] || "\0" end end |
#with_input ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/masticate/base.rb', line 18 def with_input @input = @filename ? open(@filename) : $stdin @input_count = 0 result = yield @input @input.close if @filename result end |