Class: ActionController::Routing::DynamicComponent

Inherits:
Component
  • Object
show all
Defined in:
lib/action_controller/routing.rb

Overview

:nodoc:

Direct Known Subclasses

ControllerComponent, PathComponent

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.



101
102
103
104
105
106
# File 'lib/action_controller/routing.rb', line 101

def initialize(key, options = {})
  @key = key.to_sym
  @optional = false
  default, @condition = options[:default], options[:condition]
  self.default = default if options.key?(:default)
end

Instance Attribute Details

#conditionObject

Returns the value of attribute condition.



91
92
93
# File 'lib/action_controller/routing.rb', line 91

def condition
  @condition
end

#defaultObject

Returns the value of attribute default.



90
91
92
# File 'lib/action_controller/routing.rb', line 90

def default
  @default
end

#keyObject (readonly)

Returns the value of attribute key.



90
91
92
# File 'lib/action_controller/routing.rb', line 90

def key
  @key
end

Instance Method Details

#add_segments_to(g) ⇒ Object



145
146
147
# File 'lib/action_controller/routing.rb', line 145

def add_segments_to(g)
  g.add_segment(%(\#{CGI.escape(#{g.hash_value(key, true, default)})})) {|gp| yield gp}
end

#assign_default(g) ⇒ Object



180
181
182
# File 'lib/action_controller/routing.rb', line 180

def assign_default(g)
  g.constant_result key, default unless default.nil?
end

#assign_result(g, with_default = false) ⇒ Object



175
176
177
178
# File 'lib/action_controller/routing.rb', line 175

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



108
109
110
111
112
113
114
115
# File 'lib/action_controller/routing.rb', line 108

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

Returns:

  • (Boolean)


93
# File 'lib/action_controller/routing.rb', line 93

def dynamic?()  true      end

#optional?Boolean

Returns:

  • (Boolean)


94
# File 'lib/action_controller/routing.rb', line 94

def optional?() @optional end

#recognition_check(g) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/action_controller/routing.rb', line 149

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



135
136
137
138
139
140
141
142
143
# File 'lib/action_controller/routing.rb', line 135

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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/action_controller/routing.rb', line 122

def write_dropout_generation(g)
  return false unless optional? && g.after.all? {|c| c.optional?}
    
  check = [default_check(g)]
  gp = g.dup # Use another generator to write the conditions after the first &&
  # We do this to ensure that the generator will not assume x_value is set. It will
  # not be set if it follows a false condition -- for example, false && (x = 2)
  
  check += gp.after.map {|c| c.default_check gp}
  gp.if(check.join(' && ')) { gp.finish } # If this condition is met, we stop here
  true 
end

#write_generation(g) ⇒ Object



117
118
119
120
# File 'lib/action_controller/routing.rb', line 117

def write_generation(g)
  wrote_dropout = write_dropout_generation(g)
  write_continue_generation(g, wrote_dropout)
end

#write_recognition(g) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/action_controller/routing.rb', line 158

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