Class: Shamu::Services::LazyAssociation

Inherits:
Delegator
  • Object
show all
Defined in:
lib/shamu/services/lazy_association.rb

Overview

Lazily look up an associated resource

Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, &block) ⇒ LazyAssociation

Returns a new instance of LazyAssociation.



19
20
21
22
# File 'lib/shamu/services/lazy_association.rb', line 19

def initialize( id, &block )
  @id = id
  @block = block
end

Instance Attribute Details

#idObject

Returns the primary key id of the association. Not delegated so it is safe to use and will not trigger an unnecessary fetch.

Returns:

  • (Object)

    the primary key id of the association. Not delegated so it is safe to use and will not trigger an unnecessary fetch.



14
15
16
# File 'lib/shamu/services/lazy_association.rb', line 14

def id
  @id
end

Instance Method Details

#__getobj__Object



24
25
26
27
28
# File 'lib/shamu/services/lazy_association.rb', line 24

def __getobj__
  return @association if defined? @association

  @association = @block.call
end