Module: InsaneHook::ClassMethods
Constant Summary
Constants included
from Constants
InsaneHook::Constants::ARGS_VAR, InsaneHook::Constants::NO_ARG, InsaneHook::Constants::OPTIONAL_ARGS, InsaneHook::Constants::REQUIRED_ARGS, InsaneHook::Constants::RESULT_VAR
Instance Method Summary
collapse
Instance Method Details
#call(**args, &block) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/insane_hook/class_methods.rb', line 39
def call(**args, &block)
if block_given?
raise "Block cannot take arguments" if block.arity > 0
raise "call method already defined" if self.instance_methods.include?(:call)
define_method(:call) do
result(nil)
instance_eval(&block)
self
end
else
new(**args).call
end
end
|
#fallbacks(key) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/insane_hook/class_methods.rb', line 32
def fallbacks(key)
fail "#{key} is not a symbol" unless key.is_a? Symbol
args = self.class_variable_get(ARGS_VAR)
args[OPTIONAL_ARGS] << key
self.class_variable_set(ARGS_VAR, args)
end
|
#new(**args) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/insane_hook/class_methods.rb', line 9
def new(**args)
obj = self.allocate
self.class_variable_get(ARGS_VAR)[REQUIRED_ARGS].each do |var|
value = args.fetch(var) { raise(MissingArgumentError, "#{var} not provided in #{self.class}") }
obj.instance_variable_set("@#{var}", value)
obj.class.class_eval{attr_reader var}
end
self.class_variable_get(ARGS_VAR)[OPTIONAL_ARGS].each do |var|
value = args.fetch(var, nil)
obj.instance_variable_set("@#{var}", value)
obj.class.class_eval{attr_reader var}
end
obj.send :initialize
obj
end
|
#requires(key) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/insane_hook/class_methods.rb', line 25
def requires(key)
fail "#{key} is not a symbol" unless key.is_a? Symbol
args = self.class_variable_get(ARGS_VAR)
args[REQUIRED_ARGS] << key
self.class_variable_set(ARGS_VAR, args)
end
|