Skip to content

Support inheritance #13

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

Merged
merged 2 commits into from
Dec 17, 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.5.0] - 2024-12-18

- Support inheritance

## [1.4.0] - 2024-06-23

- Support customize method owner name
Expand Down
33 changes: 6 additions & 27 deletions lib/active_method/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@ def call(*args, &block)
end

def arguments
class_variable_get(:@@arguments)
@arguments ||= {}
end

def keyword_arguments
class_variable_get(:@@keyword_arguments)
@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
return @owner_name unless @owner_name.nil?
return if self == ActiveMethod::Base
superclass.owner_name
end

protected
Expand Down Expand Up @@ -75,26 +71,9 @@ def parse_keyword_argument(name, opts = {})
end

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

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

self.class_variable_set(:@@arguments, {})
@owner_name = name
end

def init_keyword_arguments
return if self.class_variable_defined?(:@@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 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.4.0"
VERSION = "1.5.0"
end
47 changes: 47 additions & 0 deletions test/test_active_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,51 @@ class Laptop
assert laptop.off
end

################
# .active_method with inheritance
################

class DemoAPIBase < ActiveMethod::Base
owner :client
end

class GetA < DemoAPIBase
argument :id

def call
puts "Request GET: /a"
end
end

class GetB < DemoAPIBase
keyword_argument :first
keyword_argument :per

def call
puts "Request GET: /b"
end
end

class Client
include ActiveMethod
active_method :get_a
active_method :get_b
end

it ".active_method can orgianize with a Base methd" do
get_a_arguments = GetA.arguments.values
assert_equal 1, get_a_arguments.count
assert_equal :id, get_a_arguments[0].name
get_a_keyword_arguments = GetA.keyword_arguments
assert_equal 0, get_a_keyword_arguments.count
assert_equal :client, GetA.owner_name

get_b_arguments = GetB.arguments.values
assert_equal 0, get_b_arguments.count
get_b_keyword_arguments = GetB.keyword_arguments
assert_equal :first, get_b_keyword_arguments[0].name
assert_equal :per, get_b_keyword_arguments[1].name
assert_equal :client, GetB.owner_name
end

end
Loading