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

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#builderObject (readonly)

Returns the value of attribute builder.



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

def builder
  @builder
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#buildObject



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

#footObject



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

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

#headObject



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

def head
  "var templates = templates || {};\ntemplates[\"\#{klass}\"] = templates[\"\#{klass}\"] || {};\ntemplates[\"\#{klass}\"].\#{name} = function (\#{@injectors.map(&:__name__).join(', ')}) {\nvar _buf;\n_buf = \"\n  JS\nend\n".chomp