lazy_method 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +5 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lazy_method.gemspec +23 -0
- data/lib/lazy_method.rb +66 -0
- data/lib/lazy_method/version.rb +3 -0
- data/lib/lazy_method_test.rb +69 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f387793167c6f8b11a91ac506bd8ac1ce2d99341
|
4
|
+
data.tar.gz: 06e7f778c8cb7f1fb82294356086637dceeef04d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6171c1eed73220c805da6e43d49d762fff45d3961d25c1f84d7537e68cb20836e5e605a97ec91000c9b93c861835142225cdec90a0b07a7f0cde75482b1420a9
|
7
|
+
data.tar.gz: 017c23082306447eeb17991b761d319591aea4c18eb616e4adbf8c0f2e9507e766639c6e46dde94043d43602a9665fc50b971d1cc256685cf24fd05dca58a413
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 ksss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# LazyMethod
|
2
|
+
|
3
|
+
[](https://travis-ci.org/ksss/lazy_method)
|
4
|
+
|
5
|
+
Inspired by https://bugs.ruby-lang.org/issues/12125
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# standard style
|
11
|
+
foo = Foo.new
|
12
|
+
foo.bar # where is defined this method?
|
13
|
+
foo.method(:bar).source_location #=> [file.rb, 10]
|
14
|
+
foo.bar # OK remove `method(:` and `).source_location`
|
15
|
+
```
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
# lazy_method style
|
19
|
+
foo = Foo.new
|
20
|
+
foo.bar # where is defined this method?
|
21
|
+
foo.method.source_location.bar #=> [file.rb, 10]
|
22
|
+
foo.bar # OMG remove `.method.source_location` only
|
23
|
+
```
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
foo = Foo.new
|
27
|
+
|
28
|
+
# make Method object
|
29
|
+
foo.method(:bar)
|
30
|
+
|
31
|
+
# make LazyMethod object
|
32
|
+
foo.method #=> #<LazyMethod #<Foo:0x00>>
|
33
|
+
|
34
|
+
# make Method object from LazyMethod
|
35
|
+
foo.method.bar #=> #<Method: Foo#bar>
|
36
|
+
foo.method.bar.arity #=> 1
|
37
|
+
|
38
|
+
# Lazy call Method method
|
39
|
+
foo.method.arity #=>
|
40
|
+
#<LazyMethod #<Foo:0x00>(arity)>
|
41
|
+
foo.method.arity.bar #=> 1
|
42
|
+
```
|
43
|
+
|
44
|
+
## Installation
|
45
|
+
|
46
|
+
Add this line to your application's Gemfile:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
gem 'lazy_method'
|
50
|
+
```
|
51
|
+
|
52
|
+
And then execute:
|
53
|
+
|
54
|
+
$ bundle
|
55
|
+
|
56
|
+
Or install it yourself as:
|
57
|
+
|
58
|
+
$ gem install lazy_method
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "lazy_method"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lazy_method.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lazy_method/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lazy_method"
|
8
|
+
spec.version = LazyMethod::VERSION
|
9
|
+
spec.authors = ["ksss"]
|
10
|
+
spec.email = ["co000ri@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = 'LazyMethod is the new way of get method class object'
|
13
|
+
spec.description = 'LazyMethod is the new way of get method class object'
|
14
|
+
spec.homepage = "https://github.com/ksss/lazy_method"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rgot"
|
23
|
+
end
|
data/lib/lazy_method.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
class LazyMethod
|
2
|
+
require "lazy_method/version"
|
3
|
+
|
4
|
+
attr_reader :receiver
|
5
|
+
def initialize(receiver)
|
6
|
+
@receiver = receiver
|
7
|
+
@lazy_call_method = nil
|
8
|
+
end
|
9
|
+
|
10
|
+
METHOD_METHODS = %i(
|
11
|
+
source_location
|
12
|
+
parameters
|
13
|
+
arity
|
14
|
+
name
|
15
|
+
owner
|
16
|
+
to_proc
|
17
|
+
unbind
|
18
|
+
)
|
19
|
+
if "2.2.0" <= RUBY_VERSION
|
20
|
+
METHOD_METHODS << :super_method
|
21
|
+
end
|
22
|
+
METHOD_METHODS.each do |m|
|
23
|
+
define_method(m) do
|
24
|
+
@lazy_call_method = m
|
25
|
+
self
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
"#<LazyMethod #{@receiver}#{@lazy_call_method ? "(#{@lazy_call_method})" : ''}>"
|
31
|
+
end
|
32
|
+
alias inspect to_s
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def respond_to_missing?(name, _include_private)
|
37
|
+
case name
|
38
|
+
when :to_ary
|
39
|
+
false
|
40
|
+
else
|
41
|
+
true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def method_missing(name, *)
|
46
|
+
return unless respond_to_missing?(name, false)
|
47
|
+
m = @receiver.__method__(name)
|
48
|
+
if @lazy_call_method
|
49
|
+
m.__send__ @lazy_call_method
|
50
|
+
else
|
51
|
+
m
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module API
|
56
|
+
def method(name = nil)
|
57
|
+
if name
|
58
|
+
super
|
59
|
+
else
|
60
|
+
LazyMethod.new(self)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
Kernel.__send__ :alias_method, :__method__, :method
|
65
|
+
Object.prepend API
|
66
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'lazy_method'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
module LazyMethodTest
|
5
|
+
def test_puts(t)
|
6
|
+
io = StringIO.new
|
7
|
+
io.puts method
|
8
|
+
unless /#<LazyMethod/ =~ io.string
|
9
|
+
t.error(io.string)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_call_basic_methods(t)
|
14
|
+
ary = []
|
15
|
+
[
|
16
|
+
[-> { $stdout.method(:puts) }, -> { $stdout.method.puts }],
|
17
|
+
[-> { 1.method(:+) }, -> { 1.method.+ }],
|
18
|
+
[-> { 1.method(:+@) }, -> { +(1.method) }],
|
19
|
+
[-> { ary.method(:[]) }, -> { ary.method[0] }],
|
20
|
+
[-> { ary.method(:[]=) }, -> { ary.method.[]=(0, 1) }],
|
21
|
+
].each do |expect_case, test_case|
|
22
|
+
unless expect_case.call == test_case.call
|
23
|
+
path, line = test_case.source_location
|
24
|
+
code = File.open(path) { |f| (line - 1).times { f.gets }; f.gets }
|
25
|
+
t.error("LazyMethod methods are should return Method object as much as possible")
|
26
|
+
t.log("return #{test_case.call}, expect #{expect_case.call}")
|
27
|
+
t.log("#{File.basename(path)}:#{line}: #{code.chomp}")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_name_error(t)
|
33
|
+
[
|
34
|
+
-> { method.nothing },
|
35
|
+
-> { +method },
|
36
|
+
-> { method[0] },
|
37
|
+
].each do |test_case|
|
38
|
+
begin
|
39
|
+
test_case.call
|
40
|
+
rescue NameError
|
41
|
+
else
|
42
|
+
t.error("expect raise error but nothing raised")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Foo
|
48
|
+
def bar(a, b = nil)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_method_methods(t)
|
53
|
+
foo = Foo.new
|
54
|
+
LazyMethod::METHOD_METHODS.each do |m|
|
55
|
+
s = foo.method.__send__(m)
|
56
|
+
unless LazyMethod === s
|
57
|
+
t.error("should be LazyMethod instance got #{s}")
|
58
|
+
end
|
59
|
+
|
60
|
+
expect = foo.method(:bar).__send__(m)
|
61
|
+
|
62
|
+
next if m == :to_proc
|
63
|
+
|
64
|
+
unless expect == s.bar
|
65
|
+
t.error("call #{m} expect #{expect}, got #{s.bar}")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_method
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ksss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-05 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rgot
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: LazyMethod is the new way of get method class object
|
56
|
+
email:
|
57
|
+
- co000ri@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lazy_method.gemspec
|
71
|
+
- lib/lazy_method.rb
|
72
|
+
- lib/lazy_method/version.rb
|
73
|
+
- lib/lazy_method_test.rb
|
74
|
+
homepage: https://github.com/ksss/lazy_method
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.5.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: LazyMethod is the new way of get method class object
|
98
|
+
test_files: []
|