Class: OutputMode::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/output_mode/formatter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*objects, verbose: nil, ascii: nil, humanize: nil, color: nil) ⇒ Formatter

Returns a new instance of Formatter.



39
40
41
42
43
44
45
46
47
48
# File 'lib/output_mode/formatter.rb', line 39

def initialize(*objects, verbose: nil, ascii: nil, humanize: nil, color: nil)
  @verbose = verbose
  @ascii = ascii
  @humanize = humanize
  @color = color

  # NOTE: This is intentionally not exposed on the base class
  #       It is up to the individual implementations to expose it
  @objects = objects
end

Class Method Details

.build(*objects, **opts) ⇒ Object



31
32
33
# File 'lib/output_mode/formatter.rb', line 31

def self.build(*objects, **opts)
  new(*objects, **opts).tap(&:register_all)
end

.render(*objects, **opts) ⇒ Object



35
36
37
# File 'lib/output_mode/formatter.rb', line 35

def self.render(*objects, **opts)
  build(*objects, **opts).render
end

Instance Method Details

#ascii?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/output_mode/formatter.rb', line 107

def ascii?
  if @ascii.nil?
    !humanize?
  else
    @ascii
  end
end

#build_outputObject

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/output_mode/formatter.rb', line 81

def build_output
  raise NotImplementedError
end

#callablesObject



54
55
56
# File 'lib/output_mode/formatter.rb', line 54

def callables
  @callables ||= Callables.new
end

#color?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
# File 'lib/output_mode/formatter.rb', line 97

def color?
  if @color.nil? && (ascii? || !humanize?)
    false
  elsif @color.nil?
    TTY::Color.color?
  else
    @color
  end
end

#default(value = nil) ⇒ Object



133
134
135
136
# File 'lib/output_mode/formatter.rb', line 133

def default(value = nil)
  @default = value unless value.nil?
  @default ? @default : (humanize? ? '(none)' : '')
end

#format(value, **config) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/output_mode/formatter.rb', line 65

def format(value, **config)
  case value
  when TrueClass
    config[:yes] || yes
  when FalseClass
    config[:no] || no
  when NilClass
    config[:default] || default
  when Time
    format = config[:time] || time
    value.strftime(format)
  else
    value
  end
end

#humanize?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/output_mode/formatter.rb', line 89

def humanize?
  if @humanize.nil?
    $stdout.tty?
  else
    @humanize
  end
end

#no(value = nil) ⇒ Object



128
129
130
131
# File 'lib/output_mode/formatter.rb', line 128

def no(value = nil)
  @no = value unless value.nil?
  @no ? @no : (ascii? ? 'no' : '✕')
end

#register(**config, &block) ⇒ Object



58
59
60
61
62
63
# File 'lib/output_mode/formatter.rb', line 58

def register(**config, &block)
  callables << Callable.new(**config) do |*args, **opts|
    value = block.call(*args, **opts)
    format(value, **config)
  end
end

#register_allObject

Override in sub-classes to define the attributes



51
52
# File 'lib/output_mode/formatter.rb', line 51

def register_all
end

#renderObject



85
86
87
# File 'lib/output_mode/formatter.rb', line 85

def render
  build_output.render(*@objects)
end

#time(value = nil) ⇒ Object



138
139
140
141
# File 'lib/output_mode/formatter.rb', line 138

def time(value = nil)
  @time = value unless value.nil?
  @time ? @time : (verbose? ? "%Y-%m-%dT%H:%M:%S%:z" : "%d/%m/%y %H:%M")
end

#verbose?Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
# File 'lib/output_mode/formatter.rb', line 115

def verbose?
  if @verbose.nil?
    !humanize?
  else
    @verbose
  end
end

#yes(value = nil) ⇒ Object



123
124
125
126
# File 'lib/output_mode/formatter.rb', line 123

def yes(value = nil)
  @yes = value unless value.nil?
  @yes ? @yes : (ascii? ? 'yes' : '✓')
end