active_method 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +38 -92
- data/hero.png +0 -0
- data/lib/active_method/base.rb +2 -2
- data/lib/active_method/executor.rb +2 -2
- data/lib/active_method/version.rb +1 -1
- data/lib/active_method.rb +4 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b181be6c86bef5b467ce95e50c45fc7518c21e459fda43745410ec16e38263fe
|
4
|
+
data.tar.gz: 29b824f426b078e8237808012871aa355d316c291850daddc1e736576fb29850
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02ba2481e2d0ae0cfa55f31c74720b52f19ee28b753f343ebdfada18c1ee0abfb809bb43cfd50636fedd9c3815a35510eebf38c1d0a50f9c6f1a0fa7035ebc73
|
7
|
+
data.tar.gz: f3ce2172f62eb8a43be03a84602691a8d75240c001ab22c9805332f8ddc24b4e70c68c5c0c5541a22d596eb6f4d2436b83188aa0c65229c55e45384f8d48b582
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,132 +1,78 @@
|
|
1
1
|
# ActiveMethod
|
2
2
|
|
3
|
-
|
3
|
+
Method is an object in Ruby, but we need **a real method object**. Welcome to `ActiveMethod`!
|
4
4
|
|
5
|
-
|
5
|
+
- **Clean scope** without module
|
6
|
+
- **High maintainability** with tiny method objects
|
7
|
+
- Make wrting OO **easy**, then writing OO **everywhere**
|
6
8
|
|
7
|
-
|
9
|
+
![](./hero.png)
|
8
10
|
|
9
|
-
|
11
|
+
## Installation
|
10
12
|
|
11
|
-
|
13
|
+
```bash
|
14
|
+
$ bundle add active_method
|
15
|
+
```
|
12
16
|
|
13
|
-
|
17
|
+
## API
|
14
18
|
|
15
|
-
|
19
|
+
**It just do what a method do, and nothing else!**
|
16
20
|
|
17
|
-
|
21
|
+
### Define method object
|
18
22
|
|
19
23
|
```ruby
|
20
|
-
class
|
21
|
-
|
22
|
-
puts "a: #{a}"
|
23
|
-
puts "b: #{b}"
|
24
|
-
puts "c: #{c}"
|
25
|
-
puts "d: #{d}"
|
26
|
-
|
27
|
-
buzz
|
28
|
-
end
|
24
|
+
class User
|
25
|
+
include ActiveMethod
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
active_method :foo
|
28
|
+
active_method :bar, MyBar
|
29
|
+
active_method :build_one, class_method: true
|
30
|
+
end
|
31
|
+
|
32
|
+
module Util
|
33
|
+
active_method :parse_url, module_function: true
|
33
34
|
end
|
34
35
|
```
|
35
36
|
|
36
|
-
|
37
|
+
### Declaring arguments
|
37
38
|
|
38
39
|
```ruby
|
39
|
-
class
|
40
|
+
class Foo < ActiveMethod::Base
|
40
41
|
argument :a
|
41
42
|
argument :b, default: 2
|
42
43
|
keyword_argument :c
|
43
44
|
keyword_argument :d, default: 4
|
44
45
|
|
45
46
|
def call
|
46
|
-
puts "a: #{a}"
|
47
|
-
puts "b: #{b}"
|
48
|
-
puts "c: #{c}"
|
49
|
-
puts "d: #{d}"
|
50
|
-
|
51
|
-
foo.buzz
|
47
|
+
puts "a: #{a}, b: #{b}, c: #{c}, d: #{d}"
|
52
48
|
end
|
53
49
|
end
|
54
50
|
|
55
|
-
|
56
|
-
|
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
|
51
|
+
User.new.foo('aa', 3, c: 'cc', d: 5)
|
52
|
+
#=> a: aa, b: 3, c: cc, d: 5
|
88
53
|
```
|
89
54
|
|
90
|
-
###
|
55
|
+
### Get the owner of the method
|
91
56
|
|
92
57
|
```ruby
|
93
|
-
class
|
94
|
-
argument :name
|
95
|
-
|
96
|
-
def call
|
97
|
-
"Hi, #{name}"
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
module Say
|
58
|
+
class User
|
102
59
|
include ActiveMethod
|
103
|
-
|
104
|
-
|
60
|
+
attr_accessor :name
|
61
|
+
attr_accessor :fromal_name
|
62
|
+
active_method :hi
|
105
63
|
end
|
106
64
|
|
107
|
-
|
108
|
-
# => "Hi, John"
|
109
|
-
```
|
110
|
-
|
111
|
-
### Work as a class method
|
112
|
-
|
113
|
-
```ruby
|
114
|
-
class BuildARobot < ActiveMethod::Base
|
115
|
-
argument :name
|
116
|
-
|
65
|
+
class Hi < ActiveMethod::Base
|
117
66
|
def call
|
118
|
-
"#{
|
67
|
+
puts "Hi, I'm a #{@__method_owner.fromal_name}. Please call me #{user.name}"
|
119
68
|
end
|
120
69
|
end
|
121
70
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
RobotFactory.build_a_robot
|
129
|
-
# => "RobotFactory build a robot called B-2"
|
71
|
+
user = User.new
|
72
|
+
user.name = 'ActiveMethod'
|
73
|
+
user.fromal_name = 'method object'
|
74
|
+
user.hi
|
75
|
+
#=> Hi, I'm a method objectc, please call me ActiveMethod
|
130
76
|
```
|
131
77
|
|
132
78
|
## Development
|
data/hero.png
ADDED
Binary file
|
data/lib/active_method/base.rb
CHANGED
data/lib/active_method.rb
CHANGED
@@ -16,13 +16,13 @@ module ActiveMethod
|
|
16
16
|
method_class ||= Util.constantize self, Util.camel_case(name)
|
17
17
|
|
18
18
|
if options[:class_method]
|
19
|
-
define_singleton_method name do |*args|
|
20
|
-
method_class[self].call(*args)
|
19
|
+
define_singleton_method name do |*args, &block|
|
20
|
+
method_class[self].call(*args, &block)
|
21
21
|
end
|
22
22
|
|
23
23
|
else
|
24
|
-
define_method name do |*args|
|
25
|
-
method_class[self].call(*args)
|
24
|
+
define_method name do |*args, &block|
|
25
|
+
method_class[self].call(*args, &block)
|
26
26
|
end
|
27
27
|
|
28
28
|
if options[:module_function]
|
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: 1.
|
4
|
+
version: 1.3.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-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Refactor your obscure method to a method object
|
14
14
|
email:
|
@@ -23,6 +23,7 @@ files:
|
|
23
23
|
- LICENSE.txt
|
24
24
|
- README.md
|
25
25
|
- Rakefile
|
26
|
+
- hero.png
|
26
27
|
- lib/active_method.rb
|
27
28
|
- lib/active_method/base.rb
|
28
29
|
- lib/active_method/executor.rb
|