Class: OsCtl::Lib::Cli::Presentable
- Inherits:
-
Object
- Object
- OsCtl::Lib::Cli::Presentable
- Includes:
- Comparable
- 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.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/libosctl/cli/presentable.rb', line 28 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
21 22 23 |
# File 'lib/libosctl/cli/presentable.rb', line 21 def exported @exported end |
#formatted ⇒ Object (readonly)
Return the formatted value for presentation
18 19 20 |
# File 'lib/libosctl/cli/presentable.rb', line 18 def formatted @formatted end |
#raw ⇒ Object (readonly)
Return the raw, precise value
15 16 17 |
# File 'lib/libosctl/cli/presentable.rb', line 15 def raw @raw end |
Instance Method Details
#coerce(other) ⇒ Object
49 50 51 |
# File 'lib/libosctl/cli/presentable.rb', line 49 def coerce(other) [other, raw] end |
#round ⇒ Object
Forward ‘round` call to the raw value
64 65 66 |
# File 'lib/libosctl/cli/presentable.rb', line 64 def round(*) raw.round(*) end |
#to_json ⇒ Object
Returns the raw value in JSON
59 60 61 |
# File 'lib/libosctl/cli/presentable.rb', line 59 def to_json(*) exported.to_json(*) end |
#to_s ⇒ Object
Returns the formatted value
54 55 56 |
# File 'lib/libosctl/cli/presentable.rb', line 54 def to_s formatted end |