Skip to content

Commit

Permalink
merged in bquorning's branch, and applied the previous refactorings t…
Browse files Browse the repository at this point in the history
…o it
  • Loading branch information
stefanpenner committed Dec 12, 2009
2 parents 66eb674 + 3b43b74 commit 505e149
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 154 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pkg
.swp
132 changes: 0 additions & 132 deletions README.markdown

This file was deleted.

10 changes: 6 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ require 'rake/gempackagetask'

spec = Gem::Specification.new do |s|
s.name = "comma"
s.version = "0.2.2"
s.version = "0.2.2.1"
s.author = "Marcus Crafter"
s.email = "crafterm@redartisan.com"
s.homepage = "http://github.com/crafterm/comma"
s.platform = Gem::Platform::RUBY
s.summary = "Ruby Comma Seperated Values generation library"
s.files = %w( README.markdown
s.files = %w( README.rdoc
MIT-LICENSE
lib/comma.rb
lib/comma/extractors.rb
lib/comma/object.rb
lib/comma/array.rb
lib/comma/renderascsv.rb )
lib/comma/renderascsv.rb
lib/comma/generator.rb
lib/comma/namedscope.rb )

s.require_path = "lib"
s.add_dependency("fastercsv", ">= 1.4.0")
s.add_dependency("activesupport", ">= 2.2.2")
end

Expand Down
2 changes: 1 addition & 1 deletion comma.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "comma"
s.version = "0.2.2"
s.version = "0.2.2.1"
s.author = "Marcus Crafter"
s.email = "crafterm@redartisan.com"
s.homepage = "http://github.com/crafterm/comma"
Expand Down
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'comma'
require 'comma'
Binary file removed lib/.comma.rb.swp
Binary file not shown.
11 changes: 9 additions & 2 deletions lib/comma.rb
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
15 changes: 1 addition & 14 deletions lib/comma/array.rb
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
35 changes: 35 additions & 0 deletions lib/comma/generator.rb
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
6 changes: 6 additions & 0 deletions lib/comma/namedscope.rb
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

39 changes: 39 additions & 0 deletions spec/comma/ar_spec.rb
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
9 changes: 9 additions & 0 deletions spec/comma/comma_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
@books.to_comma(:brief).should == "Name,Description\nSmalltalk-80,Language and Implementation\n"
end

describe 'with :filename specified' do
before{ @books.to_comma(:filename => 'comma.csv') }
after{ File.delete('comma.csv') }

it "should write to the file" do
File.read('comma.csv').should == "Title,Description,Issuer,ISBN-10,ISBN-13\nSmalltalk-80,Language and Implementation,ISBN,123123123,321321321\n"
end
end

describe "with FasterCVS options" do
it "should not change when options are empty" do
@books.to_comma({}).should == "Title,Description,Issuer,ISBN-10,ISBN-13\nSmalltalk-80,Language and Implementation,ISBN,123123123,321321321\n"
Expand Down

0 comments on commit 505e149

Please sign in to comment.