Class: JsMethodBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, block) ⇒ JsMethodBuilder

Returns a new instance of JsMethodBuilder.



100
101
102
103
104
# File 'lib/js.rb', line 100

def initialize(klass, name, block)
  @klass, @name, @block = klass, name, block
  @builder              = POOL.get
  @injectors            = Array.new(block.arity) { |i| Injector.new("arg#{i}") }
end

Instance Attribute Details

#block ⇒ Object (readonly)

Returns the value of attribute block.



98
99
100
# File 'lib/js.rb', line 98

def block
  @block
end

#builder ⇒ Object (readonly)

Returns the value of attribute builder.



98
99
100
# File 'lib/js.rb', line 98

def builder
  @builder
end

#klass ⇒ Object (readonly)

Returns the value of attribute klass.



98
99
100
# File 'lib/js.rb', line 98

def klass
  @klass
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



98
99
100
# File 'lib/js.rb', line 98

def name
  @name
end

Instance Method Details

#build ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/js.rb', line 106

def build
  builder.raw head
  builder.dive(*@injectors, &block)
  builder.raw foot
  builder.to_html!.gsub(/\{\{([^}]*)\}\}/, '"+\1+"')
ensure
  @builder = nil
end

#foot ⇒ Object



125
126
127
# File 'lib/js.rb', line 125

def foot
  %(";\n  return _buf;\n};\n)
end

#head ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/js.rb', line 115

def head
  <<-JS.chomp
var templates = templates || {};
templates["#{klass}"] = templates["#{klass}"] || {};
templates["#{klass}"].#{name} = function (#{@injectors.map(&:__name__).join(', ')}) {
var _buf;
_buf = "
  JS
end