reflection_utils 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +73 -72
- data/lib/reflection_utils.rb +50 -49
- data/lib/reflection_utils/create_class_methods_upon_include.rb +9 -9
- data/lib/reflection_utils/create_module_functions.rb +12 -0
- data/lib/reflection_utils/version.rb +3 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 571f076e3fdbd327ef51ff4a018483cdaae24dd0
|
4
|
+
data.tar.gz: 115edd01652ae363aa797e248a82f19d1c4b9d62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf21898bdb44e9c05442df1f565c4151e36560a1d88d94b5b2e0a76ff6a517c63bd8e5bfd0e2a2c63bb034004bdbbe7da6fee7b7688126ccdb82a39693b1f0ab
|
7
|
+
data.tar.gz: e5616ad3e79722f06facd7a3163f4bba0ce02503a03b61d13869774ee639a655101ae0b83043c55df75b1cce0d0dbd0d89d941616361e19e41505eecf766f0ae
|
data/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2017
|
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 all
|
13
|
-
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 THE
|
21
|
-
SOFTWARE.
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,72 +1,73 @@
|
|
1
|
-
# ReflectionUtils
|
2
|
-
|
3
|
-
#### Commonly used Ruby utility functions related to reflection.
|
4
|
-
|
5
|
-
| Branch | Status |
|
6
|
-
| ------ | ------ |
|
7
|
-
| Release | [![Build Status](https://travis-ci.org/thisismydesign/reflection_utils.svg?branch=release)](https://travis-ci.org/thisismydesign/reflection_utils) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/reflection_utils/badge.svg?branch=release)](https://coveralls.io/github/thisismydesign/reflection_utils?branch=release) [![Gem Version](https://badge.fury.io/rb/reflection_utils.svg)](https://badge.fury.io/rb/reflection_utils) [![Total Downloads](http://ruby-gem-downloads-badge.herokuapp.com/reflection_utils?type=total)](https://rubygems.org/gems/reflection_utils) |
|
8
|
-
| Development | [![Build Status](https://travis-ci.org/thisismydesign/reflection_utils.svg?branch=master)](https://travis-ci.org/thisismydesign/reflection_utils) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/reflection_utils/badge.svg?branch=master)](https://coveralls.io/github/thisismydesign/reflection_utils?branch=master) |
|
9
|
-
|
10
|
-
## Features
|
11
|
-
|
12
|
-
Functions include:
|
13
|
-
- get_bound_instance_method(instance:, method_name:)
|
14
|
-
- get_class_constant(instance:)
|
15
|
-
- call(callback, params = nil)
|
16
|
-
- has_parameter?(proc, parameter_index, parameter)
|
17
|
-
- has_parameters?(proc, parameters)
|
18
|
-
- assert_parameters(proc, parameters)
|
19
|
-
- non_default_methods(class_or_instance)
|
20
|
-
- module CreateClassMethodsUponInclude (more: http://lifeinacubicleblog.com/2017/06/26/ruby-how-to-use-self-included-meaningfully)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
1
|
+
# ReflectionUtils
|
2
|
+
|
3
|
+
#### Commonly used Ruby utility functions related to reflection.
|
4
|
+
|
5
|
+
| Branch | Status |
|
6
|
+
| ------ | ------ |
|
7
|
+
| Release | [![Build Status](https://travis-ci.org/thisismydesign/reflection_utils.svg?branch=release)](https://travis-ci.org/thisismydesign/reflection_utils) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/reflection_utils/badge.svg?branch=release)](https://coveralls.io/github/thisismydesign/reflection_utils?branch=release) [![Gem Version](https://badge.fury.io/rb/reflection_utils.svg)](https://badge.fury.io/rb/reflection_utils) [![Total Downloads](http://ruby-gem-downloads-badge.herokuapp.com/reflection_utils?type=total)](https://rubygems.org/gems/reflection_utils) |
|
8
|
+
| Development | [![Build Status](https://travis-ci.org/thisismydesign/reflection_utils.svg?branch=master)](https://travis-ci.org/thisismydesign/reflection_utils) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/reflection_utils/badge.svg?branch=master)](https://coveralls.io/github/thisismydesign/reflection_utils?branch=master) |
|
9
|
+
|
10
|
+
## Features
|
11
|
+
|
12
|
+
Functions include:
|
13
|
+
- get_bound_instance_method(instance:, method_name:)
|
14
|
+
- get_class_constant(instance:)
|
15
|
+
- call(callback, params = nil)
|
16
|
+
- has_parameter?(proc, parameter_index, parameter)
|
17
|
+
- has_parameters?(proc, parameters)
|
18
|
+
- assert_parameters(proc, parameters)
|
19
|
+
- non_default_methods(class_or_instance)
|
20
|
+
- module CreateClassMethodsUponInclude (more: http://lifeinacubicleblog.com/2017/06/26/ruby-how-to-use-self-included-meaningfully)
|
21
|
+
- module CreateModuleFunctions (more: https://stackoverflow.com/questions/322470/can-i-invoke-an-instance-method-on-a-ruby-module-without-including-it)
|
22
|
+
|
23
|
+
For usage and examples for each of them take a look at the [unit tests](https://github.com/thisismydesign/reflection_utils/blob/master/spec/reflection_utils_spec.rb).
|
24
|
+
|
25
|
+
Another approach would be to include this functionality into classes where its needed. It may be more elegant however moving this functionality into a separate utility class decouples the logic and removes dependency from those classes.
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application's Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'reflection_utils'
|
33
|
+
```
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
$ bundle
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install reflection_utils
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'reflection_utils'
|
47
|
+
|
48
|
+
# ...
|
49
|
+
|
50
|
+
ReflectionUtils.get_bound_instance_method(instance: my_obj, method_name: :my_method)
|
51
|
+
|
52
|
+
# etc.
|
53
|
+
```
|
54
|
+
|
55
|
+
## Feedback
|
56
|
+
|
57
|
+
Any feedback is much appreciated.
|
58
|
+
|
59
|
+
I can only tailor this project to fit use-cases I know about - which are usually my own ones. If you find that this might be the right direction to solve your problem too but you find that it's suboptimal or lacks features don't hesitate to contact me.
|
60
|
+
|
61
|
+
Please let me know if you make use of this project so that I can prioritize further efforts.
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
This gem is developed using Bundler conventions. A good overview can be found [here](http://bundler.io/v1.14/guides/creating_gem.html).
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/reflection_utils.
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/reflection_utils.rb
CHANGED
@@ -1,49 +1,50 @@
|
|
1
|
-
require_relative 'reflection_utils/create_class_methods_upon_include'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
1
|
+
require_relative 'reflection_utils/create_class_methods_upon_include'
|
2
|
+
require_relative 'reflection_utils/create_module_functions'
|
3
|
+
|
4
|
+
module ReflectionUtils
|
5
|
+
|
6
|
+
def self.get_bound_instance_method(instance:, method_name:)
|
7
|
+
get_class_constant(instance: instance).instance_method(method_name).bind(instance)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.get_class_constant(instance:)
|
11
|
+
Object.const_get(instance.class.name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.call(callback, params = nil)
|
15
|
+
params.nil? ? callback.call : callback.call(params)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.has_parameters?(proc, parameters)
|
19
|
+
parameters.each_with_index do |parameter, index|
|
20
|
+
return false unless has_parameter?(proc, index, parameter)
|
21
|
+
end
|
22
|
+
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.assert_parameters(proc, parameters)
|
27
|
+
parameters.each_with_index do |parameter, index|
|
28
|
+
raise ArgumentError, "Argument #{parameter} not found for #{proc} at #{index} index." unless has_parameter?(proc, index, parameter)
|
29
|
+
end
|
30
|
+
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.has_parameter?(proc, parameter_index, parameter)
|
35
|
+
proc.parameters[parameter_index] == parameter
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.non_default_methods(class_or_instance)
|
39
|
+
class_or_instance.is_a?(Module) ? non_default_class_methods(class_or_instance) : non_default_instance_methods(class_or_instance)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.non_default_class_methods(clazz)
|
43
|
+
clazz.methods - Object.methods
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.non_default_instance_methods(instance)
|
47
|
+
instance.methods - Object.new.methods
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
module ReflectionUtils
|
2
|
-
module CreateClassMethodsUponInclude
|
3
|
-
def self.included(base)
|
4
|
-
base.define_singleton_method :included do |base|
|
5
|
-
base.extend(self)
|
6
|
-
end
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
module ReflectionUtils
|
2
|
+
module CreateClassMethodsUponInclude
|
3
|
+
def self.included(base)
|
4
|
+
base.define_singleton_method :included do |base|
|
5
|
+
base.extend(self)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module ReflectionUtils
|
2
|
-
VERSION = "0.
|
3
|
-
end
|
1
|
+
module ReflectionUtils
|
2
|
+
VERSION = "0.3.0"
|
3
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reflection_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thisismydesign
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coveralls
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- bin/setup
|
85
85
|
- lib/reflection_utils.rb
|
86
86
|
- lib/reflection_utils/create_class_methods_upon_include.rb
|
87
|
+
- lib/reflection_utils/create_module_functions.rb
|
87
88
|
- lib/reflection_utils/version.rb
|
88
89
|
- reflection_utils.gemspec
|
89
90
|
homepage: https://github.com/thisismydesign/reflection_utils
|
@@ -105,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
106
|
version: '0'
|
106
107
|
requirements: []
|
107
108
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.5.
|
109
|
+
rubygems_version: 2.5.1
|
109
110
|
signing_key:
|
110
111
|
specification_version: 4
|
111
112
|
summary: Commonly used utility functions related to reflection
|