Class: Mats::Stream
- Inherits:
-
Object
- Object
- Mats::Stream
- Defined in:
- lib/mats.rb
Instance Attribute Summary collapse
-
#optpsr ⇒ Object
Returns the value of attribute optpsr.
Instance Method Summary collapse
- #exec(obj) ⇒ Object
-
#initialize ⇒ Stream
constructor
A new instance of Stream.
- #read_io(io) ⇒ Object
- #read_stream(files = nil) ⇒ Object
- #run(cmd_argv, range = nil) ⇒ Object
- #setup(argv) ⇒ Object
- #teardown ⇒ Object
- #write_stream(obj) ⇒ Object
Constructor Details
#initialize ⇒ Stream
Returns a new instance of Stream.
10 11 12 13 14 15 |
# File 'lib/mats.rb', line 10 def initialize @out_encode = false @optpsr = OptionParser.new @optpsr.on('-m', 'output as msgpack encode') { |v| @out_encode = true } @argv = [] end |
Instance Attribute Details
#optpsr ⇒ Object
Returns the value of attribute optpsr.
8 9 10 |
# File 'lib/mats.rb', line 8 def optpsr @optpsr end |
Instance Method Details
#exec(obj) ⇒ Object
80 81 82 |
# File 'lib/mats.rb', line 80 def exec(obj) raise 'exec(obj) must be implemented' end |
#read_io(io) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mats.rb', line 31 def read_io(io) u = MessagePack::Unpacker.new(io) begin u.each {|obj| exec(obj) } rescue EOFError # ignore rescue Interrupt return end end |
#read_stream(files = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mats.rb', line 42 def read_stream(files=nil) if files == nil or files.size == 0 read_io(STDIN) else f_list = Array.new if files.instance_of? String f_list << files else f_list += files end f_list.each do |fpath| if File.directory?(fpath) Find.find(fpath) do |file| next if File.directory?(file) read_io(File.open(file, 'r')) end else read_io(File.open(fpath, 'r')) end end end end |
#run(cmd_argv, range = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mats.rb', line 17 def run(cmd_argv, range = nil) args = @optpsr.parse(cmd_argv) unless range.nil? raise "Not enough arguments" if args.size < range.last + 1 @argv = args.slice!(range) end setup(@argv) read_stream(args) teardown end |
#setup(argv) ⇒ Object
76 77 78 |
# File 'lib/mats.rb', line 76 def setup(argv) raise 'setup(argv) must be implemented' if argv.size > 0 end |
#teardown ⇒ Object
84 |
# File 'lib/mats.rb', line 84 def teardown; end |
#write_stream(obj) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/mats.rb', line 66 def write_stream(obj) if @out_encode == false pp obj else STDOUT.write(obj.to_msgpack) end rescue Errno::EPIPE => e ; end |