Skip to content

Commit 96cbf2b

Browse files
committed
Implement a draft version of ActiveMethod
1 parent 52d70ed commit 96cbf2b

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

lib/active_method.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
require_relative "active_method/version"
44

55
module ActiveMethod
6-
class Error < StandardError; end
7-
# Your code goes here...
6+
autoload :Base, "active_method/base"
87
end

lib/active_method/base.rb

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
module ActiveMethod
2+
class Base
3+
4+
class Argument
5+
attr_reader :name, :default
6+
7+
def initialize(name, default: nil)
8+
@name = name
9+
@default = default
10+
end
11+
end
12+
13+
class << self
14+
15+
def call(*args)
16+
new(*args).call
17+
end
18+
19+
def arguments
20+
class_variable_get(:@@arguments)
21+
end
22+
23+
def keyword_arguments
24+
class_variable_get(:@@keyword_arguments)
25+
end
26+
27+
private
28+
29+
def method_missing(method_name, *args)
30+
case method_name.to_s
31+
when /^argument(_\d)*$/
32+
init_arguments
33+
parse_argument(method_name, *args)
34+
when 'keyword_argument'
35+
init_keyword_arguments
36+
parse_keyword_argument(*args)
37+
else
38+
super
39+
end
40+
end
41+
42+
def parse_argument(method_name, name, opts = {})
43+
_, index = method_name.to_s.split('_')
44+
if index.nil?
45+
index = (arguments.keys.max || 0) + 1
46+
end
47+
arguments[Integer(index)] = Argument.new(name, **opts)
48+
49+
define_method name do
50+
instance_variable_get("@#{name}")
51+
end
52+
end
53+
54+
def parse_keyword_argument(name, opts = {})
55+
keyword_arguments << Argument.new(name, **opts)
56+
57+
define_method name do
58+
instance_variable_get("@#{name}")
59+
end
60+
end
61+
62+
def init_arguments
63+
return if self.class_variable_defined?(:@@arguments)
64+
65+
self.class_variable_set(:@@arguments, {})
66+
end
67+
68+
def init_keyword_arguments
69+
return if self.class_variable_defined?(:@@keyword_arguments)
70+
71+
self.class_variable_set(:@@keyword_arguments, [])
72+
end
73+
end
74+
75+
def initialize(*args)
76+
arguments.each do |index, argument|
77+
begin
78+
instance_variable_set("@#{argument.name}", args.fetch(index - 1))
79+
rescue IndexError
80+
instance_variable_set("@#{argument.name}", argument.default)
81+
end
82+
end
83+
84+
opts = args[arguments.count] || {}
85+
keyword_arguments.each do |argument|
86+
if opts.key?(argument.name.to_sym)
87+
instance_variable_set("@#{argument.name}", opts[argument.name.to_sym])
88+
elsif opts.key?(argument.name.to_s)
89+
instance_variable_set("@#{argument.name}", opts[argument.name.to_s])
90+
else
91+
instance_variable_set("@#{argument.name}", argument.default)
92+
end
93+
end
94+
end
95+
96+
def call
97+
end
98+
99+
private
100+
101+
def arguments
102+
self.class.arguments
103+
end
104+
105+
def keyword_arguments
106+
self.class.keyword_arguments
107+
end
108+
109+
end
110+
end

0 commit comments

Comments
 (0)