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:



19
20
21
22
23
24
25
26
27
# File 'lib/charisma/curator.rb', line 19

def initialize(subject)
  @characteristics = {}.extend LooseEquality
  @subject = subject
  subject.class.characterization.keys.each do |key|
    if subject.respond_to?(key) and value = subject.send(key)
      self[key] = value
    end
  end
end

Instance Attribute Details

#characteristicsObject (readonly)

The hashed wrapped by the curator that actually stores the computed characteristics’



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

def characteristics
  @characteristics
end

#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



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

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

#inspectObject

A custom inspection method to enhance IRB and log readability



37
38
39
# File 'lib/charisma/curator.rb', line 37

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

#to_sObject



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

def to_s; inspect end