Class: RightRails::JavaScriptGenerator::Util

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

Overview

We use this class to cleanup the main namespace of the JavaScriptGenerator instances So that the mesod_missing didn’t interferate with the util methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, thread = nil) ⇒ Util

Returns a new instance of Util.



201
202
203
204
# File 'lib/right_rails/java_script_generator.rb', line 201

def initialize(template, thread=nil)
  @template = template
  @thread   = thread || []
end

Instance Attribute Details

#templateObject (readonly)

Returns the value of attribute template.



199
200
201
# File 'lib/right_rails/java_script_generator.rb', line 199

def template
  @template
end

Instance Method Details

#build_scriptObject

builds the end script



242
243
244
245
246
247
248
# File 'lib/right_rails/java_script_generator.rb', line 242

def build_script
  list = @thread.collect do |line|
    line.is_a?(String) ? line : (line.to_s + ';')
  end

  list.join('')
end

#dom_id(record) ⇒ Object

returns a conventional dom id for the record



207
208
209
210
211
212
213
# File 'lib/right_rails/java_script_generator.rb', line 207

def dom_id(record)
  if [String, Symbol].include?(record.class)
    "#{record}"
  else
    @template.dom_id(record)
  end
end

#form_id_for(record) ⇒ Object

generates the form-id for the given record



216
217
218
# File 'lib/right_rails/java_script_generator.rb', line 216

def form_id_for(record)
  record.new_record? ? "new_#{record.class.table_name.singularize}" : "edit_#{dom_id(record)}"
end

#make_call(string, parent = nil) ⇒ Object

builds a new method call object



226
227
228
# File 'lib/right_rails/java_script_generator.rb', line 226

def make_call(string, parent=nil)
  MethodCall.new(string, self, parent)
end

#proc_to_function {|args| ... } ⇒ Object

converts a proc into a javascript function

Yields:

  • (args)


305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/right_rails/java_script_generator.rb', line 305

def proc_to_function(&block)
  thread = []
  args   = []
  names  = []
  name   = 'a'
  page   = RightRails::JavaScriptGenerator.new(@template, thread)

  block.arity.times do |i|
    args  << page.get(name)
    names << name
    name = name.succ
  end

  # swapping the current thread with the block's one
  old_thread = @thread
  @thread = thread

  yield(*args)

  # swapping the current therad back
  @thread = old_thread

  "function(#{names.join(',')}){#{page.to_s}}"
end

#record(command) ⇒ Object

Records a new call



231
232
233
234
# File 'lib/right_rails/java_script_generator.rb', line 231

def record(command)
  @thread << (line = make_call(command))
  line
end

#render(what) ⇒ Object

retnders the thing



221
222
223
# File 'lib/right_rails/java_script_generator.rb', line 221

def render(what)
  @template.render(what)
end

#to_js_args(args) ⇒ Object

converts the list of values into a javascript function arguments list



251
252
253
254
255
256
257
# File 'lib/right_rails/java_script_generator.rb', line 251

def to_js_args(args)
  list = args.collect do |value|
    to_js_type(value)
  end

  list.join(',')
end

#to_js_type(value) ⇒ Object

converts any ruby type into an javascript type



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/right_rails/java_script_generator.rb', line 260

def to_js_type(value)
  case value.class.name.to_sym
    when :Float, :Fixnum, :TrueClass, :FalseClass, :Symbol then value.to_s
    when :NilClass then 'null'
    when :Array    then "[#{to_js_args(value)}]"
    when :Proc     then proc_to_function(&value)
    else

      # the other method-calls processing
      if value.is_a?(MethodCall)
        # removing the call out of the calls thread
        top    = value
        parent = value
        while parent
          top    = parent
          parent = parent.instance_variable_get('@parent')
        end
        @thread.reject!{ |item| item == top }

        value.to_s

      # converting all sorts of strings
      elsif value.is_a?(String)
        "\"#{@template.escape_javascript(value)}\""

      # simple hashes processing
      elsif value.is_a?(Hash)
        pairs = []
        value.each do |key, value|
          pairs << "#{to_js_type(key)}:#{to_js_type(value)}"
        end
        "{#{pairs.sort.join(',')}}"

      # JSON exportable values processing
      elsif value.respond_to?(:to_json)
        to_js_type(value.to_json)

      # throwing an ansupported class name
      else
        throw "RightRails::JavaScriptGenerator doesn't support instances of #{value.class.name} yet"
      end
  end
end

#write(script) ⇒ Object

writes a pline script code into the thread



237
238
239
# File 'lib/right_rails/java_script_generator.rb', line 237

def write(script)
  @thread << script
end