Class: SeeingIsBelieving::HashStruct::Attr

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/hash_struct.rb

Overview

This could support dynamic attributes very easily ie they are calculated, but appear as a value (e.g. in to_hash) not sure how to deal with the fact that they could be assigned, though

Instance Method Summary collapse

Constructor Details

#initialize(instance, value = nil, &block) ⇒ Attr

Returns a new instance of Attr.



77
78
79
80
81
# File 'lib/seeing_is_believing/hash_struct.rb', line 77

def initialize(instance, value=nil, &block)
  @instance = instance
  @block    = block if block
  @value    = value unless block
end

Instance Method Details

#valueObject



82
83
84
85
# File 'lib/seeing_is_believing/hash_struct.rb', line 82

def value
  return @value if defined? @value
  @value = @block.call(@instance)
end