Class: Xaases::Js

Inherits:
Object
  • Object
show all
Defined in:
lib/xaases/js.rb,
lib/xaases/js/function.rb

Defined Under Namespace

Classes: Function

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minify: true) ⇒ Js

Returns a new instance of Js.



6
7
8
9
# File 'lib/xaases/js.rb', line 6

def initialize(minify: true)
  @minify = minify
  @s = ''
end

Instance Attribute Details

#minifyObject (readonly)

Returns the value of attribute minify.



4
5
6
# File 'lib/xaases/js.rb', line 4

def minify
  @minify
end

Instance Method Details

#call(name, *params) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/xaases/js.rb', line 21

def call(name, *params)
  if minify
    @s += "#{name}(#{params.join(',')});"
  else
    @s += "#{name}(#{params.join(', ')});"
  end
end

#const(list) ⇒ Object



15
16
17
18
19
# File 'lib/xaases/js.rb', line 15

def const(list)
  @s += 'const ' + list.map do |key, value|
    key.to_s + '=' + value.to_s
  end.join(',') + ';'
end

#export(name, f) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/xaases/js.rb', line 55

def export(name, f)
  if minify
    @s += "module.exports.#{name}=(#{f.params.join(',')})=>{#{f.render}};"
  else
    content = f.render.split("\n").join("\n  ")
    @s += "module.exports.#{name} = (#{f.params.join(', ')}) => {\n  #{content}\n};\n"
  end
end

#hash(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xaases/js.rb', line 29

def hash(data)
  if minify
    @s += '{' + data.map do |key, value|
      key.to_s + ':' + value.to_s
    end.join(',') + '}'
  else
    @s += '{ ' + data.map do |key, value|
      key.to_s + ': ' + value.to_s
    end.join(', ') + ' }'
  end
end

#if(*conditions) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xaases/js.rb', line 41

def if(*conditions)
  i = -1
  conditions.map do |condition|
    i += 1
    if i == 0
      'if{' + condition + '}'
    elsif i == conditions.length - 1
      'else{' + condition + '}'
    else
      'elsif{' + condition + '}'
    end
  end.join
end

#renderObject



64
65
66
# File 'lib/xaases/js.rb', line 64

def render
  @s
end

#strict!Object



11
12
13
# File 'lib/xaases/js.rb', line 11

def strict!
  @s = "'use strict';"
end