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.



136
137
138
139
140
# File 'lib/right_rails/java_script_generator.rb', line 136

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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/right_rails/java_script_generator.rb', line 155

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



143
144
145
# File 'lib/right_rails/java_script_generator.rb', line 143

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

#[]=(name, value) ⇒ Object

attribute assignment hook



148
149
150
# File 'lib/right_rails/java_script_generator.rb', line 148

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

#to_sObject

exports the whole thing into a javascript string



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/right_rails/java_script_generator.rb', line 178

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