Class: PSD::LazyExecute

Inherits:
Object
  • Object
show all
Defined in:
lib/psd/lazy_execute.rb

Overview

Used for lazily executing methods on demand

Instance Method Summary collapse

Constructor Details

#initialize(obj, file) ⇒ LazyExecute

Returns a new instance of LazyExecute.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/psd/lazy_execute.rb', line 4

def initialize(obj, file)
  @obj = obj
  @file = file

  @start_pos = @file.tell
  @loaded = false
  @load_method = nil
  @load_args = []
  @passthru = []

  PSD.logger.debug "Marked #{@obj.class.name} at position #{@start_pos} for lazy execution"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



38
39
40
41
# File 'lib/psd/lazy_execute.rb', line 38

def method_missing(method, *args, &block)
  load! if !loaded? && !@passthru.include?(method.to_sym)
  @obj.send(method, *args, &block)
end

Instance Method Details

#ignore(*args) ⇒ Object



29
30
31
32
# File 'lib/psd/lazy_execute.rb', line 29

def ignore(*args)
  @passthru += args
  return self
end

#inspectObject



43
44
45
46
47
48
49
# File 'lib/psd/lazy_execute.rb', line 43

def inspect
  if loaded?
    @obj.inspect
  else
    "<PSD::LazyExecute @obj=#{@obj.class.name}, @pos=#{@start_pos}, @load_method=:#{@load_method}>"
  end
end

#later(method, *args) ⇒ Object



23
24
25
26
27
# File 'lib/psd/lazy_execute.rb', line 23

def later(method, *args)
  @load_method = method
  @load_args = args
  return self
end

#loaded?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/psd/lazy_execute.rb', line 34

def loaded?
  @loaded
end

#now(method, *args, &block) ⇒ Object



17
18
19
20
21
# File 'lib/psd/lazy_execute.rb', line 17

def now(method, *args, &block)
  PSD.logger.debug "Executing #{@obj.class.name}##{method} now"
  @obj.send(method, *args, &block)
  return self
end