Class: ActionController::Routing::DynamicComponent
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Component
new
Constructor Details
#initialize(key, options = {}) ⇒ DynamicComponent
Returns a new instance of DynamicComponent.
93
94
95
96
97
|
# File 'lib/action_controller/routing.rb', line 93
def initialize(key, options = {})
@key = key.to_sym
default, @condition = options[:default], options[:condition]
self.default = default if options.key?(:default)
end
|
Instance Attribute Details
#condition ⇒ Object
Returns the value of attribute condition.
83
84
85
|
# File 'lib/action_controller/routing.rb', line 83
def condition
@condition
end
|
Returns the value of attribute default.
82
83
84
|
# File 'lib/action_controller/routing.rb', line 82
def default
@default
end
|
Returns the value of attribute key.
82
83
84
|
# File 'lib/action_controller/routing.rb', line 82
def key
@key
end
|
Instance Method Details
#add_segments_to(g) ⇒ Object
136
137
138
|
# File 'lib/action_controller/routing.rb', line 136
def add_segments_to(g)
g.add_segment(%(\#{CGI.escape(#{g.hash_value(key, true, default)})})) {|gp| yield gp}
end
|
#assign_default(g) ⇒ Object
171
172
173
|
# File 'lib/action_controller/routing.rb', line 171
def assign_default(g)
g.constant_result key, default unless default.nil?
end
|
#assign_result(g, with_default = false) ⇒ Object
166
167
168
169
|
# File 'lib/action_controller/routing.rb', line 166
def assign_result(g, with_default = false)
g.result key, "CGI.unescape(#{g.next_segment(true, with_default ? default : nil)})"
g.move_forward {|gp| yield gp}
end
|
#default_check(g) ⇒ Object
99
100
101
102
103
104
105
106
|
# File 'lib/action_controller/routing.rb', line 99
def default_check(g)
presence = "#{g.hash_value(key, !! default)}"
if default
"!(#{presence} && #{g.hash_value(key, false)} != #{default.to_s.inspect})"
else
"! #{presence}"
end
end
|
#dynamic? ⇒ Boolean
85
|
# File 'lib/action_controller/routing.rb', line 85
def dynamic?() true end
|
#optional? ⇒ Boolean
86
|
# File 'lib/action_controller/routing.rb', line 86
def optional?() @optional end
|
#recognition_check(g) ⇒ Object
140
141
142
143
144
145
146
147
|
# File 'lib/action_controller/routing.rb', line 140
def recognition_check(g)
test_type = [true, nil].include?(condition) ? :presence : :constraint
prefix = condition.is_a?(Regexp) ? "#{g.next_segment(true)} && " : ''
check = prefix + Routing.test_condition(g.next_segment(true), condition || true)
g.if(check) {|gp| yield gp, test_type}
end
|
#write_continue_generation(g, use_else) ⇒ Object
126
127
128
129
130
131
132
133
134
|
# File 'lib/action_controller/routing.rb', line 126
def write_continue_generation(g, use_else)
test = Routing.test_condition(g.hash_value(key, true, default), condition || true)
check = (use_else && condition.nil? && default) ? [:else] : [use_else ? :elsif : :if, test]
g.send(*check) do |gp|
gp.expire_for_keys(key) unless gp.after.empty?
add_segments_to(gp) {|gpp| gpp.continue}
end
end
|
#write_dropout_generation(g) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/action_controller/routing.rb', line 113
def write_dropout_generation(g)
return false unless optional? && g.after.all? {|c| c.optional?}
check = [default_check(g)]
gp = g.dup
check += gp.after.map {|c| c.default_check gp}
gp.if(check.join(' && ')) { gp.finish } true
end
|
#write_generation(g) ⇒ Object
108
109
110
111
|
# File 'lib/action_controller/routing.rb', line 108
def write_generation(g)
wrote_dropout = write_dropout_generation(g)
write_continue_generation(g, wrote_dropout)
end
|
#write_recognition(g) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/action_controller/routing.rb', line 149
def write_recognition(g)
test_type = nil
recognition_check(g) do |gp, test_type|
assign_result(gp) {|gpp| gpp.continue}
end
if optional? && g.after.all? {|c| c.optional?}
call = (test_type == :presence) ? [:else] : [:elsif, "! #{g.next_segment(true)}"]
g.send(*call) do |gp|
assign_default(gp)
gp.after.each {|c| c.assign_default(gp)}
gp.finish(false)
end
end
end
|