Module: Ruby2JS

Defined in:
lib/ruby2js/filter/jquery.rb,
lib/ruby2js.rb,
lib/ruby2js/rails.rb,
lib/ruby2js/version.rb,
lib/ruby2js/converter.rb,
lib/ruby2js/converter/if.rb,
lib/ruby2js/converter/arg.rb,
lib/ruby2js/converter/def.rb,
lib/ruby2js/converter/for.rb,
lib/ruby2js/converter/nil.rb,
lib/ruby2js/converter/sym.rb,
lib/ruby2js/converter/var.rb,
lib/ruby2js/filter/return.rb,
lib/ruby2js/filter/strict.rb,
lib/ruby2js/converter/args.rb,
lib/ruby2js/converter/case.rb,
lib/ruby2js/converter/cvar.rb,
lib/ruby2js/converter/defs.rb,
lib/ruby2js/converter/dstr.rb,
lib/ruby2js/converter/hash.rb,
lib/ruby2js/converter/ivar.rb,
lib/ruby2js/converter/next.rb,
lib/ruby2js/converter/self.rb,
lib/ruby2js/converter/send.rb,
lib/ruby2js/converter/xstr.rb,
lib/ruby2js/converter/array.rb,
lib/ruby2js/converter/begin.rb,
lib/ruby2js/converter/block.rb,
lib/ruby2js/converter/break.rb,
lib/ruby2js/converter/casgn.rb,
lib/ruby2js/converter/class.rb,
lib/ruby2js/converter/const.rb,
lib/ruby2js/converter/masgn.rb,
lib/ruby2js/converter/super.rb,
lib/ruby2js/converter/undef.rb,
lib/ruby2js/converter/until.rb,
lib/ruby2js/converter/vasgn.rb,
lib/ruby2js/converter/while.rb,
lib/ruby2js/converter/cvasgn.rb,
lib/ruby2js/converter/ivasgn.rb,
lib/ruby2js/converter/module.rb,
lib/ruby2js/converter/opasgn.rb,
lib/ruby2js/converter/regexp.rb,
lib/ruby2js/converter/return.rb,
lib/ruby2js/filter/angularrb.rb,
lib/ruby2js/filter/functions.rb,
lib/ruby2js/converter/boolean.rb,
lib/ruby2js/converter/defined.rb,
lib/ruby2js/converter/kwbegin.rb,
lib/ruby2js/converter/literal.rb,
lib/ruby2js/converter/logical.rb,
lib/ruby2js/filter/underscore.rb,
lib/ruby2js/converter/blockpass.rb,
lib/ruby2js/converter/prototype.rb,
lib/ruby2js/converter/untilpost.rb,
lib/ruby2js/converter/whilepost.rb,
lib/ruby2js/filter/angular-route.rb,
lib/ruby2js/filter/angular-resource.rb

Overview

Jquery functions are either invoked using jQuery() or, more commonly, $(). The former presents no problem, the latter is not legal Ruby.

Accordingly, the first accomodation this filter provides is to map $$ to $. This works find for $$.ajax and the like, but less so for direct calls as $$(this) is also a syntax error. $$.(this) and $$[this] will work but are a bit clumsy.

So as a second accomodation, the rarely used binary one's complement unary operator (namely, ~) is usurped, and the AST is rewritten to provide the effect of this operator being of a higher precedence than method calls. Passing multiple parameters can be accomplished by using array index syntax (e.g., ~['a', self])

As a part of this rewriting, calls to getters and setters are rewritten to match jQuery's convention for getters and setters: http://learn.jquery.com/using-jquery-core/working-with-selections/

Selected DOM properties (namely checked, disabled, readOnly, and required) can also use getter and setter syntax. Additionally, readOnly may be spelled 'readonly'.

Of course, using jQuery's style of getter and setter calls is supported, and indeed is convenient when using method chaining.

Additionally, the tilde AST rewriting can be avoided by using consecutive tildes ( is a common Ruby idiom for Math.floor, ~ will return the binary one's complement.); and the getter and setter AST rewriting can be avoided by the use of parenthesis, e.g. (~this).text.

Finally, the fourth parameter of $.post defaults to :json, allowing Ruby block syntax to be used for the success function.

Some examples of before/after conversions:

~this.val
$(this).val()

~"button.continue".html = "Next Step..."
$("button.continue").html("Next Step...")

~"button".readonly = false
$("button").prop("readOnly", false)

$$.ajax( url: "/api/getWeather", data: 97201, success: proc do |data| `"#weather-temp".html = "#data degrees" end )

$.ajax({ url: "/api/getWeather", data: 97201, success: function(data) { $("#weather-temp").html("" + data + " degrees"); } })

Defined Under Namespace

Modules: Filter, Rails, VERSION Classes: Converter

Class Method Summary collapse

Class Method Details

.convert(source, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ruby2js.rb', line 47

def self.convert(source, options={})

  if Proc === source
    file,line = source.source_location
    source = File.read(file.dup.untaint).untaint
    ast = find_block( parse(source), line )
  elsif Parser::AST::Node === source
    ast = source
    source = ast.loc.expression.source_buffer.source
  else
    ast = parse( source )
  end

  filters = options[:filters] || Filter::DEFAULTS

  unless filters.empty?
    filter = Filter::Processor
    filters.reverse.each do |mod|
      filter = Class.new(filter) {include mod} 
    end
    ast = filter.new.process(ast)
  end

  ruby2js = Ruby2JS::Converter.new( ast )

  ruby2js.binding = options[:binding]
  ruby2js.ivars = options[:ivars]
  if ruby2js.binding and not ruby2js.ivars
    ruby2js.ivars = ruby2js.binding.eval \
      'Hash[instance_variables.map {|var| [var, instance_variable_get(var)]}]'
  end

  ruby2js.width = options[:width] if options[:width]

  if source.include? "\n"
    ruby2js.enable_vertical_whitespace 
    lines = ruby2js.to_js.split("\n")
    pre = ''
    pending = false
    blank = true
    lines.each do |line|
      next if line.empty?

      if ')}]'.include? line[0]
        pre.sub!(/^  /,'')
        line.sub!(/([,;])$/,"\\1\n")
        pending = true
      else
        pending = false
      end

      line.sub! /^/, pre
      if '({['.include? line[-1]
        pre += '  ' 
        line.sub!(/^/,"\n") unless blank or pending
        pending = true
      end

      blank = pending
    end

    lines.join("\n").gsub(/^  ( *(case.*|default):$)/, '\1')
  else
    ruby2js.to_js
  end
end

.find_block(ast, line) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ruby2js.rb', line 121

def self.find_block(ast, line)
  if ast.type == :block and ast.loc.expression.line == line
    return ast.children.last
  end

  ast.children.each do |child|
    if Parser::AST::Node === child
      block = find_block child, line
      return block if block
    end
  end

  nil
end

.parse(source) ⇒ Object



114
115
116
117
118
119
# File 'lib/ruby2js.rb', line 114

def self.parse(source)
  # workaround for https://github.com/whitequark/parser/issues/112
  buffer = Parser::Source::Buffer.new('__SOURCE__')
  buffer.raw_source = source.encode('utf-8')
  Parser::CurrentRuby.new.parse(buffer)
end