Class: Helium::Console::Formatter
- Inherits:
-
Object
- Object
- Helium::Console::Formatter
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
-
#build_key_value(&block) ⇒ Object
-
#call ⇒ Object
-
#format(other_object, style = nil, **options) ⇒ Object
-
#format_key_value(key_value, **options) ⇒ Object
-
#format_nested(other_object, style = nil, **options) ⇒ Object
-
#format_string(string, **options) ⇒ Object
-
#initialize(object, style, console, **options) ⇒ Formatter
constructor
A new instance of Formatter.
-
#method_missing(name, *args) ⇒ Object
-
#optimal_format ⇒ Object
-
#render ⇒ Object
-
#render_compact ⇒ Object
-
#render_full ⇒ Object
-
#render_inline ⇒ Object
-
#render_partial ⇒ Object
-
#respond_to_missing?(name, private = false) ⇒ Boolean
-
#simple? ⇒ Boolean
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
#object ⇒ Object
Returns the value of attribute object.
61
62
63
|
# File 'lib/helium/console/formatter.rb', line 61
def object
@object
end
|
#options ⇒ Object
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
|
#call ⇒ Object
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
|
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
|
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
|
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
|
71
72
73
|
# File 'lib/helium/console/formatter.rb', line 71
def format_string(string, **options)
@console.format_string(string, **options)
end
|
36
37
38
|
# File 'lib/helium/console/formatter.rb', line 36
def optimal_format
DEFAULT_STYLES.fetch(level) { [:compact, {}] }
end
|
#render ⇒ Object
40
41
42
43
|
# File 'lib/helium/console/formatter.rb', line 40
def render
style, options = optimal_format
format(object, style, **options)
end
|
#render_compact ⇒ Object
57
58
59
|
# File 'lib/helium/console/formatter.rb', line 57
def render_compact
raise NotImplementedError
end
|
#render_full ⇒ Object
45
46
47
|
# File 'lib/helium/console/formatter.rb', line 45
def render_full
render_partial
end
|
#render_inline ⇒ Object
53
54
55
|
# File 'lib/helium/console/formatter.rb', line 53
def render_inline
render_compact
end
|
#render_partial ⇒ Object
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
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
75
76
77
|
# File 'lib/helium/console/formatter.rb', line 75
def simple?
false
end
|