Permalink
Browse files

class refactoring and added bundler

  • Loading branch information...
1 parent 09ec0d5 commit b39ab3276057d9a911abfd9442d9f5451eb24315 Rudolf Schmidt committed Mar 22, 2011
View
@@ -0,0 +1,7 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+
+# RubyMine
+.idea
View
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in google_charts/.gemspec
+gemspec
View
File renamed without changes.
View
File renamed without changes.
View
@@ -0,0 +1,4 @@
+$LOAD_PATH.unshift( 'lib' )
+
+require 'bundler'
+Bundler::GemHelper.install_tasks
View
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "google_charts/version"
+
+Gem::Specification.new do |s|
+ s.name = "google_charts"
+ s.version = GoogleCharts::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Rudolf Schmidt"]
+
+ s.homepage = ""
+ s.summary = %q{Google Charts with Ruby}
+ s.description = %q{GoogleCharts is a Ruby wrapper to the Google Chart API}
+
+ s.rubyforge_project = "google_charts"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency "actionpack", "~> 2.x"
+end
View
@@ -1,8 +1,9 @@
-require File.dirname(__FILE__) + '/lib/google_charts'
+require 'google_charts'
-if defined?( ActionView::Base )
- ActionView::Base.send :include, Rudionrails::GoogleCharts
-else
+begin
+ require 'actionpack'
+ ActionView::Base.send :include, GoogleCharts::Helpers::ActionView
+rescue LoadError
$stderr.puts "Skipping GoogleCharts plugin. `gem install actionpack` and try again."
end
View
@@ -1,15 +0,0 @@
-module Rudionrails
- module GoogleCharts
-
- def area_chart( collection, options = {}, html_options = {}, &block )
- chart = AreaChart.new( self, collection, options, html_options )
- yield( chart )
- concat( chart.to_html )
- end
-
- class AreaChart < LineChart
- end
-
- end
-end
-
View
@@ -1,15 +0,0 @@
-module Rudionrails
- module GoogleCharts
-
- def bar_chart( collection, options = {}, html_options = {}, &block )
- chart = BarChart.new( self, collection, options, html_options )
- yield( chart )
- concat( chart.to_html )
- end
-
- class BarChart < LineChart
- end
-
- end
-end
-
View
@@ -1,15 +0,0 @@
-module Rudionrails
- module GoogleCharts
-
- def column_chart( collection, options = {}, html_options = {}, &block )
- chart = ColumnChart.new( self, collection, options, html_options )
- yield( chart )
- concat( chart.to_html )
- end
-
- class ColumnChart < LineChart
- end
-
- end
-end
-
View
@@ -1,41 +0,0 @@
-module Rudionrails
- module GoogleCharts
-
- def line_chart( collection, options = {}, html_options = {}, &block )
- chart = LineChart.new( self, collection, options, html_options )
- yield( chart )
- concat( chart.to_html )
- end
-
- class LineChart < Base
- def initialize( template, collection, options = {}, html_options = {} )
- super
-
- @label, @values = [], []
- end
-
- def label( name, method ); @label = [name, method]; end
- def value( name, method ); @values << [name, method]; end
-
-
- protected
-
- def setup_data
- # setup the columns
- add_column( 'string', @label.first )
- @values.each { |val| add_column( 'number', val.first ) }
-
- # setup the rows
- @collection.each do |col|
- label = value_for( @label.last, col )
- values = @values.map { |value| value_for( value.last, col ) }
-
- add_row( [label, *values] )
- end
- end
-
- end
-
- end
-end
-
View
@@ -1,38 +0,0 @@
-module Rudionrails
- module GoogleCharts
-
- def pie_chart( collection, options = {}, html_options = {}, &block )
- chart = PieChart.new( self, collection, options, html_options )
- yield( chart )
- concat( chart.to_html )
- end
-
- class PieChart < Base
- def initialize( template, collection, options = {}, html_options = {} )
- super
-
- @label, @value = [], []
- end
-
- def label( name, method ); @label = [name, method]; end
- def value( name, method ); @value = [name, method]; end
-
-
- protected
-
- def setup_data
- # setup the columns
- add_column( 'string', @label.first )
- add_column( 'number', @value.first )
-
- # setup the rows
- @collection.each do |col|
- add_row( [value_for( @label.last, col ), value_for( @value.last, col )] )
- end
- end
-
- end
-
- end
-end
-
View
@@ -1,107 +1,8 @@
-module Rudionrails
- module GoogleCharts
+module GoogleCharts
- VERSION = "0.0.1.0"
+ autoload :Charts, File.dirname(__FILE__) + "/google_charts/charts"
+ autoload :Helpers, File.dirname(__FILE__) + "/google_charts/helpers"
- class Base
-
- def initialize( template, collection, options = {}, html_options = {} )
- @template = template
- @collection = collection
-
- @options = options
- @html_options = html_options.reverse_merge( :id => "googleChart" )
-
- @columns, @rows = [], []
- end
-
-
- def title( val ); @options[:title] = val; end
-
- def to_html
- setup_data
-
- [
- google_jsapi,
- container_div,
- google_chart
- ].join("\n")
- end
-
-
- protected
-
- def class_name; self.class.name.split( "::" ).last; end
- def packages; [class_name.downcase].to_json end
-
- def value_for( method_or_proc, obj )
- method_or_proc.is_a?( Proc ) ? method_or_proc.call( obj ) : obj.send( method_or_proc )
- end
-
- # stub
- def setup_data; end
-
- # === columns and rows
- def columns
- @columns.join( "\n" )
- end
-
- def add_column( type, value )
- @columns << "data.addColumn( '#{type}', '#{value}' );"
- end
-
- def rows
- [
- "data.addRows([ ",
- @rows.join( ", \n" ),
- "]);"
- ].join( "\n" )
- end
-
- def add_row( value )
- @rows << value.to_json
- end
-
-
- private
-
- def google_jsapi
- @template.javascript_tag(
- <<-EOS
- if( typeof google == 'undefined' ) {
- document.write(unescape("%3Cscript src='http://www.google.com/jsapi' type='text/javascript'%3E%3C/script%3E"));
- };
- EOS
- )
- end
-
- def google_chart
- @template.javascript_tag(
- <<-EOS
- google.load( "visualization", "1", { packages: #{packages} } );
- google.setOnLoadCallback( function() {
- var data = new google.visualization.DataTable();
-
- #{columns}
- #{rows}
-
- var chart = new google.visualization.#{class_name}( document.getElementById('#{@html_options[:id]}') );
- chart.draw( data, #{@options.to_json} );
- });
- EOS
- )
- end
-
- def container_div
- @template.content_tag( :div, "", :id => @html_options[:id] )
- end
-
- end
-
- end
end
-# get all the charts
-[ :pie, :line, :area, :bar, :column ].each { |chart| require File.dirname( __FILE__ ) + "/charts/#{chart}" }
-
@@ -0,0 +1,12 @@
+module GoogleCharts
+ module Charts
+
+ autoload :Base, File.dirname(__FILE__) + "/charts/base"
+ autoload :Pie, File.dirname(__FILE__) + "/charts/pie"
+ autoload :Line, File.dirname(__FILE__) + "/charts/line"
+ autoload :Area, File.dirname(__FILE__) + "/charts/area"
+ autoload :Bar, File.dirname(__FILE__) + "/charts/bar"
+ autoload :Column, File.dirname(__FILE__) + "/charts/column"
+
+ end
+end
@@ -0,0 +1,7 @@
+module GoogleCharts::Charts
+
+ class Area < GoogleCharts::Charts::Line
+ end
+
+end
+
@@ -0,0 +1,7 @@
+module GoogleCharts::Charts
+
+ class Bar < GoogleCharts::Charts::Line
+ end
+
+end
+
Oops, something went wrong.

0 comments on commit b39ab32

Please sign in to comment.