├── .rspec ├── spec ├── spec_helper.rb └── attr_lazy_spec.rb ├── lib ├── attr_lazy │ └── version.rb └── attr_lazy.rb ├── Gemfile ├── Rakefile ├── .gitignore ├── README.md ├── attr_lazy.gemspec └── LICENSE.md /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | require 'attr_lazy' 4 | require 'rspec' 5 | -------------------------------------------------------------------------------- /lib/attr_lazy/version.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | module AttrLazy 4 | VERSION = '0.0.2' 5 | end 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in attr_lazy.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | require 'bundler/gem_tasks' 4 | 5 | require 'rspec/core/rake_task' 6 | RSpec::Core::RakeTask.new(:spec) 7 | 8 | task :default => :spec 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /lib/attr_lazy.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | require 'attr_lazy/version' 4 | 5 | module AttrLazy 6 | def attr_lazy_reader(reader_name, &block) 7 | define_method(reader_name) do 8 | instance_variable_get("@#{reader_name}") || 9 | instance_variable_set("@#{reader_name}", instance_eval(&block)) 10 | end 11 | end 12 | 13 | def attr_lazy_accessor(accessor_name, &block) 14 | attr_lazy_reader(accessor_name, &block) 15 | define_method("#{accessor_name}=") do |value| 16 | instance_variable_set("@#{accessor_name}", value) 17 | end 18 | end 19 | end 20 | 21 | class Module 22 | include AttrLazy 23 | end 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AttrLazy 2 | 3 | Define an accessor to class that do lazy evaluation. 4 | 5 | ## Installation 6 | 7 | Add this line to your application's Gemfile: 8 | 9 | gem 'attr_lazy' 10 | 11 | And then execute: 12 | 13 | $ bundle 14 | 15 | Or install it yourself as: 16 | 17 | $ gem install attr_lazy 18 | 19 | ## Usage 20 | 21 | ```ruby 22 | require 'attr_lazy' 23 | 24 | class Foo 25 | attr_lazy_reader :bar do 26 | # high cost initializer 27 | end 28 | attr_lazy_accessor :baz do 29 | # high cost initializer 30 | end 31 | end 32 | ``` 33 | 34 | ## Contributing 35 | 36 | 1. Fork it 37 | 2. Create your feature branch (`git checkout -b my-new-feature`) 38 | 3. Commit your changes (`git commit -am 'Add some feature'`) 39 | 4. Push to the branch (`git push origin my-new-feature`) 40 | 5. Create new Pull Request 41 | 42 | ## Copyright 43 | 44 | Copyright (c) 2013 [Kazuya Takeshima](mailto:mail@mitukiii.jp). See [LICENSE][license] for details. 45 | 46 | [license]: LICENSE.md 47 | -------------------------------------------------------------------------------- /attr_lazy.gemspec: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | lib = File.expand_path('../lib', __FILE__) 4 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 5 | require 'attr_lazy/version' 6 | 7 | Gem::Specification.new do |spec| 8 | spec.name = 'attr_lazy' 9 | spec.version = AttrLazy::VERSION 10 | spec.authors = ['Kazuya Takeshima'] 11 | spec.email = ['mail@mitukiii.jp'] 12 | spec.description = %q{Define an accessor to class that do lazy evaluation.} 13 | spec.summary = %q{Define an accessor to class that do lazy evaluation.} 14 | spec.homepage = 'https://github.com/mitukiii/attr_lazy' 15 | spec.license = 'MIT' 16 | 17 | spec.files = `git ls-files`.split($/) 18 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 19 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 20 | spec.require_paths = ['lib'] 21 | 22 | spec.add_development_dependency 'bundler', '~> 1.3' 23 | spec.add_development_dependency 'rake' 24 | spec.add_development_dependency 'rspec' 25 | end 26 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Kazuya Takeshima 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /spec/attr_lazy_spec.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | require 'spec_helper' 4 | 5 | describe AttrLazy do 6 | let(:cat) do 7 | Class.new { 8 | attr_lazy_reader(:name) do 9 | :nyan 10 | end 11 | 12 | attr_lazy_accessor(:color) do 13 | :black 14 | end 15 | }.new 16 | end 17 | 18 | describe '#attr_lazy_reader' do 19 | it 'should define reader' do 20 | cat.should be_respond_to(:name) 21 | cat.instance_variable_get(:@name).should == nil 22 | cat.name.should == :nyan 23 | cat.instance_variable_get(:@name).should == :nyan 24 | end 25 | end 26 | 27 | describe '#attr_lazy_accessor' do 28 | it 'should define accessor' do 29 | cat.should be_respond_to(:color) 30 | cat.instance_variable_get(:@color).should == nil 31 | cat.color.should == :black 32 | cat.instance_variable_get(:@color).should == :black 33 | cat.should be_respond_to(:color=) 34 | cat.color = :white 35 | cat.color.should == :white 36 | cat.instance_variable_get(:@color).should == :white 37 | end 38 | end 39 | end 40 | --------------------------------------------------------------------------------