Class: Xaases::Js
- Inherits:
-
Object
- Object
- Xaases::Js
- Defined in:
- lib/xaases/js.rb,
lib/xaases/js/function.rb
Defined Under Namespace
Classes: Function
Instance Attribute Summary collapse
-
#minify ⇒ Object
readonly
Returns the value of attribute minify.
Instance Method Summary collapse
- #call(name, *params) ⇒ Object
- #const(list) ⇒ Object
- #export(name, f) ⇒ Object
- #hash(data) ⇒ Object
- #if(*conditions) ⇒ Object
-
#initialize(minify: true) ⇒ Js
constructor
A new instance of Js.
- #render ⇒ Object
- #strict! ⇒ Object
Constructor Details
#initialize(minify: true) ⇒ Js
6 7 8 9 |
# File 'lib/xaases/js.rb', line 6 def initialize(minify: true) @minify = minify @s = '' end |
Instance Attribute Details
#minify ⇒ Object (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
31 32 33 34 35 36 37 |
# 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) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
65 66 67 68 69 70 71 72 |
# 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) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# 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) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 ⇒ Object
74 75 76 |
# File 'lib/xaases/js.rb', line 74 def render @s end |
#strict! ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/xaases/js.rb', line 11 def strict! if minify @s = "'use strict';" else @s = "'use strict';\n" end end |