Sha256: af12410098469602980b786b5b8a209eaea16204d13b1000a436f18272df2509

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module Terrestrial
  class LazyObjectProxy
    include ShortInspectionString

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

    attr_reader :object_loader
    private     :object_loader

    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

    def loaded?
      !!@lazy_object
    end

    def __getobj__
      lazy_object
    end

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

    def __key_fields
      @key_fields
    end

    private

    def respond_to_missing?(method_id, _include_private = false)
      __key_fields.include?(method_id) || lazy_object.respond_to?(method_id)
    end

    def lazy_object
      @lazy_object ||= object_loader.call
    end

    def inspectable_properties
      [
        :key_fields,
        :lazy_object,
      ]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terrestrial-0.1.1 lib/terrestrial/lazy_object_proxy.rb