Class: Gvis::DataCell

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, type = '') ⇒ DataCell

Returns a new instance of DataCell.



12
13
14
15
# File 'lib/gvis/data_cell.rb', line 12

def initialize(value, type = '')
  @value = value
  @type = type
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/gvis/data_cell.rb', line 10

def type
  @type
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/gvis/data_cell.rb', line 9

def value
  @value
end

Instance Method Details

#to_jsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gvis/data_cell.rb', line 17

def to_js
  # Format/escape individual values for javascript, checking column types, and the ruby value as a failsafe
  if @type == "datetime" || @value.is_a?(DateTime)
    # Format a DateTime as a javascript date object down to seconds
    @value.is_a?(DateTime) ? "new Date (#{@value.year},#{@value.month - 1},#{@value.day},#{@value.hour},#{@value.min},#{@value.sec})" : @value.to_json
  elsif @type == "date" || @value.is_a?(Date)
    # Format a Date object as a javascript date
    @value.is_a?(Date) ? "new Date (#{@value.year},#{@value.month - 1},#{@value.day})" : @value.to_json
  elsif @type == "timeofday" || @value.is_a?(Time)
    # Format a Time as a javascript date object down to milliseconds
    @value.is_a?(Time) ? "[#{@value.hour},#{@value.min},#{@value.sec},#{@value.usec}]" : @value.to_json
  else
    # Non date/time values can be JS escaped/formatted safely with # to_json
    @value.to_json
  end
end

#to_sObject



34
35
36
# File 'lib/gvis/data_cell.rb', line 34

def to_s
  @value.to_s
end