lazy_donkey 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +2 -0
- data/lazy_donkey.gemspec +24 -0
- data/lib/lazy_donkey/version.rb +3 -0
- data/lib/lazy_donkey.rb +6 -0
- data/lib/module.rb +25 -0
- data/spec/module_spec.rb +68 -0
- data/spec/spec_helper.rb +5 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1f6bc9cd848f27f244d89c0e65fd96396b9e4d4d
|
4
|
+
data.tar.gz: d0ee6aefdbec19cf8783a2290a30aac2ae88443e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c5a10062e1e00e74fa99feb3dbf3243311c815ae179f59f64b68c64e710c9ad93e53c563e4e4a20a4eb02c41461c96c1d5579428aaac2d5d5ccb351d0d82ab2
|
7
|
+
data.tar.gz: a7e6058e3d19534eae0ec282b7846da9f78818f8c87b2e40a9eade3967006199eb152b985ebda67ec7271496d76200fbd9349b0dc7efc0ba3a20cc0b891a57df
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# LazyDonkey
|
2
|
+
|
3
|
+
LazyDonkey is a gem that introduces lazily initialized attributes, whose value is calculated on the first use. In all other aspects they are identical to attributes defined with attribute_accessor or attr:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
class Foo
|
7
|
+
lazy_attribute(:foo) {"Return value"}
|
8
|
+
end
|
9
|
+
```
|
10
|
+
|
11
|
+
When you call "#foo" for the first time, it will execute the code block and assign its return value to the instance variable @foo. The return value is cached and will be returned next times you call #foo. The initialization code block will be executed only once.
|
12
|
+
|
13
|
+
## Visibility
|
14
|
+
By default, lazy_attributes creates public methods, but you can explicitly specify visibility for each attribute:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
lazy_attribute(:public_attribute, :public)
|
18
|
+
lazy_attribute(:protected_attrobite, :protected)
|
19
|
+
lazy_attribute(:private_attribute, :private)
|
20
|
+
```
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your application's Gemfile:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem 'lazy_donkey'
|
28
|
+
```
|
29
|
+
|
30
|
+
And then execute:
|
31
|
+
|
32
|
+
$ bundle
|
33
|
+
|
34
|
+
Or install it yourself as:
|
35
|
+
|
36
|
+
$ gem install lazy_donkey
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
Use this gem for defining instance attributes that you don't need right away. You can use it instead of initializers which always run at start time, and keep your initialization code with the rest of your components. You can avoid initialization errors in test environment which has incorrect/missing settings for components you're not going to test. Or you can find another simple use for it.
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it ( https://github.com/[my-github-username]/lazy_donkey/fork )
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lazy_donkey.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lazy_donkey/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lazy_donkey"
|
8
|
+
spec.version = LazyDonkey::VERSION
|
9
|
+
spec.authors = ["Aliaksei Baturytski"]
|
10
|
+
spec.email = ["abaturytski@gmail.com"]
|
11
|
+
spec.summary = "Lazy attributes"
|
12
|
+
spec.description = "Attributes that are initialized on the first use"
|
13
|
+
spec.homepage = "https://github.com/aliakb/lazy_donkey"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 1.8.7"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
data/lib/lazy_donkey.rb
ADDED
data/lib/module.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class Module
|
2
|
+
def lazy_attribute(symbol, visibility = :public, &block)
|
3
|
+
var_symbol = "@#{symbol}"
|
4
|
+
block = Proc.new{nil} unless block
|
5
|
+
instance_variable_set(var_symbol, block)
|
6
|
+
|
7
|
+
define_method("#{symbol}=") do |value|
|
8
|
+
instance_variable_set("@#{symbol}", value)
|
9
|
+
end
|
10
|
+
|
11
|
+
define_method(symbol) do
|
12
|
+
var = "@#{symbol}"
|
13
|
+
value = instance_variable_get(var)
|
14
|
+
|
15
|
+
if (!value && !instance_variable_defined?(var))
|
16
|
+
value = self.class.instance_variable_get(var).call
|
17
|
+
instance_variable_set(var, value)
|
18
|
+
end
|
19
|
+
value
|
20
|
+
end
|
21
|
+
|
22
|
+
send(visibility, symbol)
|
23
|
+
send(visibility, "#{symbol}=")
|
24
|
+
end
|
25
|
+
end
|
data/spec/module_spec.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Module do
|
4
|
+
class TestClass
|
5
|
+
lazy_attribute(:without_block)
|
6
|
+
lazy_attribute(:test){"Test"}
|
7
|
+
|
8
|
+
lazy_attribute(:public_attr, :public) {"Test"}
|
9
|
+
lazy_attribute(:protected_attr, :protected) {"Test"}
|
10
|
+
lazy_attribute(:private_attr, :private) {"Test"}
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:test) {TestClass.new}
|
14
|
+
|
15
|
+
it "is nil without code block" do
|
16
|
+
expect(test.without_block).to be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns value of the code block" do
|
20
|
+
expect(test.test).to eq "Test"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "supports assignment" do
|
24
|
+
expected = "This is a test"
|
25
|
+
test.test = expected
|
26
|
+
expect(test.test).to eq expected
|
27
|
+
end
|
28
|
+
|
29
|
+
context "default visibility" do
|
30
|
+
it "defines public reader" do
|
31
|
+
expect(test.public_methods.map(&:to_s)).to include("test")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "defines public writer" do
|
35
|
+
expect(test.public_methods.map(&:to_s)).to include("test=")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "public visibility" do
|
40
|
+
it "defines public reader" do
|
41
|
+
expect(test.public_methods.map(&:to_s)).to include("public_attr")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "defines public writer" do
|
45
|
+
expect(test.public_methods.map(&:to_s)).to include("public_attr=")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "protected visibility" do
|
50
|
+
it "defines protected reader" do
|
51
|
+
expect(test.protected_methods.map(&:to_s)).to include("protected_attr")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "defines protected writer" do
|
55
|
+
expect(test.protected_methods.map(&:to_s)).to include("protected_attr=")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "private visibility" do
|
60
|
+
it "defines private reader" do
|
61
|
+
expect(test.private_methods.map(&:to_s)).to include("private_attr")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "defines private writer" do
|
65
|
+
expect(test.private_methods.map(&:to_s)).to include("private_attr=")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_donkey
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aliaksei Baturytski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Attributes that are initialized on the first use
|
42
|
+
email:
|
43
|
+
- abaturytski@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lazy_donkey.gemspec
|
54
|
+
- lib/lazy_donkey.rb
|
55
|
+
- lib/lazy_donkey/version.rb
|
56
|
+
- lib/module.rb
|
57
|
+
- spec/module_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: https://github.com/aliakb/lazy_donkey
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 1.8.7
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.4.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Lazy attributes
|
83
|
+
test_files:
|
84
|
+
- spec/module_spec.rb
|
85
|
+
- spec/spec_helper.rb
|