Sha256: 3bf8fa6c36161bd28bc986810a690f54b072e6a22942cec741798f7c0c3bf27c

Contents?: true

Size: 1.34 KB

Versions: 18

Compression:

Stored size: 1.34 KB

Contents

class PSD
  # Used for lazily executing methods on demand
  class LazyExecute
    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

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

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

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

    def loaded?
      @loaded
    end

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

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

    private

    def load!
      PSD.logger.debug "Lazily executing #{@obj.class.name}##{@load_method}"
      orig_pos = @file.tell
      @file.seek @start_pos

      @obj.send(@load_method, *@load_args)

      @file.seek orig_pos
      @loaded = true
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
psd-3.9.0 lib/psd/lazy_execute.rb
psd-3.8.0 lib/psd/lazy_execute.rb
psd-3.7.0 lib/psd/lazy_execute.rb
psd-3.6.0 lib/psd/lazy_execute.rb
psd-3.5.0 lib/psd/lazy_execute.rb
psd-3.4.0 lib/psd/lazy_execute.rb
psd-3.3.3 lib/psd/lazy_execute.rb
psd-3.3.2 lib/psd/lazy_execute.rb
psd-3.3.1 lib/psd/lazy_execute.rb
psd-3.2.4 lib/psd/lazy_execute.rb
psd-3.2.3 lib/psd/lazy_execute.rb
psd-3.2.2 lib/psd/lazy_execute.rb
psd-3.2.1 lib/psd/lazy_execute.rb
psd-3.2.0 lib/psd/lazy_execute.rb
psd-3.1.5 lib/psd/lazy_execute.rb
psd-3.1.4 lib/psd/lazy_execute.rb
psd-3.1.3 lib/psd/lazy_execute.rb
psd-3.1.2 lib/psd/lazy_execute.rb