active_method 1.2.1 → 1.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfbff213fde68c49d9a524d227aa9dc651160bbe5b72973b62991d718e092d08
4
- data.tar.gz: 2b40034fb74458e547196d8c990ca6cf814950c2be638aefdd9829df4c10fcc2
3
+ metadata.gz: b181be6c86bef5b467ce95e50c45fc7518c21e459fda43745410ec16e38263fe
4
+ data.tar.gz: 29b824f426b078e8237808012871aa355d316c291850daddc1e736576fb29850
5
5
  SHA512:
6
- metadata.gz: ee417c29294f6ef9ce18f397eddc770c420ca370d460fa4e831708f3f8e673493971eae1fc6522776c7a440d3721eb1308584dbc1310cb8b259c096040822012
7
- data.tar.gz: 50a1753a7745ab0248fcbc54164a3a93b9b8bfccb446fe2bc1b12b69bb2f4b61e5dd92d781e5ea64e1eb32ad7e595486d64a03481307afab67c92c2a4553c26f
6
+ metadata.gz: 02ba2481e2d0ae0cfa55f31c74720b52f19ee28b753f343ebdfada18c1ee0abfb809bb43cfd50636fedd9c3815a35510eebf38c1d0a50f9c6f1a0fa7035ebc73
7
+ data.tar.gz: f3ce2172f62eb8a43be03a84602691a8d75240c001ab22c9805332f8ddc24b4e70c68c5c0c5541a22d596eb6f4d2436b83188aa0c65229c55e45384f8d48b582
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.3.0] - 2022-12-23
4
+
5
+ - Support &block
6
+
3
7
  ## [1.2.1] - 2022-11-21
4
8
 
5
9
  - Build on main branch
data/README.md CHANGED
@@ -1,132 +1,78 @@
1
1
  # ActiveMethod
2
2
 
3
- Refactor your obscure method to a method object with `ActiveMethod`
3
+ Method is an object in Ruby, but we need **a real method object**. Welcome to `ActiveMethod`!
4
4
 
5
- ## Installation
5
+ - **Clean scope** without module
6
+ - **High maintainability** with tiny method objects
7
+ - Make wrting OO **easy**, then writing OO **everywhere**
6
8
 
7
- Install the gem and add to the application's Gemfile by executing:
9
+ ![](./hero.png)
8
10
 
9
- $ bundle add active_method
11
+ ## Installation
10
12
 
11
- If bundler is not being used to manage dependencies, install the gem by executing:
13
+ ```bash
14
+ $ bundle add active_method
15
+ ```
12
16
 
13
- $ gem install active_method
17
+ ## API
14
18
 
15
- ## Usage
19
+ **It just do what a method do, and nothing else!**
16
20
 
17
- Refactor `Foo#bar` to `Bar` with `ActiveMethod`
21
+ ### Define method object
18
22
 
19
23
  ```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
24
+ class User
25
+ include ActiveMethod
29
26
 
30
- def buzs
31
- puts 'buzz'
32
- end
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
- Refactor to:
37
+ ### Declaring arguments
37
38
 
38
39
  ```ruby
39
- class Bar < ActiveMethod::Base
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
- 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
51
+ User.new.foo('aa', 3, c: 'cc', d: 5)
52
+ #=> a: aa, b: 3, c: cc, d: 5
88
53
  ```
89
54
 
90
- ### Work on module
55
+ ### Get the owner of the method
91
56
 
92
57
  ```ruby
93
- class Hi < ActiveMethod::Base
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
- active_method :hi, module_function: true
60
+ attr_accessor :name
61
+ attr_accessor :fromal_name
62
+ active_method :hi
105
63
  end
106
64
 
107
- Say.hi('John')
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
- "#{robot_factory} build a robot called #{name}"
67
+ puts "Hi, I'm a #{@__method_owner.fromal_name}. Please call me #{user.name}"
119
68
  end
120
69
  end
121
70
 
122
- class RobotFactory
123
- include ActiveMethod
124
-
125
- active_method :build_a_robot, class_method: true
126
- end
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
@@ -17,8 +17,8 @@ module ActiveMethod
17
17
  end
18
18
  alias_method :on, :[]
19
19
 
20
- def call(*args)
21
- new(*args).call
20
+ def call(*args, &block)
21
+ new(*args).call(&block)
22
22
  end
23
23
 
24
24
  def arguments
@@ -8,10 +8,10 @@ module ActiveMethod
8
8
  @owner = owner
9
9
  end
10
10
 
11
- def call(*args)
11
+ def call(*args, &block)
12
12
  method = method_class.new(*args)
13
13
  method.__set_owner(owner)
14
- method.call
14
+ method.call(&block)
15
15
  end
16
16
 
17
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveMethod
4
- VERSION = "1.2.1"
4
+ VERSION = "1.3.0"
5
5
  end
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.2.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-21 00:00:00.000000000 Z
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