class Xaases::Js

Attributes

minify[R]

Public Class Methods

new(minify: true) click to toggle source
# File lib/xaases/js.rb, line 6
def initialize(minify: true)
  @minify = minify
  @s = ''
end

Public Instance Methods

call(name, *params) click to toggle source
# File lib/xaases/js.rb, line 31
def call(name, *params)
  if minify
    @s += "#{name}(#{params.join(',')});"
  else
    @s += "#{name}(#{params.join(', ')});"
  end
end
const(list) click to toggle source
# File lib/xaases/js.rb, line 19
def const(list)
  if minify
    @s += 'const ' + list.map do |key, value|
      key.to_s + '=' + value.to_s
    end.join(',') + ';'
  else
    @s += 'const ' + list.map do |key, value|
      key.to_s + ' = ' + value.to_s
    end.join(",\n      ") + ";\n"
  end
end
export(name, f) click to toggle source
# File lib/xaases/js.rb, line 65
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) click to toggle source
# File lib/xaases/js.rb, line 39
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) click to toggle source
# File lib/xaases/js.rb, line 51
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
render() click to toggle source
# File lib/xaases/js.rb, line 74
def render
  @s
end
strict!() click to toggle source
# File lib/xaases/js.rb, line 11
def strict!
  if minify
    @s = "'use strict';"
  else
    @s = "'use strict';\n"
  end
end