Class: OsCtl::Lib::Cli::Presentable
- Inherits:
-
Object
- Object
- OsCtl::Lib::Cli::Presentable
- Defined in:
- lib/libosctl/cli/presentable.rb
Overview
Container for values that should be treated and presented differently
Presentable is used for OutputFormatter, so that values are sorted by their precise representation, but presented as a formatted string, whose sorting would yield incorrect results.
The formatting is done either by passing a ‘presenter` callable, or the `formatted` value. If `presenter` is given, it is called to format the value, otherwise `formatted` is used.
Instance Attribute Summary collapse
-
#exported ⇒ Object
readonly
Return the value which is used for JSON representation.
-
#formatted ⇒ Object
readonly
Return the formatted value for presentation.
-
#raw ⇒ Object
readonly
Return the raw, precise value.
Instance Method Summary collapse
- #coerce(other) ⇒ Object
-
#initialize(raw, **opts) ⇒ Presentable
constructor
A new instance of Presentable.
-
#round ⇒ Object
Forward ‘round` call to the raw value.
-
#to_json ⇒ Object
Returns the raw value in JSON.
-
#to_s ⇒ Object
Returns the formatted value.
Constructor Details
#initialize(raw, **opts) ⇒ Presentable
Returns a new instance of Presentable.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/libosctl/cli/presentable.rb', line 26 def initialize(raw, **opts) @raw = raw formatted = opts[:formatted] v = opts.has_key?(:presenter) ? opts[:presenter].call(raw) : formatted @formatted = v ? v.to_s : raw.to_s @exported = opts.has_key?(:exported) ? opts[:exported] : raw end |
Instance Attribute Details
#exported ⇒ Object (readonly)
Return the value which is used for JSON representation
19 20 21 |
# File 'lib/libosctl/cli/presentable.rb', line 19 def exported @exported end |
#formatted ⇒ Object (readonly)
Return the formatted value for presentation
16 17 18 |
# File 'lib/libosctl/cli/presentable.rb', line 16 def formatted @formatted end |
#raw ⇒ Object (readonly)
Return the raw, precise value
13 14 15 |
# File 'lib/libosctl/cli/presentable.rb', line 13 def raw @raw end |
Instance Method Details
#coerce(other) ⇒ Object
47 48 49 |
# File 'lib/libosctl/cli/presentable.rb', line 47 def coerce(other) [other, raw] end |
#round ⇒ Object
Forward ‘round` call to the raw value
62 63 64 |
# File 'lib/libosctl/cli/presentable.rb', line 62 def round(*) raw.round(*) end |
#to_json ⇒ Object
Returns the raw value in JSON
57 58 59 |
# File 'lib/libosctl/cli/presentable.rb', line 57 def to_json(*) exported.to_json(*) end |
#to_s ⇒ Object
Returns the formatted value
52 53 54 |
# File 'lib/libosctl/cli/presentable.rb', line 52 def to_s formatted end |