Class: ChartJS::EventStream
- Inherits:
-
Object
- Object
- ChartJS::EventStream
- Defined in:
- lib/chart_js/chart/event_stream.rb
Instance Method Summary collapse
- #build(&block) ⇒ Object
- #chart(chart_obj = nil) ⇒ Object
- #counter(dataset:, chart: @chart, counter: "counter") ⇒ Object
-
#initialize(path, chart) ⇒ EventStream
constructor
A new instance of EventStream.
- #path(value = nil) ⇒ Object
- #push(dataset: 0, chart: @chart, &block) ⇒ Object
-
#raw(chart: @chart, str: nil, file: nil, without_source: true, update: true) ⇒ Object
end.
- #to_html(source: @path, chart: @chart) ⇒ Object
Constructor Details
#initialize(path, chart) ⇒ EventStream
Returns a new instance of EventStream.
49 50 51 52 53 54 |
# File 'lib/chart_js/chart/event_stream.rb', line 49 def initialize(path, chart) @push = nil chart(chart) path(path) build(block) if block_given? end |
Instance Method Details
#build(&block) ⇒ Object
56 57 58 59 |
# File 'lib/chart_js/chart/event_stream.rb', line 56 def build(&block) instance_eval(&block) self end |
#chart(chart_obj = nil) ⇒ Object
44 45 46 47 |
# File 'lib/chart_js/chart/event_stream.rb', line 44 def chart(chart_obj = nil) return @chart if chart_obj.nil? @chart = chart_obj end |
#counter(dataset:, chart: @chart, counter: "counter") ⇒ Object
38 39 40 41 42 |
# File 'lib/chart_js/chart/event_stream.rb', line 38 def counter(dataset:, chart: @chart, counter: "counter") str = "" str << "var #{counter} = 0;\n" str << "source.onmessage = function(e) { #{counter} += 1 };\n" end |
#path(value = nil) ⇒ Object
61 62 63 64 |
# File 'lib/chart_js/chart/event_stream.rb', line 61 def path(value = nil) return @path if value.nil? @path = value end |
#push(dataset: 0, chart: @chart, &block) ⇒ Object
19 20 21 |
# File 'lib/chart_js/chart/event_stream.rb', line 19 def push(dataset: 0, chart: @chart, &block) @push = EventStreams.push(dataset: dataset, chart: @chart, &block) end |
#raw(chart: @chart, str: nil, file: nil, without_source: true, update: true) ⇒ Object
end
28 29 30 31 32 33 34 35 36 |
# File 'lib/chart_js/chart/event_stream.rb', line 28 def raw(chart: @chart, str: nil, file: nil, without_source: true, update: true) @raw = "" @raw = @raw + "source.onmessage = function(e) { json = JSON.parse(e.data);" return @raw if str.nil? and file.nil? @raw = @raw + str unless str.nil? @raw = @raw + File.readlines(file) unless file.nil? @rar = @raw + "#{chart}.update();" if update @raw = @raw + "};" end |
#to_html(source: @path, chart: @chart) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/chart_js/chart/event_stream.rb', line 7 def to_html(source: @path, chart: @chart) str = "" str << "<script>\n" str << "var source = new EventSource('#{path}');\n" str << "var json;\n" str << @counter if @counter str << @push.to_s if @push str << @pop if @pop str << @raw if @raw str << "</script>\n" end |