Class: Charisma::Curator::Curation

Inherits:
Delegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/charisma/curator/curation.rb

Overview

A wrapper around computed characteristic values that intervenes when the value is asked to present itself.

Implements the Delegator pattern, with uncaught methods delegated to the characteristic’s computed value. (Undocumented below is that #to_s is specifically delegated to #render.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, characteristic = nil) ⇒ Curation

Create new Curation wrapper.

This is typically done by the instance’s Charisma::Curator

Parameters:

  • value

    The computed value of the characteristic for the instance

  • characteristic (Charisma::Characteristic, optional) (defaults to: nil)

    The associated characteristic definition



23
24
25
26
27
# File 'lib/charisma/curator/curation.rb', line 23

def initialize(value, characteristic = nil)
  @characteristic = characteristic
  establish_units_methods
  self.value = value
end

Instance Attribute Details

#characteristicCharisma::Characteristic (readonly)

The characteristic, as defined in the class’s characterization



16
17
18
# File 'lib/charisma/curator/curation.rb', line 16

def characteristic
  @characteristic
end

#valueObject

The computed value of the characteristic



12
13
14
# File 'lib/charisma/curator/curation.rb', line 12

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
34
35
# File 'lib/charisma/curator/curation.rb', line 31

def ==(other)
  a = self.value
  b = other.respond_to?(:value) ? other.value : other
  a == b
end

#__getobj__Object

Delegator method



47
# File 'lib/charisma/curator/curation.rb', line 47

def __getobj__; value end

#__setobj__(obj) ⇒ Object

Delegator method



50
# File 'lib/charisma/curator/curation.rb', line 50

def __setobj__(obj); self.value = obj end

#as_jsonObject

Render a JSON-like object for later conversion to JSON



61
62
63
64
65
66
67
68
69
# File 'lib/charisma/curator/curation.rb', line 61

def as_json(*)
  if characteristic.measurement
    characteristic.measurement_class.new(value).as_json
  elsif value.respond_to? :as_json
    value.as_json
  else
    value
  end
end

#inspectObject

An inspection method for more readable IRB sessions and logs.



38
39
40
41
42
43
44
# File 'lib/charisma/curator/curation.rb', line 38

def inspect
  if characteristic
    "<Charisma::Curator::Curation for :#{characteristic.name} (use #to_s to render value of #{value.class})>"
  else
    "<Charisma::Curator::Curation for undefined characteristic (use #to_s to render value of #{value.class})>"
  end          
end

#uObject



56
57
58
# File 'lib/charisma/curator/curation.rb', line 56

def u
  characteristic.try(:measurement_class).try(:unit_abbreviation)
end

#unitsObject



52
53
54
# File 'lib/charisma/curator/curation.rb', line 52

def units
  characteristic.try(:measurement_class).try(:unit)
end