active_method 0.5.0 → 1.0.0
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +80 -9
- data/lib/active_method/base.rb +14 -0
- data/lib/active_method/executor.rb +18 -0
- data/lib/active_method/util.rb +43 -0
- data/lib/active_method/version.rb +1 -1
- data/lib/active_method.rb +16 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 223f8bbbc57474a6cc9d23abcc64e3e813897cb351e1ffd2ff80405e3e6e6769
|
4
|
+
data.tar.gz: 75b7a12d9080d3e268be90cf9cca439e946312758c5c72720482761abed1e01c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aefd7ff1a3160ea7b59cccd3ddb5b1b2baed523a34557f1df65db497df66f89501fb8ded964776e3abc2faac8ca8665e70ea6c17f786b99f54810e1420cf662c
|
7
|
+
data.tar.gz: c5294d303371c0e9e8da8840131b13abb0cbc694ecd2d423f848373ececbc51689198800d81c7275834520378333ccf5ae0f92b11ae1f4b304f74fed20925e2d
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# ActiveMethod
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Refactor your obscure method to a method object with `ActiveMethod`
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -16,17 +14,90 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
16
14
|
|
17
15
|
## Usage
|
18
16
|
|
19
|
-
|
17
|
+
Refactor `Foo#bar` to `Bar` with `ActiveMethod`
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class Foo
|
21
|
+
def bar(a, b, c: , d:)
|
22
|
+
puts "a: #{a}"
|
23
|
+
puts "b: #{b}"
|
24
|
+
puts "c: #{c}"
|
25
|
+
puts "d: #{d}"
|
26
|
+
|
27
|
+
buzz
|
28
|
+
end
|
29
|
+
|
30
|
+
def buzs
|
31
|
+
puts 'buzz'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Refactor to:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class Bar < ActiveMethod::Base
|
40
|
+
argument :a
|
41
|
+
argument :b, default: 2
|
42
|
+
keyword_argument :c
|
43
|
+
keyword_argument :d, default: 4
|
44
|
+
|
45
|
+
def call
|
46
|
+
puts "a: #{a}"
|
47
|
+
puts "b: #{b}"
|
48
|
+
puts "c: #{c}"
|
49
|
+
puts "d: #{d}"
|
50
|
+
|
51
|
+
foo.buzz
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Foo
|
56
|
+
include ActiveMethod
|
57
|
+
|
58
|
+
active_method :bar
|
59
|
+
end
|
60
|
+
|
61
|
+
Bar.call(1)
|
62
|
+
# => a: 1
|
63
|
+
# => b: 2
|
64
|
+
# => c: nil
|
65
|
+
# => d: 4
|
66
|
+
# => buzz
|
67
|
+
|
68
|
+
Bar.call(1, 3)
|
69
|
+
# => a: 1
|
70
|
+
# => b: 3
|
71
|
+
# => c: nil
|
72
|
+
# => d: 4
|
73
|
+
# => buzz
|
74
|
+
|
75
|
+
Bar.call(1, 3, c: 6)
|
76
|
+
# => a: 1
|
77
|
+
# => b: 3
|
78
|
+
# => c: 6
|
79
|
+
# => d: 4
|
80
|
+
# => buzz
|
81
|
+
|
82
|
+
Bar.call(1, 3, c: 4, d: 5)
|
83
|
+
# => a: 1
|
84
|
+
# => b: 3
|
85
|
+
# => c: 4
|
86
|
+
# => d: 5
|
87
|
+
# => buzz
|
88
|
+
```
|
20
89
|
|
21
90
|
## Development
|
22
91
|
|
23
|
-
|
24
|
-
|
25
|
-
|
92
|
+
```bash
|
93
|
+
bundle install
|
94
|
+
meval rake # Run test
|
95
|
+
meval -a rake # Run tests against all Ruby versions and Rails versions
|
96
|
+
```
|
26
97
|
|
27
98
|
## Contributing
|
28
99
|
|
29
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hoppergee/active_method. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hoppergee/active_method/blob/master/CODE_OF_CONDUCT.md).
|
30
101
|
|
31
102
|
## License
|
32
103
|
|
@@ -34,4 +105,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
34
105
|
|
35
106
|
## Code of Conduct
|
36
107
|
|
37
|
-
Everyone interacting in the ActiveMethod project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
108
|
+
Everyone interacting in the ActiveMethod project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hoppergee/active_method/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/active_method/base.rb
CHANGED
@@ -12,6 +12,11 @@ module ActiveMethod
|
|
12
12
|
|
13
13
|
class << self
|
14
14
|
|
15
|
+
def [](owner)
|
16
|
+
Executor.new(self, owner)
|
17
|
+
end
|
18
|
+
alias_method :on, :[]
|
19
|
+
|
15
20
|
def call(*args)
|
16
21
|
new(*args).call
|
17
22
|
end
|
@@ -96,6 +101,15 @@ module ActiveMethod
|
|
96
101
|
end
|
97
102
|
end
|
98
103
|
|
104
|
+
def __set_owner(owner)
|
105
|
+
@__method_owner = owner
|
106
|
+
|
107
|
+
instance_name = Util.snake_case(owner.class.name.split("::").last)
|
108
|
+
self.class.define_method instance_name do
|
109
|
+
@__method_owner
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
99
113
|
def call
|
100
114
|
end
|
101
115
|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveMethod
|
2
|
+
class Executor
|
3
|
+
|
4
|
+
attr_reader :method_class, :owner
|
5
|
+
|
6
|
+
def initialize(klass, owner)
|
7
|
+
@method_class = klass
|
8
|
+
@owner = owner
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(*args)
|
12
|
+
method = method_class.new(*args)
|
13
|
+
method.__set_owner(owner)
|
14
|
+
method.call
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module ActiveMethod
|
2
|
+
module Util
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def constantize(klass, name)
|
6
|
+
constant = klass.const_get(name) rescue nil
|
7
|
+
return constant unless constant.nil?
|
8
|
+
|
9
|
+
namespaces_of(klass).each do |namespace|
|
10
|
+
constant = namespace.const_get(name) rescue nil
|
11
|
+
return constant unless constant.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
raise NameError.new("wrong constant name #{name}") if constant.nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def namespaces_of(klass)
|
18
|
+
namespaces = []
|
19
|
+
|
20
|
+
names = klass.name.split("::")
|
21
|
+
names.pop
|
22
|
+
while !names.empty?
|
23
|
+
namespaces << Object.const_get(names.join("::"))
|
24
|
+
names.pop
|
25
|
+
end
|
26
|
+
|
27
|
+
namespaces
|
28
|
+
end
|
29
|
+
|
30
|
+
def camel_case(str)
|
31
|
+
str = str.to_s
|
32
|
+
return str if str !~ /_/ && str =~ /[A-Z]+.*/
|
33
|
+
str.split("_").map(&:capitalize).join
|
34
|
+
end
|
35
|
+
|
36
|
+
def snake_case(str)
|
37
|
+
return str.downcase if str =~ /^[A-Z_]+$/
|
38
|
+
str.gsub(/\B[A-Z]/, '_\&').squeeze("_") =~ /_*(.*)/
|
39
|
+
$+.downcase
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
data/lib/active_method.rb
CHANGED
@@ -3,5 +3,21 @@
|
|
3
3
|
require_relative "active_method/version"
|
4
4
|
|
5
5
|
module ActiveMethod
|
6
|
+
autoload :Util, "active_method/util"
|
7
|
+
autoload :Executor, "active_method/executor"
|
6
8
|
autoload :Base, "active_method/base"
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
base.extend ClassMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def active_method(name, method_class = nil)
|
16
|
+
method_class ||= Util.constantize self, Util.camel_case(name)
|
17
|
+
|
18
|
+
define_method name do |*args|
|
19
|
+
method_class[self].call(*args)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
7
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_method
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hopper Gee
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Refactor your obscure method to a method object
|
14
14
|
email:
|
@@ -26,6 +26,8 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- lib/active_method.rb
|
28
28
|
- lib/active_method/base.rb
|
29
|
+
- lib/active_method/executor.rb
|
30
|
+
- lib/active_method/util.rb
|
29
31
|
- lib/active_method/version.rb
|
30
32
|
- matrixeval.yml
|
31
33
|
homepage: https://github.com/hoppergee/active_method
|