Class: Charisma::Curator
- Inherits:
-
Object
- Object
- Charisma::Curator
- 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
-
#subject ⇒ Object
readonly
The curator’s subject–the instance itself’.
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Store a late-defined characteristic, with a Charisma wrapper.
-
#characteristics ⇒ Object
The special hash wrapped by the curator that actually stores the computed characteristics.
-
#dup ⇒ Charisma::Curator
Provide a shallow copy.
-
#initialize(subject) ⇒ Curator
constructor
Create a Curator.
-
#inspect ⇒ Object
A custom inspection method to enhance IRB and log readability.
-
#to_hash ⇒ Hash
Provide a hash of the plain values (dropping presentation information).
- #to_s ⇒ Object
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.
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
#subject ⇒ Object (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.
42 43 44 |
# File 'lib/charisma/curator.rb', line 42 def []=(key, value) characteristics[key] = Curation.new value, subject.class.characterization[key] end |
#characteristics ⇒ Object
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 |
#dup ⇒ Charisma::Curator
Provide a shallow copy.
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 |
#inspect ⇒ Object
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_hash ⇒ Hash
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.
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_s ⇒ Object
52 |
# File 'lib/charisma/curator.rb', line 52 def to_s; inspect end |