Class: OsCtl::Lib::Cli::Presentable

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(raw, **opts) ⇒ Presentable

Returns a new instance of Presentable.

Parameters:

  • raw (any)

    precise value

  • opts (Hash)

Options Hash (**opts):

  • presenter (Method, Proc)

    called to format the value

  • formatted (String)

    formatted value

  • exported (any)

    value used for dump to JSON



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

#exportedObject (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

#formattedObject (readonly)

Return the formatted value for presentation



18
19
20
# File 'lib/libosctl/cli/presentable.rb', line 18

def formatted
  @formatted
end

#rawObject (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

#roundObject

Forward round call to the raw value



64
65
66
# File 'lib/libosctl/cli/presentable.rb', line 64

def round(*)
  raw.round(*)
end

#to_jsonObject

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_sObject

Returns the formatted value



54
55
56
# File 'lib/libosctl/cli/presentable.rb', line 54

def to_s
  formatted
end