Module: Sfn::Utils::Output

Defined in:
lib/sfn/utils/output.rb

Overview

Output Helpers

Instance Method Summary collapse

Instance Method Details

#get_titles(thing, args = {}) ⇒ Array<String>

Generate formatted titles

Parameters:

  • thing (Object)

    thing being processed

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :attributes (Array)

Returns:

  • (Array<String>)

    formatted titles



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sfn/utils/output.rb', line 37

def get_titles(thing, args = {})
  attrs = args[:attributes] || []
  if attrs.empty?
    hash = thing.is_a?(Array) ? thing.first : thing
    hash ||= {}
    attrs = hash.keys
  end
  titles = attrs.map do |key|
    camel(key).gsub(/([a-z])([A-Z])/, '\1 \2')
  end.compact
  if args[:format]
    titles.map { |s| @ui.color(s, :bold) }
  else
    titles
  end
end

#process(things, args = {}) ⇒ Object

TODO:

this was extracted from events and needs to be cleaned up

Process things and return items

Parameters:

  • things (Array)

    items to process

  • args (Hash) (defaults to: {})

    options

Options Hash (args):

  • :flat (TrueClass, FalseClass)

    flatten result array

  • :attributes (Array)

    attributes to extract



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sfn/utils/output.rb', line 15

def process(things, args = {})
  @event_ids ||= []
  processed = things.reverse.map do |thing|
    next if @event_ids.include?(thing["id"])
    @event_ids.push(thing["id"]).compact!
    if args[:attributes]
      args[:attributes].map do |key|
        thing[key].to_s
      end
    else
      thing.values
    end
  end
  args[:flat] ? processed.flatten : processed
end

#things_output(stack, things, what, *args) ⇒ Object

Output stack related things in nice format

Parameters:

  • stack (String)

    name of stack

  • things (Array)

    things to display

  • what (String)

    description of things for output

  • args (Symbol)

    options (:ignore_empty_output)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sfn/utils/output.rb', line 60

def things_output(stack, things, what, *args)
  unless args.include?(:no_title)
    output = get_titles(things, :format => true, :attributes => allowed_attributes)
  else
    output = []
  end
  columns = allowed_attributes.size
  output += process(things, :flat => true, :attributes => allowed_attributes)
  output.compact!
  if output.empty?
    ui.warn "No information found" unless args.include?(:ignore_empty_output)
  else
    ui.info "#{what.to_s.capitalize} for stack: #{ui.color(stack, :bold)}" if stack
    ui.info "#{ui.list(output, :uneven_columns_across, columns)}"
  end
end