Class: GitHooker::Action

Inherits:
Object
  • Object
show all
Includes:
TerminalColors
Defined in:
lib/githooker/action.rb

Defined Under Namespace

Classes: DSL

Constant Summary

Constants included from TerminalColors

TerminalColors::MARK_FAILURE, TerminalColors::MARK_SUCCESS, TerminalColors::MARK_UNKNOWN, TerminalColors::NORMAL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TerminalColors

#color

Constructor Details

#initialize(title, phase, &block) ⇒ Action

Returns a new instance of Action.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/githooker/action.rb', line 11

def initialize(title, phase, &block)
  raise ArgumentError, "Missing required block for Action#new" unless block_given?

  phase = phase.to_s.to_sym
  unless GitHooker::VALID_PHASES.include? phase
    raise ArgumentError, "Phase must be one of #{GitHooker::VALID_PHASES.join(', ')}"
  end

  @title    = title.to_s.titleize
  @success  = true
  @phase    = phase || :any
  @errors   = []
  @warnings = []
  @action   = DSL.new(block)

  waiting!
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/githooker/action.rb', line 9

def errors
  @errors
end

#phaseObject (readonly)

Returns the value of attribute phase.



9
10
11
# File 'lib/githooker/action.rb', line 9

def phase
  @phase
end

#successObject (readonly)

Returns the value of attribute success.



9
10
11
# File 'lib/githooker/action.rb', line 9

def success
  @success
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/githooker/action.rb', line 9

def title
  @title
end

#warningsObject (readonly)

Returns the value of attribute warnings.



9
10
11
# File 'lib/githooker/action.rb', line 9

def warnings
  @warnings
end

Instance Method Details

#colored_titleObject



29
30
31
# File 'lib/githooker/action.rb', line 29

def colored_title()
  status_colorize title
end

#finished!Object



41
# File 'lib/githooker/action.rb', line 41

def finished!() @status = :finished; end

#finished?Boolean

Returns:

  • (Boolean)


40
# File 'lib/githooker/action.rb', line 40

def finished?() @status == :finished; end

#runObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/githooker/action.rb', line 53

def run
  warnings, errors = StringIO.new, StringIO.new
  begin
    running!
    $stdout, $stderr = warnings, errors
    @success &= @action.call
    return @success
  ensure
    @errors = errors.tap {|e| e.rewind}.read.split(/\n(?:[\t ]*)/)
    @warnings = warnings.tap {|w| w.rewind}.read.split(/\n(?:[\t ]*)/)
    $stdout, $stderr = STDOUT, STDERR
    finished!
  end
end

#running!Object



44
# File 'lib/githooker/action.rb', line 44

def running!() @status = :running; end

#running?Boolean

Returns:

  • (Boolean)


43
# File 'lib/githooker/action.rb', line 43

def running?() @status == :running; end

#state_symbolObject



33
34
35
36
# File 'lib/githooker/action.rb', line 33

def state_symbol
  symbol = finished? ? (success? ? "✓" : 'X') : '?'
  status_colorize symbol
end

#status_colorize(text) ⇒ Object



49
50
51
# File 'lib/githooker/action.rb', line 49

def status_colorize(text)
  finished? ? (success? ? bright_green(text) : bright_red(text)) : dark_cyan(text)
end

#success?Boolean

Returns:

  • (Boolean)


38
# File 'lib/githooker/action.rb', line 38

def success?() @success; end

#waiting!Object



47
# File 'lib/githooker/action.rb', line 47

def waiting!() @status = :waiting; end

#waiting?Boolean

Returns:

  • (Boolean)


46
# File 'lib/githooker/action.rb', line 46

def waiting?() @status == :waiting; end