Skip to content

Commit

Permalink
Add methods for all types supported
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelXavier authored and rossmeissl committed Jul 2, 2010
1 parent 4169fd7 commit 79da7a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
19 changes: 12 additions & 7 deletions lib/grafico/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
module Grafico
module Helpers
GRAPH_TYPES = %w[LineGraph AreaGraph StackGraph StreamGraph BarGraph StackedBarGraph HorizontalBarGraph SparkLine SparkBar]

def graph_tag(graph_type, element, data, options = {})
args = []
args << data.to_json
args << options.to_json unless options.empty?
javascript_tag "var #{options[:variable_name] || (element + graph_type)} = new Grafico.#{graph_type}($('#{element}'), #{args.join(',')});"
end

def line_graph_tag(element, data, options = {})
graph_tag 'LineGraph', element, data, options
end

def sparkline_tag(element, data, options = {})
graph_tag 'SparkLine', element, data, options
def self.included(receiver)
GRAPH_TYPES.each do |graph|
receiver.instance_eval do
define_method(:"#{graph.underscore}_tag") do |e, d, *opts|
opts = opts.first || Hash.new
graph_tag(graph, e, d, opts)
end
end
end
end
end
end
end
10 changes: 4 additions & 6 deletions test/test_grafico.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ def test_graph_tag
assert_equal @helper.graph_tag('FooGraph', 'my_element', [1, 2, 3]), wrap_as_javascript("var my_elementFooGraph = new Grafico.FooGraph($('my_element'), [1,2,3]);")
end

def test_line_graph_tag
assert_equal @helper.line_graph_tag('my_element', [1, 2, 3]), wrap_as_javascript("var my_elementLineGraph = new Grafico.LineGraph($('my_element'), [1,2,3]);")
end

def test_sparkline_tag
assert_equal @helper.line_graph_tag('my_element', [1, 2, 3]), wrap_as_javascript("var my_elementSparkLine = new Grafico.SparkLine($('my_element'), [1,2,3]);")
def test_graph_types
Grafico::Helpers::GRAPH_TYPES.each do |type|
assert_equal @helper.send(:"#{type.underscore}_tag", 'my_element', [1, 2, 3]), wrap_as_javascript("var my_element#{type} = new Grafico.#{type}($('my_element'), [1,2,3]);")
end
end
end

0 comments on commit 79da7a5

Please sign in to comment.