reflection_utils 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +72 -72
- data/lib/reflection_utils.rb +49 -49
- data/lib/reflection_utils/version.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e79dd0612063c610415fb53e00eb28f7028d051
|
4
|
+
data.tar.gz: e038ab0057ebf11a500b161d9dd723804a0e0941
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbbfc1be41596ec50c7f55dc574cd307af011b02c26f1b3a842eec285fe5d17290f677cda16de6f648499e22200563f84b2fee0184017c490eb53d6a37bf6187
|
7
|
+
data.tar.gz: 41ebc3ff100c6ec141d62f451b9c22a1c117e51261f23bde1d4a6b6828d0f71675c4da0df1d53524d97633f2c159c7bd1a1cb9b12a20edfbf9102d9acd647057
|
data/README.md
CHANGED
@@ -1,72 +1,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
|
-
|
22
|
-
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).
|
23
|
-
|
24
|
-
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.
|
25
|
-
|
26
|
-
## Installation
|
27
|
-
|
28
|
-
Add this line to your application's Gemfile:
|
29
|
-
|
30
|
-
```ruby
|
31
|
-
gem 'reflection_utils'
|
32
|
-
```
|
33
|
-
|
34
|
-
And then execute:
|
35
|
-
|
36
|
-
$ bundle
|
37
|
-
|
38
|
-
Or install it yourself as:
|
39
|
-
|
40
|
-
$ gem install reflection_utils
|
41
|
-
|
42
|
-
## Usage
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
require 'reflection_utils'
|
46
|
-
|
47
|
-
# ...
|
48
|
-
|
49
|
-
ReflectionUtils.get_bound_instance_method(instance: my_obj, method_name: :my_method)
|
50
|
-
|
51
|
-
# etc.
|
52
|
-
```
|
53
|
-
|
54
|
-
## Feedback
|
55
|
-
|
56
|
-
Any feedback is much appreciated.
|
57
|
-
|
58
|
-
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.
|
59
|
-
|
60
|
-
Please let me know if you make use of this project so that I can prioritize further efforts.
|
61
|
-
|
62
|
-
## Development
|
63
|
-
|
64
|
-
This gem is developed using Bundler conventions. A good overview can be found [here](http://bundler.io/v1.14/guides/creating_gem.html).
|
65
|
-
|
66
|
-
## Contributing
|
67
|
-
|
68
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
69
|
-
|
70
|
-
## License
|
71
|
-
|
72
|
-
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
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
|
+
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).
|
23
|
+
|
24
|
+
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.
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
gem 'reflection_utils'
|
32
|
+
```
|
33
|
+
|
34
|
+
And then execute:
|
35
|
+
|
36
|
+
$ bundle
|
37
|
+
|
38
|
+
Or install it yourself as:
|
39
|
+
|
40
|
+
$ gem install reflection_utils
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
require 'reflection_utils'
|
46
|
+
|
47
|
+
# ...
|
48
|
+
|
49
|
+
ReflectionUtils.get_bound_instance_method(instance: my_obj, method_name: :my_method)
|
50
|
+
|
51
|
+
# etc.
|
52
|
+
```
|
53
|
+
|
54
|
+
## Feedback
|
55
|
+
|
56
|
+
Any feedback is much appreciated.
|
57
|
+
|
58
|
+
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.
|
59
|
+
|
60
|
+
Please let me know if you make use of this project so that I can prioritize further efforts.
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
This gem is developed using Bundler conventions. A good overview can be found [here](http://bundler.io/v1.14/guides/creating_gem.html).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/reflection_utils.
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
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,49 @@
|
|
1
|
-
require_relative 'reflection_utils/create_class_methods_upon_include'
|
2
|
-
|
3
|
-
module ReflectionUtils
|
4
|
-
|
5
|
-
def self.get_bound_instance_method(instance:, method_name:)
|
6
|
-
get_class_constant(instance: instance).instance_method(method_name).bind(instance)
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.get_class_constant(instance:)
|
10
|
-
Object.const_get(instance.class.name)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.call(callback, params = nil)
|
14
|
-
params.nil? ? callback.call : callback.call(params)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.has_parameters?(proc, parameters)
|
18
|
-
parameters.each_with_index do |parameter, index|
|
19
|
-
return false unless has_parameter?(proc, index, parameter)
|
20
|
-
end
|
21
|
-
|
22
|
-
true
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.assert_parameters(proc, parameters)
|
26
|
-
parameters.each_with_index do |parameter, index|
|
27
|
-
raise ArgumentError, "Argument #{parameter} not found for #{proc} at #{index} index." unless has_parameter?(proc, index, parameter)
|
28
|
-
end
|
29
|
-
|
30
|
-
true
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.has_parameter?(proc, parameter_index, parameter)
|
34
|
-
proc.parameters[parameter_index] == parameter
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.non_default_methods(class_or_instance)
|
38
|
-
class_or_instance.is_a?(Module) ? non_default_class_methods(class_or_instance) : non_default_instance_methods(class_or_instance)
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.non_default_class_methods(clazz)
|
42
|
-
clazz.methods - Object.methods
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.non_default_instance_methods(instance)
|
46
|
-
instance.methods - Object.new.methods
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
1
|
+
require_relative 'reflection_utils/create_class_methods_upon_include'
|
2
|
+
|
3
|
+
module ReflectionUtils
|
4
|
+
|
5
|
+
def self.get_bound_instance_method(instance:, method_name:)
|
6
|
+
get_class_constant(instance: instance).instance_method(method_name).bind(instance)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.get_class_constant(instance:)
|
10
|
+
Object.const_get(instance.class.name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.call(callback, params = nil)
|
14
|
+
params.nil? ? callback.call : callback.call(params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.has_parameters?(proc, parameters)
|
18
|
+
parameters.each_with_index do |parameter, index|
|
19
|
+
return false unless has_parameter?(proc, index, parameter)
|
20
|
+
end
|
21
|
+
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.assert_parameters(proc, parameters)
|
26
|
+
parameters.each_with_index do |parameter, index|
|
27
|
+
raise ArgumentError, "Argument #{parameter} not found for #{proc} at #{index} index." unless has_parameter?(proc, index, parameter)
|
28
|
+
end
|
29
|
+
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.has_parameter?(proc, parameter_index, parameter)
|
34
|
+
proc.parameters[parameter_index] == parameter
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.non_default_methods(class_or_instance)
|
38
|
+
class_or_instance.is_a?(Module) ? non_default_class_methods(class_or_instance) : non_default_instance_methods(class_or_instance)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.non_default_class_methods(clazz)
|
42
|
+
clazz.methods - Object.methods
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.non_default_instance_methods(instance)
|
46
|
+
instance.methods - Object.new.methods
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module ReflectionUtils
|
2
|
-
VERSION = "0.2.
|
3
|
-
end
|
1
|
+
module ReflectionUtils
|
2
|
+
VERSION = "0.2.1"
|
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.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thisismydesign
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coveralls
|