From 98719d085b3b81fd38a00d6395c7ef2c879c7db7 Mon Sep 17 00:00:00 2001 From: Hopper Gee Date: Wed, 18 Dec 2024 01:48:20 +0800 Subject: [PATCH 1/2] Support inheritance --- lib/active_method/base.rb | 33 +++++--------------------- test/test_active_method.rb | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/lib/active_method/base.rb b/lib/active_method/base.rb index b95cf50..b12b9fe 100644 --- a/lib/active_method/base.rb +++ b/lib/active_method/base.rb @@ -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 @@ -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) diff --git a/test/test_active_method.rb b/test/test_active_method.rb index b2f9e4d..7ca997e 100644 --- a/test/test_active_method.rb +++ b/test/test_active_method.rb @@ -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 From 9f32d5b10bf36ec9ad09b8b85ee418674fe364de Mon Sep 17 00:00:00 2001 From: Hopper Gee Date: Wed, 18 Dec 2024 01:49:37 +0800 Subject: [PATCH 2/2] Bump to 1.5.0 --- CHANGELOG.md | 4 ++++ lib/active_method/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5c692b..594d41d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## [Unreleased] +## [1.5.0] - 2024-12-18 + +- Support inheritance + ## [1.4.0] - 2024-06-23 - Support customize method owner name diff --git a/lib/active_method/version.rb b/lib/active_method/version.rb index 4e0fc24..a0e39df 100644 --- a/lib/active_method/version.rb +++ b/lib/active_method/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ActiveMethod - VERSION = "1.4.0" + VERSION = "1.5.0" end