Class: ActionController::CodeGeneration::CodeGenerator

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

Overview

:nodoc:

Direct Known Subclasses

GenerationGenerator, RecognitionGenerator

Constant Summary collapse

BeginKeywords =
%w(if unless begin until while def).collect {|kw| kw.to_sym}
ResumeKeywords =
%w(elsif else rescue).collect {|kw| kw.to_sym}
Keywords =
BeginKeywords + ResumeKeywords
FieldsToDuplicate =
[:locals]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ CodeGenerator

Returns a new instance of CodeGenerator.



29
30
31
32
# File 'lib/action_controller/code_generation.rb', line 29

def initialize(source = nil)
  @locals = []
  @source = source || Source.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(keyword, *text) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/action_controller/code_generation.rb', line 38

def method_missing(keyword, *text)
  if Keywords.include? keyword
    if ResumeKeywords.include? keyword
      raise GenerationError, "Can only resume with #{keyword} immediately after an end" unless source.lines.last =~ /^\s*end\s*$/ 
      source.lines.pop # Remove the 'end'
    end

    line "#{keyword} #{text.join ' '}"
    begin source.indent { yield(self.dup) }
    ensure line 'end'
    end
  else
    super(keyword, *text)
  end
end

Instance Attribute Details

#localsObject

Returns the value of attribute locals.



28
29
30
# File 'lib/action_controller/code_generation.rb', line 28

def locals
  @locals
end

#sourceObject

Returns the value of attribute source.



28
29
30
# File 'lib/action_controller/code_generation.rb', line 28

def source
  @source
end

Instance Method Details

#dupObject



64
65
66
67
68
69
70
71
72
# File 'lib/action_controller/code_generation.rb', line 64

def dup
  copy = self.class.new(source)
  self.class::FieldsToDuplicate.each do |sym|
    value = self.send(sym)
    value = value.dup unless value.nil? || value.is_a?(Numeric)
    copy.send("#{sym}=", value)
  end
  return copy
end

#indent(*args, &block) ⇒ Object



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

def indent(*args, &block) source(*args, &block) end

#line(*args) ⇒ Object Also known as: <<



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

def line(*args) self.source.line(*args) end

#share_locals_with(other) ⇒ Object



59
60
61
# File 'lib/action_controller/code_generation.rb', line 59

def share_locals_with(other)
  other.locals = self.locals = (other.locals | locals) 
end

#to_sObject



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

def to_s() source.to_s end