Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize owner name #12

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [1.4.0] - 2024-06-23

- Support customize method owner name

## [1.3.0] - 2022-12-23

- Support &block
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ User.new.foo('aa', 3, c: 'cc', d: 5)
#=> a: aa, b: 3, c: cc, d: 5
```

### Get the owner of the method
### Customize the owner name of the method

```ruby
class User
Expand All @@ -63,8 +63,10 @@ class User
end

class Hi < ActiveMethod::Base
onwer :support

def call
puts "Hi, I'm a #{@__method_owner.fromal_name}. Please call me #{user.name}"
puts "Hi, I'm a #{support.fromal_name}. Please call me #{user.name}"
end
end

Expand Down
27 changes: 24 additions & 3 deletions lib/active_method/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ def keyword_arguments
class_variable_get(:@@keyword_arguments)
end

def owner_name
class_variable_get(:@@owner_name)
end

def inherited(subclass)
subclass.init_arguments
subclass.init_keyword_arguments
subclass.init_owner_name
end

protected
Expand All @@ -42,6 +47,8 @@ def method_missing(method_name, *args)
parse_argument(method_name, *args)
when 'keyword_argument'
parse_keyword_argument(*args)
when 'owner'
parse_method_owner(*args)
else
super
end
Expand All @@ -67,6 +74,10 @@ def parse_keyword_argument(name, opts = {})
end
end

def parse_method_owner(name)
class_variable_set(:@@owner_name, name)
end

def init_arguments
return if self.class_variable_defined?(:@@arguments)

Expand All @@ -78,6 +89,12 @@ def init_keyword_arguments

self.class_variable_set(:@@keyword_arguments, [])
end

def init_owner_name
return if self.class_variable_defined?(:@@owner_name)

self.class_variable_set(:@@owner_name, nil)
end
end

def initialize(*args)
Expand All @@ -104,9 +121,13 @@ def initialize(*args)
def __set_owner(owner)
@__method_owner = owner

klass = owner.is_a?(Class) ? owner : owner.class
instance_name = Util.snake_case(klass.name.split("::").last)
self.define_singleton_method instance_name do
@__owner_name = self.class.owner_name
if @__owner_name.nil?
klass = owner.is_a?(Class) ? owner : owner.class
@__owner_name = Util.snake_case(klass.name.split("::").last)
end

self.define_singleton_method @__owner_name do
@__method_owner
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_method/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ActiveMethod
VERSION = "1.3.0"
VERSION = "1.4.0"
end
8 changes: 7 additions & 1 deletion matrixeval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ matrix:
container:
image: ruby:3.0.4
- key: 3.1
default: true
container:
image: ruby:3.1.2
- key: 3.2
container:
image: ruby:3.2.4
- key: 3.3
default: true
container:
image: ruby:3.3.4
# - key: jruby-9.3
# container:
# image: jruby:9.3
Expand Down
34 changes: 34 additions & 0 deletions test/test_active_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,38 @@ def a=(value)
assert_equal 'aaa', ClassConfiguration.a
end

################
# .active_method customize owner name
################

class PowerOff < ActiveMethod::Base
owner :machine

def call
machine.off = true
end
end

class Desktop
include ActiveMethod
active_method :power_off
attr_accessor :off
end

class Laptop
include ActiveMethod
active_method :power_off
attr_accessor :off
end

it ".active_method can customize owenr name for sharing to work like a concern" do
desktop = Desktop.new
desktop.power_off
assert desktop.off

laptop = Laptop.new
laptop.power_off
assert laptop.off
end

end
Loading