Class: ActionController::CodeGeneration::RecognitionGenerator

Inherits:
CodeGenerator show all
Defined in:
lib/action_controller/code_generation.rb

Overview

:nodoc:

Constant Summary collapse

Attributes =
[:after, :before, :current, :results, :constants, :depth, :move_ahead, :finish_statement]
FieldsToDuplicate =
CodeGenerator::FieldsToDuplicate + Attributes

Constants inherited from CodeGenerator

CodeGenerator::BeginKeywords, CodeGenerator::Keywords, CodeGenerator::ResumeKeywords

Instance Attribute Summary

Attributes inherited from CodeGenerator

#locals, #source

Instance Method Summary collapse

Methods inherited from CodeGenerator

#dup, #indent, #line, #method_missing, #share_locals_with, #to_s

Constructor Details

#initialize(*args) ⇒ RecognitionGenerator

Returns a new instance of RecognitionGenerator.


80
81
82
83
84
85
86
87
88
# File 'lib/action_controller/code_generation.rb', line 80

def initialize(*args)
  super(*args)
  @after, @before = [], []
  @current = nil
  @results, @constants = {}, {}
  @depth = 0
  @move_ahead = nil
  @finish_statement = Proc.new {|hash_expr| hash_expr}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActionController::CodeGeneration::CodeGenerator

Instance Method Details

#constant_result(key, object) ⇒ Object


148
149
150
# File 'lib/action_controller/code_generation.rb', line 148

def constant_result(key, object)
  constants[key] = object
end

#continueObject


128
129
130
131
132
133
# File 'lib/action_controller/code_generation.rb', line 128

def continue
  dup = self.dup
  dup.before << dup.current
  dup.current = dup.after.shift
  dup.go
end

#finish(ensure_traversal_finished = true) ⇒ Object


152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/action_controller/code_generation.rb', line 152

def finish(ensure_traversal_finished = true)
  pairs = [] 
  (results.keys + constants.keys).uniq.each do |key|
    pairs << "#{key.to_s.inspect} => #{results[key] ? results[key] : constants[key].inspect}"
  end
  hash_expr = "{#{pairs.join(', ')}}"

  statement = finish_statement.call(hash_expr)
  if ensure_traversal_finished then self.if("! #{next_segment(true)}") {|gp| gp << statement}
  else self << statement
  end
end

#goObject


135
136
137
138
139
# File 'lib/action_controller/code_generation.rb', line 135

def go
  if current then current.write_recognition(self)
  else self.finish
  end
end

#if_next_matches(string, &block) ⇒ Object


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

def if_next_matches(string, &block)
  test = Routing.test_condition(next_segment(true), string)
  self.if(test, &block)
end

#index_nameObject


123
124
125
126
# File 'lib/action_controller/code_generation.rb', line 123

def index_name
  move_ahead, @move_ahead = @move_ahead, nil
  move_ahead ? "index += #{move_ahead}" : 'index'
end

#move_forward(places = 1) {|dup| ... } ⇒ Object

Yields:


95
96
97
98
99
100
# File 'lib/action_controller/code_generation.rb', line 95

def move_forward(places = 1)
  dup = self.dup
  dup.depth += 1
  dup.move_ahead = places
  yield dup
end

#next_segment(assign_inline = false, default = nil) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/action_controller/code_generation.rb', line 102

def next_segment(assign_inline = false, default = nil)
  if locals.include?(segment_name)
    code = segment_name
  else
    code = "#{segment_name} = #{path_name}[#{index_name}]"
    if assign_inline
      code = "(#{code})"
    else
      line(code)
      code = segment_name
    end
  
    locals << segment_name
  end
  code = "(#{code} || #{default.inspect})" if default 

  return code.to_s
end

#path_nameObject


122
# File 'lib/action_controller/code_generation.rb', line 122

def path_name() :path end

#result(key, expression, delay = false) ⇒ Object


141
142
143
144
145
146
147
# File 'lib/action_controller/code_generation.rb', line 141

def result(key, expression, delay = false)
  unless delay
    line "#{key}_value = #{expression}"
    expression = "#{key}_value"
  end
  results[key] = expression
end

#segment_nameObject


121
# File 'lib/action_controller/code_generation.rb', line 121

def segment_name() "segment#{depth}".to_sym end