Class: Helium::Console::Formatter

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

Defined Under Namespace

Classes: LazyStringEvaluator

Constant Summary collapse

DEFAULT_STYLES =
{
  1 => [:full, {}],
  2 => [:partial, {}],
  3 => [:partial, { max_lines: 1 }]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, style, console, **options) ⇒ Formatter

Returns a new instance of Formatter.



24
25
26
27
28
29
# File 'lib/helium/console/formatter.rb', line 24

def initialize(object, style, console, **options)
  @object = object
  @options = options
  @style = style
  @console = console
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



85
86
87
88
89
# File 'lib/helium/console/formatter.rb', line 85

def method_missing(name, *args)
  return @options[name] if @options.key?(name)

  super
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



61
62
63
# File 'lib/helium/console/formatter.rb', line 61

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



61
62
63
# File 'lib/helium/console/formatter.rb', line 61

def options
  @options
end

Instance Method Details

#build_key_value(&block) ⇒ Object



95
96
97
98
99
# File 'lib/helium/console/formatter.rb', line 95

def build_key_value(&block)
  key_value = KeyValue.new
  block.(key_value)
  key_value
end

#callObject



31
32
33
34
# File 'lib/helium/console/formatter.rb', line 31

def call
  method_name = [:render, @style].compact.join('_')
  public_send(method_name)
end

#format(other_object, style = nil, **options) ⇒ Object



67
68
69
# File 'lib/helium/console/formatter.rb', line 67

def format(other_object, style = nil, **options)
  @console.format(other_object, style, **nested_opts(options, increase_level: false))
end

#format_key_value(key_value, **options) ⇒ Object



101
102
103
# File 'lib/helium/console/formatter.rb', line 101

def format_key_value(key_value, **options)
  key_value.as_table(level: level, console: @console, max_width: max_width, **options)
end

#format_nested(other_object, style = nil, **options) ⇒ Object



63
64
65
# File 'lib/helium/console/formatter.rb', line 63

def format_nested(other_object, style = nil, **options)
  @console.format(other_object, style, **nested_opts(options))
end

#format_string(string, **options) ⇒ Object



71
72
73
# File 'lib/helium/console/formatter.rb', line 71

def format_string(string, **options)
  @console.format_string(string, **options)
end

#optimal_formatObject



36
37
38
# File 'lib/helium/console/formatter.rb', line 36

def optimal_format
  DEFAULT_STYLES.fetch(level) { [:compact, {}] }
end

#renderObject



40
41
42
43
# File 'lib/helium/console/formatter.rb', line 40

def render
  style, options = optimal_format
  format(object, style, **options)
end

#render_compactObject

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/helium/console/formatter.rb', line 57

def render_compact
  raise NotImplementedError
end

#render_fullObject



45
46
47
# File 'lib/helium/console/formatter.rb', line 45

def render_full
  render_partial
end

#render_inlineObject



53
54
55
# File 'lib/helium/console/formatter.rb', line 53

def render_inline
  render_compact
end

#render_partialObject



49
50
51
# File 'lib/helium/console/formatter.rb', line 49

def render_partial
  format_string(render_inline, max_width: max_width, indent: indent)
end

#respond_to_missing?(name, private = false) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/helium/console/formatter.rb', line 91

def respond_to_missing?(name, private = false)
  @options.key?(name) || super
end

#simple?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/helium/console/formatter.rb', line 75

def simple?
  false
end