Class: Terrestrial::LazyObjectProxy

Inherits:
Object
  • Object
show all
Includes:
InspectionString
Defined in:
lib/terrestrial/lazy_object_proxy.rb

Overview

TODO: This should do a better job of showing what will be loaded when it is inspected

Instance Method Summary collapse

Methods included from InspectionString

#inspect

Constructor Details

#initialize(object_loader, key_fields) ⇒ LazyObjectProxy

Returns a new instance of LazyObjectProxy.



6
7
8
9
10
# File 'lib/terrestrial/lazy_object_proxy.rb', line 6

def initialize(object_loader, key_fields)
  @object_loader = object_loader
  @key_fields = key_fields
  @lazy_object = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/terrestrial/lazy_object_proxy.rb', line 15

def method_missing(method_id, *args, &block)
  if args.empty? && __key_fields.include?(method_id)
    __key_fields.fetch(method_id)
  else
    lazy_object.public_send(method_id, *args, &block)
  end
end

Instance Method Details

#__getobj__Object



27
28
29
# File 'lib/terrestrial/lazy_object_proxy.rb', line 27

def __getobj__
  lazy_object
end

#__key_fieldsObject



35
36
37
# File 'lib/terrestrial/lazy_object_proxy.rb', line 35

def __key_fields
  @key_fields
end

#each_loaded(&block) ⇒ Object



31
32
33
# File 'lib/terrestrial/lazy_object_proxy.rb', line 31

def each_loaded(&block)
  [self].each(&block)
end

#loaded?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/terrestrial/lazy_object_proxy.rb', line 23

def loaded?
  !!@lazy_object
end