Class: RightRails::JavaScriptGenerator::MethodCall

Inherits:
Object
  • Object
show all
Defined in:
lib/right_rails/java_script_generator.rb

Overview

Keeps the javascript method calls sequence and then represents iteslf like a string of javascript

Constant Summary collapse

OPERATIONS =
%w{+ - * / % <<}

Instance Method Summary collapse

Constructor Details

#initialize(this, util, parent) ⇒ MethodCall

Returns a new instance of MethodCall.



128
129
130
131
132
# File 'lib/right_rails/java_script_generator.rb', line 128

def initialize(this, util, parent)
  @this   = this
  @util   = util
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

catches all the method calls



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/right_rails/java_script_generator.rb', line 147

def method_missing(name, *args, &block)
  name = name.to_s
  args << block if block_given?
  
  
  cmd = if name[name.size-1, name.size] == '='
    # assignments
    ".#{name[0,name.size-1]}=#{@util.to_js_type(args.first)}"
    
  # operation calls
  elsif OPERATIONS.include?(name)
    name = "+=" if name == '<<'
    "#{name}#{@util.to_js_type(args.first)}"
    
  # usual method calls
  else
    ".#{name}(#{@util.to_js_args(args)})"
  end
  
  @child = @util.make_call(cmd, self)
end

Instance Method Details

#[](name) ⇒ Object

catches the properties request



135
136
137
# File 'lib/right_rails/java_script_generator.rb', line 135

def [](name)
  @child = @util.make_call(".#{name}", self)
end

#[]=(name, value) ⇒ Object

attribute assignment hook



140
141
142
# File 'lib/right_rails/java_script_generator.rb', line 140

def []=(name, value)
  send "#{name}=", value
end

#to_sObject

exports the whole thing into a javascript string



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/right_rails/java_script_generator.rb', line 170

def to_s
  nodes = []
  node = self
  
  while node
    nodes << node
    node = node.instance_variable_get(@parent.nil? ? "@child" : "@parent")
  end
  
  # reversing the calls list if building from the right end
  nodes.reverse! unless @parent.nil?
  
  nodes.collect{|n| n.instance_variable_get("@this").to_s }.join('')
end