forked from comma-csv/comma
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged in bquorning's branch, and applied the previous refactorings t…
…o it
- Loading branch information
Showing
12 changed files
with
108 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pkg | ||
.swp |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
require 'comma' | ||
require 'comma' |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,39 @@ | ||
# conditional loading of activesupport | ||
if defined? Rails and Rails.version < '2.3.5' | ||
require 'activesupport' | ||
else | ||
require 'active_support' | ||
end | ||
|
||
# load the right csv library | ||
if RUBY_VERSION >= '1.9' | ||
require 'csv' | ||
FasterCSV = CSV | ||
else | ||
begin | ||
# try faster csv | ||
require 'fastercsv' | ||
rescue Error => e | ||
|
||
if defined? Rails | ||
Rails.logger.info "FasterCSV not installed, falling back on CSV" | ||
else | ||
puts "FasterCSV not installed, falling back on CSV" | ||
end | ||
|
||
require 'csv' | ||
FasterCSV = CSV | ||
end | ||
end | ||
|
||
require 'comma/extractors' | ||
require 'comma/generator' | ||
require 'comma/array' | ||
require 'comma/object' | ||
require 'comma/renderascsv' | ||
|
||
if defined?(ActiveRecord) | ||
require 'comma/namedscope' | ||
end | ||
|
||
if defined?(ActionController) | ||
ActionController::Base.send :include, RenderAsCSV | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,5 @@ | ||
class Array | ||
def to_comma(style = :default) | ||
options = {} | ||
|
||
if style.is_a? Hash | ||
options = style | ||
style = options.delete(:style)||:default | ||
end | ||
|
||
FasterCSV.generate(options) do |csv| | ||
return "" if empty? | ||
csv << first.to_comma_headers(style) # REVISIT: request to optionally include headers | ||
each do |object| | ||
csv << object.to_comma(style) | ||
end | ||
end | ||
Comma::Generator.new(self, style).run(:each) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Comma | ||
|
||
class Generator | ||
|
||
def initialize(instance, style) | ||
@instance = instance | ||
@style = style | ||
@options = {} | ||
|
||
if @style.is_a? Hash | ||
@options = @style.clone | ||
@style = @options.delete(:style) || :default | ||
@filename = @options.delete(:filename) | ||
end | ||
end | ||
|
||
def run(iterator_method) | ||
if @filename | ||
FasterCSV.open(@filename, 'w'){ |csv| append_csv(csv, iterator_method) } and return true | ||
else | ||
FasterCSV.generate(@options){ |csv| append_csv(csv, iterator_method) } | ||
end | ||
end | ||
|
||
private | ||
def append_csv(csv, iterator_method) | ||
return '' if @instance.empty? | ||
csv << @instance.first.to_comma_headers(@style) # REVISIT: request to optionally include headers | ||
@instance.send(iterator_method) do |object| | ||
csv << object.to_comma(@style) | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class ActiveRecord::NamedScope::Scope | ||
def to_comma(style = :default) | ||
Comma::Generator.new(self, style).run(:find_each) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
require 'active_record' | ||
|
||
describe Comma, 'generating CSV from an ActiveRecord object' do | ||
before(:all) do | ||
class Person < ActiveRecord::Base | ||
named_scope :teenagers, :conditions => { :age => 13..19 } | ||
comma do | ||
name | ||
age | ||
end | ||
end | ||
|
||
require 'active_record/connection_adapters/abstract_adapter' | ||
Column = ActiveRecord::ConnectionAdapters::Column | ||
end | ||
|
||
before do | ||
Person.stub!(:columns).and_return [Column.new('age', 0, 'integer', false), | ||
Column.new('name', nil, 'string', false) ] | ||
Person.stub!(:table_exists?).and_return(true) | ||
end | ||
|
||
describe 'case' do | ||
before do | ||
people = [ Person.new(:age => 18, :name => 'Junior') ] | ||
Person.stub!(:find_every).and_return people | ||
Person.stub!(:calculate).with(:count, :all, {}).and_return people.size | ||
end | ||
|
||
it 'should extend ActiveRecord::NamedScope::Scope to add a #to_comma method which will return CSV content for objects within the scope' do | ||
Person.teenagers.to_comma.should == "Name,Age\nJunior,18\n" | ||
end | ||
|
||
it 'should find in batches' do | ||
Person.teenagers.to_comma | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters