Class: Charisma::Curator

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

Overview

A Hash-like object that stores computed characteristics about an instance of a characterized class.

Every instance of a characterized class gets a Curator, accessed via #characteristics.

Defined Under Namespace

Modules: LooseEquality Classes: Curation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ Curator

Create a Curator.

Typically this is done automatically when #characteristics is called on an instance of a characterized class for the first time.

Parameters:

  • The (Object)

    subject of the curation – an instance of a characterized class

See Also:



16
17
18
19
20
21
22
23
24
# File 'lib/charisma/curator.rb', line 16

def initialize(subject)
  @subject = subject
  subject.class.characterization.keys.each do |key|
    if subject.respond_to?(key)
      value = subject.send(key)
      self[key] = value unless value.nil?
    end
  end
end

Instance Attribute Details

#subjectObject (readonly)

The curator’s subject–the instance itself’



9
10
11
# File 'lib/charisma/curator.rb', line 9

def subject
  @subject
end

Instance Method Details

#[]=(key, value) ⇒ Object

Store a late-defined characteristic, with a Charisma wrapper.

Parameters:

  • key (Symbol)

    The name of the characteristic, which must match one defined in the class’s characterization

  • value

    The value of the characteristic



42
43
44
# File 'lib/charisma/curator.rb', line 42

def []=(key, value)
  characteristics[key] = Curation.new value, subject.class.characterization[key]
end

#characteristicsObject

The special hash wrapped by the curator that actually stores the computed characteristics.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/charisma/curator.rb', line 27

def characteristics
  return @characteristics if @characteristics

  hsh = Hash.new do |_, key|
    if characterization = subject.class.characterization[key]
      Curation.new nil, characterization
    end
  end
  hsh.extend LooseEquality
  @characteristics = hsh
end

#dupCharisma::Curator

Provide a shallow copy.

Returns:



68
69
70
71
72
# File 'lib/charisma/curator.rb', line 68

def dup
  shallow = super
  shallow.instance_variable_set :@characteristics, characteristics.dup
  shallow
end

#inspectObject

A custom inspection method to enhance IRB and log readability



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

def inspect
  "<Charisma:Curator #{keys.length} known characteristic(s)>"
end

#to_hashHash

Provide a hash of the plain values (dropping presentation information).

Previous versions of leap returned a hash of display-friendly representations; this was rather surprising and not especially useful.

Returns:

  • (Hash)


58
59
60
61
62
63
# File 'lib/charisma/curator.rb', line 58

def to_hash
  characteristics.inject({}) do |memo, (k, v)|
    memo[k] = v.value
    memo
  end
end

#to_sObject



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

def to_s; inspect end