/
view.rb
160 lines (147 loc) · 5.22 KB
/
view.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require 'daru/view/version'
require 'daru/view/plot'
require 'daru/view/plot_list'
require 'daru/view/adapters/highcharts/display'
require 'daru/view/adapters/nyaplot/display'
require 'daru/view/adapters/googlecharts/display'
require 'daru/view/adapters/googlecharts/google_visualr'
require 'daru/view/table'
# needed in load_lib_in_iruby method
require 'daru/view/adapters/highcharts/iruby_notebook'
require 'daru/view/adapters/nyaplot/iruby_notebook'
require 'daru/view/adapters/googlecharts/iruby_notebook'
# Rails Helper
require 'daru/view/app/rails/railtie.rb' if defined?(Rails)
module Daru
module View
class Engine < ::Rails::Engine; end if defined?(Rails)
# default Nyaplot library is used.
@plotting_library = :nyaplot
Daru::View::Plot.adapter = @plotting_library
class << self
attr_reader :plotting_library, :table_library
# New plotting library is set. Same time Daru::View::Plot.adapter is set.
def plotting_library=(lib)
case lib
when :nyaplot, :highcharts, :googlecharts
# plot charts
@plotting_library = lib
Daru::View::Plot.adapter = lib
if lib == :googlecharts
# plot table drawing
Daru::View::Table.adapter = lib
end
else
raise ArgumentError, "Unsupported library #{lib}"
end
# When code is running in console/terminal then IRuby NameError.
# Since IRuby methods can't work in console.
begin
load_lib_in_iruby lib.to_s if defined? IRuby
rescue NameError
return
end
end
# New table library is set. Same time Daru::View::Table.adapter is set.
def table_library=(lib)
case lib
when :googlecharts
# plot chart and table drawing
@plotting_library = @table_library = lib
Daru::View::Plot.adapter = Daru::View::Table.adapter = lib
when :datatables
# only for table drawing
@table_library = Daru::View::Table.adapter = lib
else
raise ArgumentError, "Unsupported library #{lib}"
end
# When code is running in console/terminal then IRuby NameError.
# Since IRuby methods can't work in console.
begin
load_lib_in_iruby lib.to_s if defined? IRuby
rescue NameError
return
end
end
# dependent script for the library. It must be added in the head tag
# of the web application.
#
# @param lib [String, Symbol] library whose dependencies are to be loaded
# @return [void, String] dependent script for the library
# @example
# dep_js = Daru::View.dependent_script(:highcharts)
# use in Rails app : <%=raw dep_js %>
# @example
# dep_js = Daru::View.dependent_script('highcharts')
# use in Rails app : <%=raw dep_js %>
#
# @example
# To load the dependent JS file for Nyaplot library
# plotting system (Nyaplot.js, d3.js):
#
# Daru::View.dependent_script(:nyaplot)
# @example
# Daru::View.dependent_script('nyaplot')
def dependent_script(lib=:nyaplot)
load_lib_in_iruby(lib.to_s) if defined? IRuby
rescue NameError
case lib.to_s
when 'nyaplot'
Nyaplot.init_script
when 'highcharts'
LazyHighCharts.init_script
when 'googlecharts'
GoogleVisualr.init_script
when 'datatables'
DataTables.init_script
else
raise ArgumentError, "Unsupported library #{lib}"
end
end
# @param libraries [Array] libraries whose dependencies are to be
# loaded
# @return [void, String] dependent script for the libraries
#
# @example
# To load the dependent JS file for Nyaplot and GoogleCharts libraries
# Daru::View.dependent_scripts(['nyaplot', 'googlecharts'])
# @example
# To load the dependent JS file for Nyaplot and GoogleCharts libraries
# Daru::View.dependent_scripts([:nyaplot, :googlecharts])
def dependent_scripts(libraries=[])
load_libs_in_iruby(libraries) if defined? IRuby
rescue NameError
script = ''
libraries.each do |library|
script << dependent_script(library)
end
script
end
private
# @param libraries [Array] Adapters whose JS files will be loaded
# @return [void] load the dependent JS files for the adapter in IRuby
# notebook
def load_libs_in_iruby(libraries=[])
libraries.each do |library|
load_lib_in_iruby(library.to_s)
end
end
# Load the dependent JS files in IRuby notebook. Those JS will help in
# plotting the charts in IRuby cell.
def load_lib_in_iruby(library)
if library.match('highcharts')
library = 'LazyHighCharts'
Object.const_get(library).init_iruby
elsif library.match('googlecharts')
library = 'GoogleVisualr'
Object.const_get(library).init_iruby
elsif library.match('datatables')
library = 'DataTables'
Object.const_get(library).init_iruby
else
Object.const_get(library.capitalize).init_iruby
end
end
end
end
end