Class: Jabs::Precompiler

Inherits:
Fold::Precompiler
  • Object
show all
Defined in:
lib/jabs/precompiler.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrecompiler

Returns a new instance of Precompiler.



17
18
19
20
21
22
23
# File 'lib/jabs/precompiler.rb', line 17

def initialize
  super
  
  @ready = false
  @current_sexp = []
  @full_sexp = [:source_elements, @current_sexp]
end

Class Attribute Details

.spot_replacementsObject

Returns the value of attribute spot_replacements.



6
7
8
# File 'lib/jabs/precompiler.rb', line 6

def spot_replacements
  @spot_replacements
end

Instance Attribute Details

#current_sexpObject (readonly)

Returns the value of attribute current_sexp.



15
16
17
# File 'lib/jabs/precompiler.rb', line 15

def current_sexp
  @current_sexp
end

#sexpObject (readonly)

Returns the value of attribute sexp.



15
16
17
# File 'lib/jabs/precompiler.rb', line 15

def sexp
  @sexp
end

Class Method Details

.compile_arguments(expression, call, real, args) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/jabs/precompiler.rb', line 176

def self.compile_arguments expression, call, real, args
  return real if expression[Regexp.new("^\\#{call}\\(")]
  arguments = []
  if args[/^\./]
    "#{call}()#{do_spot_replace(args).gsub("$this", "")}"
  else
    args.split(/\s|,/).each do |arg|
      arg.gsub!(/:(\w+)/) {%{"#{$1}"}}
      next if arg[/\s/]
      next if arg == ""
      arguments << arg
    end
    "#{call}(#{arguments.join(', ')})"
  end
end

.do_spot_replace(expression, precompiler = nil) ⇒ Object



169
170
171
172
173
174
# File 'lib/jabs/precompiler.rb', line 169

def self.do_spot_replace expression, precompiler=nil
  spot_replacements.each do |block|
    expression = block.call(expression, precompiler)
  end
  expression
end

.spot_replace(key, &block) ⇒ Object



11
12
13
# File 'lib/jabs/precompiler.rb', line 11

def self.spot_replace key, &block
  spot_replacements << block if block_given?
end

Instance Method Details

#access(left, right) ⇒ Object



216
217
218
# File 'lib/jabs/precompiler.rb', line 216

def access left, right
  [:dot_accessor, right, left]
end

#call(*args) ⇒ Object



208
209
210
# File 'lib/jabs/precompiler.rb', line 208

def call *args
  [:function_call, args]
end

#event_bind(event, binds_to, function_sexp = nil) ⇒ Object



203
204
205
206
# File 'lib/jabs/precompiler.rb', line 203

def event_bind event, binds_to, function_sexp=nil
  binding = NeverLive.include?(event.to_s)? 'bind' : 'live'
  call(access(binds_to, [:name, binding]), [:string, event], [:function, nil,  ["event"], function_sexp])
end

#function(name = nil, args = [], function_sexp = nil) ⇒ Object



220
221
222
223
# File 'lib/jabs/precompiler.rb', line 220

def function name=nil, args=[], function_sexp=nil
  function_sexp.last << [:return, parse("$this")]
  [:return, [:function, name, args, function_sexp]]
end

#johnsonize(sexp) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/jabs/precompiler.rb', line 232

def johnsonize(sexp)
  return sexp if sexp.is_a?(Johnson::Nodes::Node)
  return sexp if sexp.class.to_s == "String"
  return [] if sexp === []
  return nil if sexp === nil
  unless sexp.first.class.to_s == "Array"
    if sexp.first.class.to_s == "Symbol"
      klass = eval("Johnson::Nodes::#{sexp.shift.to_s.camelcase}")
      klass.new 0,0, *sexp.map{|n|johnsonize(n)}
    elsif sexp.is_a? Array
      sexp.map{|n|johnsonize(n)}
    else
      sexp
    end
  else
    sexp.map{|n|johnsonize(n)}
  end
end

#jquery(*jquery_arg) ⇒ Object



225
226
227
228
229
230
# File 'lib/jabs/precompiler.rb', line 225

def jquery *jquery_arg
  [:function_call, [
    [:name, 'jQuery'],
    *jquery_arg
  ]]
end

#onready(function_sexp = nil) ⇒ Object



212
213
214
# File 'lib/jabs/precompiler.rb', line 212

def onready function_sexp=nil
  event_bind('ready', jquery([:name, "document"]), function_sexp)
end

#parse(expression) ⇒ Object



192
193
194
195
# File 'lib/jabs/precompiler.rb', line 192

def parse expression
  self.class.do_spot_replace expression, self
  Johnson::Parser.parse(expression).value.first
end

#source(block = nil) ⇒ Object



197
198
199
200
201
# File 'lib/jabs/precompiler.rb', line 197

def source block=nil
  source = [:source_elements]
  source << [block] unless(block.nil? or block.empty?)
  source
end