Class: Humr::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/humr/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
# File 'lib/humr/runner.rb', line 14

def initialize(args)
  @args = args
  @config = Config.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/humr/runner.rb', line 12

def config
  @config
end

Class Method Details

.bootstrap(args) ⇒ Object



19
20
21
# File 'lib/humr/runner.rb', line 19

def self.bootstrap(args)
  new(args).run
end

Instance Method Details

#colorize(chunk, handler) ⇒ Object



63
64
65
# File 'lib/humr/runner.rb', line 63

def colorize(chunk, handler)
  Term::ANSIColor.send(config.color(handler), chunk)
end

#handlersObject



27
28
29
30
31
# File 'lib/humr/runner.rb', line 27

def handlers
  @handlers ||= config.handlers.map do |name|
    Handler[name].new
  end
end

#readable_field(field) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/humr/runner.rb', line 52

def readable_field(field)
  handlers.each do |handler|
    readable = handler.replace(field) do |chunk|
      colorize(chunk, handler.name)
    end
    return readable if readable
  end

  field
end

#readable_line(line) ⇒ Object



46
47
48
49
50
# File 'lib/humr/runner.rb', line 46

def readable_line(line)
  splitter.sub_each_field(line) do |field|
    readable_field field
  end
end

#runObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/humr/runner.rb', line 33

def run
  OptionParser.new do |opts|
    opts.on('-s', '--splitter SPLITTER[:ARGS]', 'Specify ield splitter (default, pattern:re, ltsv)') do |splitter|
      impl, *args = splitter.split(/:/, 2)
      @splitter = Splitter::Impl[impl.to_sym].new(*args)
    end
  end.parse!(@args)

  STDIN.each_line do |line|
    puts readable_line(line.chomp)
  end
end

#splitterObject



23
24
25
# File 'lib/humr/runner.rb', line 23

def splitter
  @splitter ||= Splitter::Default.new
end