Class: Yay::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/yay/application.rb

Constant Summary collapse

DO_NOTHING_ARG =

arg that can be used if you don’t want the application to do anything

'--do-nothing'
DUMP_RULES_ARG =

arg that can be used if you want to dump the rules instead of using them

'--dump'
SHOW_VERSION_ARG =

arg that can be used to dump the version and exit

'--version'

Instance Method Summary collapse

Constructor Details

#initialize(input, output, error, args) ⇒ Application

Returns a new instance of Application.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yay/application.rb', line 17

def initialize(input, output, error, args)
  raise ArgumentError, "input" unless input.kind_of? IO
  raise ArgumentError, "output" unless output.kind_of? IO
  raise ArgumentError, "error" unless error.kind_of? IO
  raise ArgumentError, "args" unless args.kind_of? Array

  @input   = input
  @output  = output
  @error   = error
  @args    = args
  @running = false
end

Instance Method Details

#dump_colours(line_rules, word_rules) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yay/application.rb', line 67

def dump_colours line_rules, word_rules
 
  puts "line rules:" if line_rules
  
  line_rules.each { |rule| 
    puts "#{rule[0]} => #{rule[1].dump}"
  }
  
  puts "word rules:" if word_rules
  
  word_rules.each { |rule|
    puts "#{rule[0]} => #{rule[1].dump}"
  }
end

#runObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yay/application.rb', line 30

def run
  raise "already running" if @running
  @running = true
  
  preArg = @args[0]
  @args.shift if preArg == DO_NOTHING_ARG || 
                 preArg == DUMP_RULES_ARG ||
                 preArg == SHOW_VERSION_ARG
  
  if preArg == SHOW_VERSION_ARG
    puts Yay::version
    exit
  end
  
  return if preArg == DO_NOTHING_ARG
  
  begin      
  
    @parser = Yay::Parser.new
@parser.allow_all = true
    @parser.parse_array(@args)
    @rules = @parser.get_rules

    @colourizer = Yay::Colourizer.new @rules, @input, @output
    
    if preArg == DUMP_RULES_ARG
      dump_colours @colourizer.line_rules, @colourizer.part_rules
      return
    end

    @colourizer.colourize_pipe
  rescue Yay::Error => error
    @error.puts error.printable_message
  rescue Interrupt
  end
end