google_visualr 2.0.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/google_visualr/data_table.rb +12 -6
- data/lib/google_visualr/version.rb +1 -1
- data/spec/google_visualr/data_table_spec.rb +34 -6
- metadata +2 -2
data/Gemfile.lock
CHANGED
@@ -43,6 +43,7 @@ module GoogleVisualr
|
|
43
43
|
#
|
44
44
|
# * v [Optional] The cell value. The data type should match the column data type.
|
45
45
|
# * f [Optional] A string version of the v value, formatted strictly for display only. If omitted, a string version of v will be used.
|
46
|
+
# * p [Optional] An object that is a map of custom values applied to the cell. Example: :p => { :style => 'border: 1px solid green;' }.
|
46
47
|
#
|
47
48
|
# Cells in the row array should be in the same order as their column descriptions in cols.
|
48
49
|
#
|
@@ -74,7 +75,7 @@ module GoogleVisualr
|
|
74
75
|
# - 'boolean' : Boolean value ('true' or 'false'). Example values: v: true
|
75
76
|
# * label [Optional] A string value that some visualizations display for this column. Example: label:'Height'
|
76
77
|
# * id [Optional] A unique (basic alphanumeric) string ID of the column. Be careful not to choose a JavaScript keyword. Example: id:'col_1'
|
77
|
-
def new_column(type, label=
|
78
|
+
def new_column(type, label=nil, id =nil)
|
78
79
|
@cols << { :type => type, :label => label, :id => id }
|
79
80
|
end
|
80
81
|
|
@@ -199,12 +200,16 @@ module GoogleVisualr
|
|
199
200
|
js = "var data_table = new google.visualization.DataTable();"
|
200
201
|
|
201
202
|
@cols.each do |column|
|
202
|
-
js << "data_table.addColumn('
|
203
|
+
js << "data_table.addColumn('"
|
204
|
+
js << "#{column[:type]}'"
|
205
|
+
js << ", '#{column[:label]}'" unless column[:label].nil?
|
206
|
+
js << ", '#{column[:id]}'" unless column[:id].nil?
|
207
|
+
js << ");"
|
203
208
|
end
|
204
209
|
|
205
210
|
@rows.each do |row|
|
206
211
|
js << "data_table.addRow("
|
207
|
-
js << "[
|
212
|
+
js << "[#{row.collect { |cell| cell.to_js }.join(", ")}]" unless row.empty?
|
208
213
|
js << ");"
|
209
214
|
end
|
210
215
|
|
@@ -240,7 +245,6 @@ module GoogleVisualr
|
|
240
245
|
end
|
241
246
|
end
|
242
247
|
|
243
|
-
|
244
248
|
class Cell
|
245
249
|
include GoogleVisualr::ParamHelpers
|
246
250
|
|
@@ -261,10 +265,12 @@ module GoogleVisualr
|
|
261
265
|
end
|
262
266
|
|
263
267
|
def to_js
|
268
|
+
return "null" if @v.nil? && @f.nil? && @p.nil?
|
269
|
+
|
264
270
|
js = "{"
|
265
271
|
js << "v: #{typecast(@v)}"
|
266
|
-
js << ", f: '#{@f}'"
|
267
|
-
js << ", p: #{@p}"
|
272
|
+
js << ", f: '#{@f}'" unless @f.nil?
|
273
|
+
js << ", p: #{typecast(@p)}" unless @p.nil?
|
268
274
|
js << "}"
|
269
275
|
|
270
276
|
js
|
@@ -48,7 +48,7 @@ describe GoogleVisualr::DataTable do
|
|
48
48
|
it "initializes a new column with only type param" do
|
49
49
|
dt = GoogleVisualr::DataTable.new
|
50
50
|
dt.new_column('string')
|
51
|
-
dt.cols.first.should == {:id =>
|
51
|
+
dt.cols.first.should == {:id => nil, :label => nil, :type => 'string'}
|
52
52
|
end
|
53
53
|
|
54
54
|
it "initializes a new column with all params" do
|
@@ -208,6 +208,24 @@ describe GoogleVisualr::DataTable do
|
|
208
208
|
end
|
209
209
|
|
210
210
|
describe "#to_js" do
|
211
|
+
context "cols" do
|
212
|
+
it "includes :id and :label when these are specified" do
|
213
|
+
data_table = GoogleVisualr::DataTable.new()
|
214
|
+
data_table.new_column("Total", "Total", "1")
|
215
|
+
data_table.add_row([1])
|
216
|
+
|
217
|
+
data_table.to_js.should == "var data_table = new google.visualization.DataTable();data_table.addColumn('Total', 'Total', '1');data_table.addRow([{v: 1}]);"
|
218
|
+
end
|
219
|
+
|
220
|
+
it "excludes :id and :label when these are not specified" do
|
221
|
+
data_table = GoogleVisualr::DataTable.new()
|
222
|
+
data_table.new_column("Total")
|
223
|
+
data_table.add_row([1])
|
224
|
+
|
225
|
+
data_table.to_js.should == "var data_table = new google.visualization.DataTable();data_table.addColumn('Total');data_table.addRow([{v: 1}]);"
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
211
229
|
it "converts object to js string" do
|
212
230
|
dt = valid_object
|
213
231
|
js = dt.to_js
|
@@ -221,14 +239,14 @@ describe GoogleVisualr::DataTable do
|
|
221
239
|
describe "#new" do
|
222
240
|
it "initializes with a value" do
|
223
241
|
cell = GoogleVisualr::DataTable::Cell.new(1)
|
224
|
-
cell.v.should == 1
|
242
|
+
cell.v.should == 1
|
225
243
|
end
|
226
244
|
|
227
245
|
it "initializes with a hash" do
|
228
|
-
cell = GoogleVisualr::DataTable::Cell.new({:v => 1, :f => "1.0", :p =>
|
246
|
+
cell = GoogleVisualr::DataTable::Cell.new( { :v => 1, :f => "1.0", :p => {:style => 'border: 1px solid green;'} } )
|
229
247
|
cell.v.should == 1
|
230
248
|
cell.f.should == "1.0"
|
231
|
-
cell.p.should ==
|
249
|
+
cell.p.should == {:style => 'border: 1px solid green;'}
|
232
250
|
end
|
233
251
|
end
|
234
252
|
|
@@ -238,13 +256,23 @@ describe GoogleVisualr::DataTable do
|
|
238
256
|
cell = GoogleVisualr::DataTable::Cell.new(1)
|
239
257
|
cell.to_js.should == "{v: 1}"
|
240
258
|
end
|
259
|
+
|
260
|
+
it "returns 'null' when v is nil" do
|
261
|
+
cell = GoogleVisualr::DataTable::Cell.new(nil)
|
262
|
+
cell.to_js.should == "null"
|
263
|
+
end
|
241
264
|
end
|
242
265
|
|
243
266
|
context "initialized with a hash" do
|
244
|
-
it "returns a json string" do
|
245
|
-
cell = GoogleVisualr::DataTable::Cell.new({:v => 1, :f => "1.0", :p =>
|
267
|
+
it "returns a json string when v is present" do
|
268
|
+
cell = GoogleVisualr::DataTable::Cell.new( { :v => 1, :f => "1.0", :p => {:style => 'border: 1px solid green;'} } )
|
246
269
|
cell.to_js.should == "{v: 1, f: '1.0', p: {style: 'border: 1px solid green;'}}"
|
247
270
|
end
|
271
|
+
|
272
|
+
it "returns a json string when v is nil" do
|
273
|
+
cell = GoogleVisualr::DataTable::Cell.new( { :v => nil, :f => "-", :p => {:style => 'border: 1px solid red;'} } )
|
274
|
+
cell.to_js.should == "{v: null, f: '-', p: {style: 'border: 1px solid red;'}}"
|
275
|
+
end
|
248
276
|
end
|
249
277
|
end
|
250
278
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: google_visualr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 2.0.
|
5
|
+
version: 2.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Winston Teo
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
13
|
+
date: 2011-07-03 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|