Class: IParty::CLI::Formatter

Inherits:
Object
  • Object
show all
Includes:
Colorize
Defined in:
lib/iparty/cli/formatter.rb

Defined Under Namespace

Classes: ConsolePretty, JsonFormatter, NoOutput, StringFormatter

Constant Summary collapse

VOID_OUTPUT =
false

Constants included from Colorize

Colorize::COLORMAP

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colorize

#colorize, #decolorize, #with_color

Constructor Details

#initialize(app, **opts) ⇒ Formatter

Returns a new instance of Formatter.



30
31
32
33
34
35
# File 'lib/iparty/cli/formatter.rb', line 30

def initialize(app, **opts)
  @app = app
  @opts = { colorize: true }.merge(opts)

  setup if respond_to?(:setup)
end

Class Attribute Details

.idObject



15
16
17
# File 'lib/iparty/cli/formatter.rb', line 15

def id
  @id || name.split("::").last
end

Class Method Details

.descendants(of: self) ⇒ Object



19
20
21
# File 'lib/iparty/cli/formatter.rb', line 19

def descendants of: self
  of.subclasses.flat_map{ [_1] + descendants(of: _1) }
end

.find_by_id(input, of: self) ⇒ Object



23
24
25
26
27
# File 'lib/iparty/cli/formatter.rb', line 23

def find_by_id input, of: self
  descendants.detect do |fmt|
    fmt.id === input # rubocop:disable Style/CaseEquality -- deliberate to support regex/proc also
  end
end

Instance Method Details

#colorize?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/iparty/cli/formatter.rb', line 37

def colorize?
  @opts[:colorize]
end

#format(ip, index: 0, **kw, &to_data) ⇒ Object



45
46
47
# File 'lib/iparty/cli/formatter.rb', line 45

def format ip, index: 0, **kw, &to_data
  to_data.call(ip)
end

#format_all(ips, base_index: 0, **kw, &to_data) ⇒ Object



41
42
43
# File 'lib/iparty/cli/formatter.rb', line 41

def format_all ips, base_index: 0, **kw, &to_data
  ips.map.with_index {|ip, index| format(ip, index: base_index + index, **kw, &to_data) }
end