diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31cafb5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp +*.bundle +*.so +*.o +*.a +mkmf.log diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 0000000..01f18cd --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +lazy_object diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..2972947 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-2.1 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..cb8da7f --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in lazy_object.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..06fe521 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 Arthur Shagall + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2440ba --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# 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: + +```ruby +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! diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..809eb56 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require "bundler/gem_tasks" + diff --git a/lazy_object.gemspec b/lazy_object.gemspec new file mode 100644 index 0000000..1719b67 --- /dev/null +++ b/lazy_object.gemspec @@ -0,0 +1,23 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'lazy_object' + +Gem::Specification.new do |spec| + spec.name = "lazy_object" + spec.version = LazyObject.version + spec.authors = ["Arthur Shagall", "Sergey Potapov"] + spec.email = ["arthur.shagall@gmail.com"] + spec.summary = %q{Lazily initialized object wrapper.} + spec.description = %q{It's an object wrapper that forwards all calls to the reference object. This object is not created until the first method dispatch.} + spec.homepage = "" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0") + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.6" + spec.add_development_dependency "rake" +end diff --git a/lib/lazy_object.rb b/lib/lazy_object.rb new file mode 100644 index 0000000..f96d53d --- /dev/null +++ b/lib/lazy_object.rb @@ -0,0 +1,30 @@ +# Lazy object wrapper. +# +# 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. +# lazy.get_expensive_results(foo, bar) # Initializes VeryExpensiveObject and calls 'get_expensive_results' on it, passing in foo and bar +class LazyObject + + def self.version + '0.0.1' + end + + def initialize(&callable) + @__callable__ = callable + end + + # Cached target object. + def __target_object__ + @__target_object__ ||= @__callable__.call + end + + # Forwards all method calls to the target object. + def method_missing(method_name, *args, &block) + __target_object__.send(method_name, *args, &block) + end +end