Class: Jets::Router::MethodCreator::Code
- Inherits:
-
Object
- Object
- Jets::Router::MethodCreator::Code
show all
- Includes:
- Util
- Defined in:
- lib/jets/router/method_creator/code.rb
Instance Method Summary
collapse
Methods included from Util
#get_controller_action, #handle_on!, #join, #underscore
Constructor Details
#initialize(options, scope, controller, action = nil) ⇒ Code
Returns a new instance of Code.
5
6
7
8
|
# File 'lib/jets/router/method_creator/code.rb', line 5
def initialize(options, scope, controller, action=nil)
@options, @scope, @controller, @action = options, scope, controller, action
@path, @as = options[:path], options[:as]
end
|
Instance Method Details
#action ⇒ Object
35
36
37
|
# File 'lib/jets/router/method_creator/code.rb', line 35
def action
@action || self.class.name.split('::').last.downcase end
|
#full_as ⇒ Object
39
40
41
|
# File 'lib/jets/router/method_creator/code.rb', line 39
def full_as
@scope&.full_as
end
|
#full_meth_name(suffix = nil) ⇒ Object
51
52
53
54
55
|
# File 'lib/jets/router/method_creator/code.rb', line 51
def full_meth_name(suffix=nil)
as = @as || meth_name
name = [as, suffix].compact.join('_')
underscore(name)
end
|
#full_path ⇒ Object
30
31
32
33
|
# File 'lib/jets/router/method_creator/code.rb', line 30
def full_path
route = Jets::Router::Route.new(@options, @scope)
route.compute_path
end
|
#meth_args ⇒ Object
10
11
12
13
14
15
|
# File 'lib/jets/router/method_creator/code.rb', line 10
def meth_args
params = full_path.split('/').select { |x| x.include?(':') }
items = params.map { |x| x.sub(':','') }
items.empty? ? nil : "("+items.join(', ')+")"
end
|
#meth_result ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/jets/router/method_creator/code.rb', line 17
def meth_result
results = full_path.split('/').map do |x|
if x.include?(':')
variable = x.sub(':','')
"\#{#{variable}.to_param}"
else
x
end
end
'/' + results.join('/') unless results.empty?
end
|
#method_name_leaf ⇒ Object
The method_name_leaf is used to generate method names. Can be nil because sometimes the name is fully acquired from the scope.
45
46
47
48
49
|
# File 'lib/jets/router/method_creator/code.rb', line 45
def method_name_leaf
unless %w[resource resources].include?(@scope.from.to_s) && @options[:from_scope]
@controller
end
end
|
#param_name(name) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/jets/router/method_creator/code.rb', line 78
def param_name(name)
name.to_s.split('/').last.singularize + "_id"
end
|
#path_method ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/jets/router/method_creator/code.rb', line 57
def path_method
return if @as == :disabled
<<~EOL
def #{full_meth_name(:path)}#{meth_args}
"#{meth_result}"
end
EOL
end
|
#url_method ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/jets/router/method_creator/code.rb', line 66
def url_method
return if @as == :disabled
path_method_call = "#{full_meth_name(:path)}#{meth_args}"
<<~EOL
def #{full_meth_name(:url)}#{meth_args}
"\#{ENV['JETS_HOST']}\#{#{path_method_call}}"
end
EOL
end
|