Class: Yay::Application

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

Overview

this class acts as controller for yay. it interprets user input and coordinates with the parser

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

create an application with all the necessary user context

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/yay/application.rb', line 20

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

with the –dump command we can see the resulting rules created by the rule definitions (from all files included too)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/yay/application.rb', line 86

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

run the application. when this ends, termination is expected



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/yay/application.rb', line 34

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
    # allow restricted commands such as installation of new scripts
@parser.allow_restricted = true
    @parser.parse_array(@args)
    @rules = @parser.get_rules

    # the parser may instruct us to shut down
    if @parser.shutdown
      return
    end

    @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

  # provide readable text for internal errors
  rescue Yay::Error => error
    @error.puts "#{ColourWheel::fail}#{error.printable_message}#{ColourWheel::end_colour}"

  # catch ctrl+c and similar events
  rescue Interrupt
  end
  
  # emit the end colour just in case we were interrupted
  print ColourWheel.end_colour
end