Class: GoogleVisualr::Formatter

Inherits:
Object
  • Object
show all
Includes:
ParamHelpers
Defined in:
lib/google_visualr/formatters.rb

Overview

Instance Method Summary collapse

Methods included from ParamHelpers

#js_parameters, #stringify_keys!, #typecast

Constructor Details

#initialize(options = {}) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
# File 'lib/google_visualr/formatters.rb', line 7

def initialize(options={})
  @options = options
end

Instance Method Details

#columns(*columns) ⇒ Object



12
13
14
# File 'lib/google_visualr/formatters.rb', line 12

def columns(*columns)
  @columns = columns.flatten
end

#options(*options) ⇒ Object



16
17
18
# File 'lib/google_visualr/formatters.rb', line 16

def options(*options)
  @options = stringify_keys!(options.pop)
end

#to_js {|js| ... } ⇒ Object

Yields:

  • (js)


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/google_visualr/formatters.rb', line 20

def to_js(&block)
  js   = "\nvar formatter = new google.visualization.#{self.class.to_s.split('::').last}("
  js  <<  js_parameters(@options)
  js  << ");"

  yield js if block_given?

  @columns.each do |column|
   js << "\nformatter.format(data_table, #{column});"
  end

  js
end