0.01
Low commit activity in last 3 years
A long-lived project that still receives updates
It's an object wrapper that forwards all calls to the reference object. This object is not created until the first method dispatch.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
>= 0
>= 0
 Project Readme

LazyObject

It's an object wrapper that forwards all calls to the reference target object. This object is not created until the first method dispatch.

Installation

Add this line to your application's Gemfile:

gem 'lazy_object'

And then execute:

$ bundle

Or install it yourself as:

$ gem install lazy_object

Usage

Pass a block to the initializer, which returns an instance of the target object. Lazy object forwards all method calls to the target. The block only gets called the first time a method is forwarded.

Example:

lazy = LazyObject.new { VeryExpensiveObject.new }
# At this point the VeryExpensiveObject hasn't been initialized yet.

# Initializes VeryExpensiveObject and calls 'get_expensive_results' on it, passing in foo and bar
lazy.get_expensive_results(foo, bar)

# You can pass in blocks to the target.
lazy.perform_operation do |foo, bar|
  foo + bar
end

Contributing

  1. Fork it ( https://github.com/[my-github-username]/lazy_object/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
  6. Make sure all the test pass and your changes have test coverage!