-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreator.rb
More file actions
94 lines (79 loc) · 2.32 KB
/
Copy pathcreator.rb
File metadata and controls
94 lines (79 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true
module Kiba
module Extend
module Registry
# Bundles up the logic/options of different ways of validating and
# calling registry entry creators
class Creator
attr_reader :mod, :meth, :args
def initialize(spec)
@spec = spec.is_a?(Proc) ? spec.call : spec
if Kiba::Extend.job_verbosity == :debug
puts "Initializing Creator class for #{@spec}"
end
@mod = nil
@meth = nil
@args = nil
set_vars
end
def call
job = args ? mod.send(meth, **args) : mod.send(meth)
job.run
job
end
def to_s
mod_meth = "#{mod}.#{meth}"
return mod_meth unless args
arg_str = args.map { |key, val| "#{key}: #{val}" }
.join(", ")
"#{mod_meth}(#{arg_str})"
end
private
attr_reader :spec
def args_type_ok?
spec[:args].is_a?(Hash)
end
def callee_ok?
callee = spec[:callee]
callee.is_a?(Method) || callee.is_a?(Module)
end
def set_vars
case spec.class.to_s
when "Method"
setup_method_spec
when "Module"
setup_module_spec
when "Hash"
setup_hash_spec
else
raise TypeError.new(spec)
end
end
def setup_hash_spec
raise HashCreatorKeyError.new unless spec.key?(:callee)
raise HashCreatorCalleeError.new(spec[:callee]) unless callee_ok?
raise HashCreatorArgsTypeError.new(spec[:args]) unless args_type_ok?
@args = spec[:args]
callee = spec[:callee]
if callee.is_a?(Method)
setup_method_spec(callee)
else
setup_module_spec(callee)
end
end
def setup_method_spec(using = spec)
@meth = using.name
@mod = using.receiver
end
def setup_module_spec(using = spec)
default_job_method = Kiba::Extend.default_job_method_name
unless using.private_method_defined?(default_job_method)
raise JoblessModuleCreatorError.new(using)
end
@mod = using
@meth = default_job_method
end
end
end
end
end