active_method 0.5.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d5e11d7ed75a94873fb99d6f8b03300800a76b5b400794d195dc4127ceeb3e2
4
- data.tar.gz: 8004a835cfd838a079467e1d5307216f4ca2ec2e84a48819ff46f99dd8372745
3
+ metadata.gz: '09981bf259603ed5a3702d558f61812780801c59af7dc1f61e0a8a4d9785e211'
4
+ data.tar.gz: 6ac6cc4ab0f822592bd355f2d57cb9c7bddfd5d743685e2e20115da8a6f6eae3
5
5
  SHA512:
6
- metadata.gz: 2f97a88bb3f53bd43e29823b2ec9bc27251b76a988e28ec510d60ab207433bad35545d93407ae217546d3ed9c56fff1078315745e6e4f74092430dc8c62653a8
7
- data.tar.gz: '038421de6e5ce8df43ec48c3abd72c67d12ce54ffd0bbe99c24407f47fc8efddf5b12fe56ebe6123ac875d8649993ed01a0e239c3eaf5bc6c64ea2856ad990f6'
6
+ metadata.gz: 44df9e385113755724745f2fc0a022b71d44611b7443f627dc04a78dc3b26d529259315ee8eaada7172fc6791a2eab22b0f2ee9c368b1c933d1c25c2314ee1b1
7
+ data.tar.gz: eb4cb181d3112ae398aaae750a7bf431f71265d21f0b6084fc804bd4368e78bb07fbabc14bd756a00f1e840299e1d2e9ea5521cb03591b4e0ba781043c88b9a2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.1.0] - 2022-10-08
4
+
5
+ - Support `module_function`
6
+
7
+ ## [1.0.0] - 2022-09-16
8
+
9
+ - Now you can use `.active_method` through `include ActiveMethod`
10
+ - Get each method with owner like `Foo[user].call(a: 1)` for testing
3
11
  ## [0.5.0] - 2022-08-28
4
12
 
5
13
  - Initial release
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # ActiveMethod
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_method`. To experiment with that code, run `bin/console` for an interactive prompt.
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,111 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
14
 
17
15
  ## Usage
18
16
 
19
- TODO: Write usage instructions here
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
+ ```
89
+
90
+ ### Work on module
91
+
92
+ ```ruby
93
+ class Hi < ActiveMethod::Base
94
+ argument :name
95
+
96
+ def call
97
+ "Hi, #{name}"
98
+ end
99
+ end
100
+
101
+ module Say
102
+ include ActiveMethod
103
+
104
+ active_method :hi, module_function: true
105
+ end
106
+
107
+ Say.hi('John')
108
+ # => "Hi, John"
109
+ ```
20
110
 
21
111
  ## Development
22
112
 
23
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
-
25
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
113
+ ```bash
114
+ bundle install
115
+ meval rake # Run test
116
+ meval -a rake # Run tests against all Ruby versions and Rails versions
117
+ ```
26
118
 
27
119
  ## Contributing
28
120
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/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/[USERNAME]/active_method/blob/master/CODE_OF_CONDUCT.md).
121
+ 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
122
 
31
123
  ## License
32
124
 
@@ -34,4 +126,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
34
126
 
35
127
  ## Code of Conduct
36
128
 
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/[USERNAME]/active_method/blob/master/CODE_OF_CONDUCT.md).
129
+ 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).
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveMethod
4
- VERSION = "0.5.0"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/active_method.rb CHANGED
@@ -3,5 +3,25 @@
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, **options)
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
+
22
+ if options[:module_function]
23
+ module_function name
24
+ end
25
+ end
26
+ end
7
27
  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.5.0
4
+ version: 1.1.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-08-28 00:00:00.000000000 Z
11
+ date: 2022-10-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Refactor your obscure method to a method object
14
14
  email:
@@ -20,12 +20,13 @@ files:
20
20
  - CHANGELOG.md
21
21
  - CODE_OF_CONDUCT.md
22
22
  - Gemfile
23
- - Gemfile.lock
24
23
  - LICENSE.txt
25
24
  - README.md
26
25
  - Rakefile
27
26
  - lib/active_method.rb
28
27
  - lib/active_method/base.rb
28
+ - lib/active_method/executor.rb
29
+ - lib/active_method/util.rb
29
30
  - lib/active_method/version.rb
30
31
  - matrixeval.yml
31
32
  homepage: https://github.com/hoppergee/active_method
data/Gemfile.lock DELETED
@@ -1,50 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- active_method (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- concurrent-ruby (1.1.10)
10
- debug (1.6.2)
11
- irb (>= 1.3.6)
12
- reline (>= 0.3.1)
13
- docile (1.4.0)
14
- io-console (0.5.11)
15
- irb (1.4.1)
16
- reline (>= 0.3.0)
17
- matrixeval (0.4.3)
18
- concurrent-ruby
19
- rainbow (~> 3.1)
20
- terminal-table
21
- matrixeval-ruby (0.4.0)
22
- matrixeval (~> 0.4.2)
23
- minitest (5.16.2)
24
- rainbow (3.1.1)
25
- rake (13.0.6)
26
- reline (0.3.1)
27
- io-console (~> 0.5)
28
- simplecov (0.21.2)
29
- docile (~> 1.1)
30
- simplecov-html (~> 0.11)
31
- simplecov_json_formatter (~> 0.1)
32
- simplecov-html (0.12.3)
33
- simplecov_json_formatter (0.1.4)
34
- terminal-table (3.0.2)
35
- unicode-display_width (>= 1.1.1, < 3)
36
- unicode-display_width (2.2.0)
37
-
38
- PLATFORMS
39
- arm64-darwin-21
40
-
41
- DEPENDENCIES
42
- active_method!
43
- debug
44
- matrixeval-ruby
45
- minitest (~> 5.0)
46
- rake (~> 13.0)
47
- simplecov
48
-
49
- BUNDLED WITH
50
- 2.3.12