Class: Abak::Flow::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/abak-flow/visitor.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Visitor

Returns a new instance of Visitor.



6
7
8
9
10
11
12
13
14
15
# File 'lib/abak-flow/visitor.rb', line 6

def initialize(*args)
  options = args.pop if args.last.is_a?(Hash)

  @objects = args
  @call = options.fetch(:call)
  @inspect = options.fetch(:inspect)
  @command = options.fetch(:command, "default")

  @asked = false
end

Instance Method Details

#exit_on_fail(command, code = 1) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/abak-flow/visitor.rb', line 40

def exit_on_fail(command, code = 1)
  return if ready?

  say ANSI.red { I18n.t("commands.#{command}.fail") }
  say ANSI.yellow { output }

  exit(code)
end

#on_fail(options = Hash.new, &block) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/abak-flow/visitor.rb', line 49

def on_fail(options = Hash.new, &block)
  return if ready?

  say ANSI.red { I18n.t("commands.#{@command}.fail") }
  say ANSI.yellow { output }

  exit(options[:exit]) if options.key?(:exit)
end

#outputObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/abak-flow/visitor.rb', line 24

def output
  ready? unless asked?

  @objects.map do |o|
    next if o.send(@inspect).empty?

    info = ""
    name = o.respond_to?(:display_name) ? o.display_name : o.class.name
    o.send(@inspect).each_with_index do |inf, idx|
      info << "\n  #{idx + 1}. #{inf}"
    end

    "\n#{name}#{info}"
  end * "\n"
end

#ready?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/abak-flow/visitor.rb', line 17

def ready?
  @asked = true

  ready = @objects.map { |o| o.send(@call) }.uniq
  ready.size == 1 && ready.first
end