class MiniObject::Lazy

Attributes

lazy_name[RW]

Public Class Methods

new(name = nil, &block) click to toggle source
# File lib/mini_object/lazy.rb, line 7
def initialize name = nil, &block
  self.lazy_name = name
  @block = block
end

Public Instance Methods

__getobj__() click to toggle source
# File lib/mini_object/lazy.rb, line 12
def __getobj__
  @obj ||= @block.call.tap do |obj|
    build_steps.each do |name, block|
      block.call obj
    end
  end
end
Also aliased as: get_obj
build(&block) click to toggle source
# File lib/mini_object/lazy.rb, line 30
def build &block
  @block = block
end
build_step(name, &block) click to toggle source
# File lib/mini_object/lazy.rb, line 26
def build_step name, &block
  build_steps[name] = block
end
get_obj()
Alias for: __getobj__
inspect() click to toggle source
# File lib/mini_object/lazy.rb, line 34
def inspect
  prefix = lazy_name ? "#{lazy_name}: " : ""
  steps = " " + build_steps.keys.join(", ") if build_steps.any?
  "< #{prefix}Lazy(#{formatted_block_source}#{steps}) >"
end
Also aliased as: to_s
resolver_bound() click to toggle source
# File lib/mini_object/lazy.rb, line 22
def resolver_bound
  @block.binding.eval('self')
end
to_s()
Alias for: inspect

Private Instance Methods

block_source() click to toggle source
# File lib/mini_object/lazy.rb, line 48
def block_source
  require 'method_source'
  begin
    MethodSource.source_helper(@block.source_location)
  rescue MethodSource::SourceNotFoundError
    '{???}'
  end
end
build_steps() click to toggle source
# File lib/mini_object/lazy.rb, line 44
def build_steps
  @build_steps ||= {}
end
formatted_block_source() click to toggle source
# File lib/mini_object/lazy.rb, line 57
def formatted_block_source
  code = block_source.split("\n").map(&:strip).join("; ")
  code = code[0..59] + "\u2026" if code.length > 60
  code.inspect
end