Module: Ruby2JS::Filter::AngularRB

Includes:
SEXP
Defined in:
lib/ruby2js/filter/angularrb.rb

Instance Method Summary collapse

Methods included from SEXP

#s

Instance Method Details

#initialize(*args) ⇒ Object



9
10
11
12
# File 'lib/ruby2js/filter/angularrb.rb', line 9

def initialize(*args)
  @ngApp = nil
  super
end

#on_block(node) ⇒ Object

input:

filter :name do |input|
  ...
end

output:

AppName.filter :name do
  return lambda {|input| return ... }
end


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby2js/filter/angularrb.rb', line 138

def on_block(node)
  return super unless @ngApp

  call = node.children.first
  return super unless call.children[0..1] == [nil, :filter]

  # insert return
  children = process_all(node.children[1..-1])
  block = [children.pop || s(:nil)]
  while block.length == 1 and block.first.type == :begin
    block = block.first.children.dup
  end
  block.push s(:return, block.pop) unless block.last.type == :return
  children.push (block.length == 1 ? block.first : s(:begin, *block))

  # construct a function returning a function
  inner = s(:block, s(:send, nil, :lambda), *children)
  outer = s(:send, s(:lvar, @ngApp), :filter, *call.children[2..-1])

  node.updated nil, [outer, s(:args), s(:return, inner)]
end

#on_class(node) ⇒ Object

input:

class Name < Angular::Controller
  use :$service
  ...
end

output:

AppName.controller :Name do |$service|
  ...
end


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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby2js/filter/angularrb.rb', line 80

def on_class(node)
  return super unless @ngApp
  return super unless node.children.length == 3

  # (const nil :Name)
  name = node.children.first
  return super unless name.type == :const and name.children.first == nil

  # (const (const nil :Angular) :Controller)
  parent = node.children[1]
  return super unless parent and parent.children.length == 2
  return super unless parent.children[0]
  return super unless parent.children[0].type == :const
  return super unless parent.children[0].children == [nil, :Angular]
  return super unless [:Controller].include? parent.children[1]

  # find the block
  block = process_all(node.children[2..-1])
  while block.length == 1 and block.first.type == :begin
    block = block.first.children.dup
  end

  # find use class method calls
  uses = block.find_all do |node|
    node.type == :send and node.children[0..1] == [nil, :use]
  end

  # convert use calls into args
  args = []
  uses.each do |use|
    pending = []
    use.children[2..-1].each do |node|
      break unless [:str, :sym].include? node.type
      pending << s(:arg, *node.children)
    end
    args += pending
    block.delete use
  end

  # build Appname.controller call statement
  call = s(:send,
           s(:const, nil, @ngApp),
           :controller,
           s(:sym, name.children.last))

  # replace class with a block
  node.updated :block, [call, s(:args, *args), s(:begin, *block)]
end

#on_module(node) ⇒ Object

input:

module Angular::AppName
  use :Dependency
  ...
end

output:

AppName = angular.module("AppName", ["Dependency"])
...


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby2js/filter/angularrb.rb', line 24

def on_module(node)
  module_name = node.children[0]
  parent_name = module_name.children[0]

  return super unless parent_name and parent_name.type == :const
  return super unless parent_name.children == [nil, :Angular]

  @ngApp = module_name.children[1]

  # find the block
  block = process_all(node.children[1..-1])
  while block.length == 1 and block.first and block.first.type == :begin
    block = block.first.children.dup
  end

  # find use class method calls
  uses = block.find_all do |node|
    node and node.type == :send and node.children[0..1] == [nil, :use]
  end

  # convert use calls into dependencies
  depends = []
  uses.each do |use|
    pending = []
    use.children[2..-1].each do |node|
      break unless [:str, :sym].include? node.type
      pending << node
    end
    depends += pending
    block.delete use
  end

  # build constant assignment statement
  casgn = s(:casgn, nil, @ngApp, s(:send, 
            s(:lvar, :angular), 
            :module,
            s(:str, @ngApp.to_s), 
            s(:array, *depends)))

  @ngApp = nil

  # replace module with a constant assign followed by the module contents
  node.updated :begin, [casgn, *block]
end