tabularasa 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008-2011 Ary Djmal
1
+ Copyright (c) 2011 Chris Rosario
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/Manifest CHANGED
@@ -3,7 +3,4 @@ README.rdoc
3
3
  Rakefile
4
4
  init.rb
5
5
  lib/tabularasa.rb
6
- tabularasa.gemspec
7
- test/test_helper.rb
8
- test/to_csv_test.rb
9
6
  Manifest
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = tabularasa gem
2
2
 
3
- This simple gem gives you the ability to call to_csv to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.
3
+ This simple gem gives you the ability to convert an array of activerecord and/or hash objects to tabular data.
4
4
 
5
5
 
6
6
  == Usage
@@ -13,16 +13,12 @@ This simple gem gives you the ability to call to_csv to a collection of activere
13
13
 
14
14
  @users.to_csv
15
15
  @users.to_csv(:only => [:last_name, :role]) # This will also set the order
16
- @users.to_csv(:headers => false)
16
+ @users.to_csv(:headers => ["Last Name", "Role"])
17
17
  @users.to_csv(:except => [:last_name, :role])
18
- @users.to_csv(:methods => :admin?)
19
- @users.to_csv({:methods => :admin?}, {:col_sep => '\t'}) # Send csv generation options
20
18
 
21
19
 
22
20
  == Real life example
23
21
 
24
- In the controller where you want to export to csv, add the format.csv line (as of rails 2.1 it is not necessary to register the csv myme_type)
25
-
26
22
  class UserController < ApplicationController
27
23
  def index
28
24
  @users = User.all
@@ -46,14 +42,9 @@ In the controller where you want to export to csv, add the format.csv line (as o
46
42
  gem install tabularasa
47
43
 
48
44
 
49
- == Note
50
-
51
- Does not work on a single activerecord, ie, User.first.to_csv.
52
-
53
45
  == Contributors
54
46
 
55
- * Ary Djmal (initial plugin)
56
- * Chris Roasario (gem conversion)
47
+ * Chris Roasario
57
48
 
58
49
 
59
50
  Copyright (c) 2011 Chris Rosario, released under the MIT license
data/Rakefile CHANGED
@@ -1,23 +1,12 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
1
  require 'rubygems'
4
2
  require 'echoe'
5
3
 
6
- desc 'Default: run unit tests.'
7
- task :default => :test
8
-
9
- desc 'Test the to_csv plugin.'
10
- Rake::TestTask.new(:test) do |t|
11
- t.pattern = 'test/**/*_test.rb'
12
- t.verbose = true
13
- end
14
-
15
- Echoe.new('tabularasa', '0.1.6') do |p|
16
- p.summary = "Gives the ability to convert activerecord objects into csv"
17
- p.description = "This Rails gem gives you the ability to call to_csv to a collection of activerecords"
4
+ Echoe.new('tabularasa', '0.2.0') do |p|
5
+ p.summary = "Gives the ability to convert homogeneous activerecord and/or hash objects into csv"
6
+ p.description = "This Rails gem gives you the ability to call to_csv to a collection of homogenous activerecords and/or hashes"
18
7
  p.url = "https://github.com/callenrosario/tabularasa"
19
- p.author = ["Chris Rosario", "Ary Djmal"]
20
- p.email = "arydjmal@gmail.com"
8
+ p.author = "Chris Rosario"
9
+ p.email = "callenrosario@gmail.com"
21
10
  p.ignore_pattern = ["tmp/*", "script/*"]
22
11
  p.runtime_dependencies = []
23
12
  p.development_dependencies = []
data/init.rb CHANGED
@@ -1,2 +1 @@
1
- require 'csv'
2
1
  require 'tabularasa'
data/lib/tabularasa.rb CHANGED
@@ -1,28 +1,97 @@
1
- class Array
2
- require 'csv'
3
- def to_csv(options = {}, csv_options = {})
4
- return '' if self.empty?
1
+ require 'csv'
2
+
3
+ class Tabularasa
4
+ def initialize(rows, options = {}, &block)
5
+ @rows = rows
6
+ @keys = (options[:only] || get_keys) - (options[:exclude] || [])
7
+ @headers = options[:headers] || options[:only] || get_headers
8
+ end
5
9
 
6
- klass = self.first.class
7
- attributes = self.first.attributes.keys.sort.map(&:to_sym)
10
+ def to_csv
11
+ csv_data = CSV.generate do |csv|
12
+ csv << @headers
13
+ @rows.each do |row|
14
+ csv << collect(row)
15
+ end
16
+ end
17
+ csv_data
18
+ end
8
19
 
9
- if options[:only]
10
- columns = Array(options[:only]) & attributes
11
- else
12
- columns = attributes - Array(options[:except])
20
+ private
21
+ def data_type(object = @rows.first)
22
+ return 'active_record' if object.is_a?(ActiveRecord::Base)
23
+ return 'hash' if object.is_a?(Hash)
24
+ return 'array' if object.is_a?(Array) && !object.first.is_a?(Array)
25
+ return 'unsupported'
13
26
  end
14
27
 
15
- columns += Array(options[:methods])
16
28
 
17
- return '' if columns.empty?
29
+ def collect(object)
30
+ send "collect_#{data_type}", (object)
31
+ end
18
32
 
19
- output = CSV.generate(csv_options) do |csv|
20
- csv << columns.map { |column| klass.human_attribute_name(column) } unless options[:headers] == false
21
- self.each do |item|
22
- csv << columns.collect { |column| item.send(column) }
23
- end
33
+ def collect_active_record(object)
34
+ @keys.collect{|key| object[key]}
24
35
  end
25
36
 
26
- output
27
- end
37
+ def collect_hash(object)
38
+ @keys.collect{|key| object[key]}
39
+ end
40
+
41
+ def collect_array(object)
42
+ @keys.collect{|key| object[key]}
43
+ end
44
+
45
+ def collect_unsupported(object)
46
+ []
47
+ end
48
+
49
+
50
+ def get_keys
51
+ send "get_#{data_type}_keys"
52
+ end
53
+
54
+ def get_active_record_keys
55
+ @rows.first.attributes.keys
56
+ end
57
+
58
+ def get_hash_keys
59
+ @rows.first.keys
60
+ end
61
+
62
+ def get_array_keys
63
+ (0...@rows.first.size).collect{|n| n}
64
+ end
65
+
66
+ def get_unsupported_keys
67
+ []
68
+ end
69
+
70
+
71
+ def get_headers
72
+ send "get_#{data_type}_headers"
73
+ end
74
+
75
+ def get_active_record_headers
76
+ @rows.first.attributes.keys
77
+ end
78
+
79
+ def get_hash_headers
80
+ @rows.first.stringify_keys.keys
81
+ end
82
+
83
+ def get_array_headers
84
+ (0...@rows.first.size).collect{|n| n.to_s}
85
+ end
86
+
87
+ def get_unsupported_headers
88
+ []
89
+ end
90
+ end
91
+
92
+ class Array
93
+ def to_csv(options = {}, &block)
94
+ tabularasa = Tabularasa.new(self, options, &block)
95
+ tabularasa.to_csv
96
+ end
28
97
  end
data/tabularasa.gemspec CHANGED
@@ -2,22 +2,21 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "tabularasa"
5
- s.version = "0.1.6"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Chris Rosario, Ary Djmal"]
9
- s.date = "2011-11-23"
10
- s.description = "This Rails gem gives you the ability to call to_csv to a collection of activerecords"
11
- s.email = "arydjmal@gmail.com"
8
+ s.authors = ["Chris Rosario"]
9
+ s.date = "2011-11-29"
10
+ s.description = "This Rails gem gives you the ability to call to_csv to a collection of homogenous activerecords and/or hashes"
11
+ s.email = "callenrosario@gmail.com"
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/tabularasa.rb"]
13
- s.files = ["MIT-LICENSE", "README.rdoc", "Rakefile", "init.rb", "lib/tabularasa.rb", "tabularasa.gemspec", "test/test_helper.rb", "test/to_csv_test.rb", "Manifest"]
13
+ s.files = ["MIT-LICENSE", "README.rdoc", "Rakefile", "init.rb", "lib/tabularasa.rb", "Manifest", "tabularasa.gemspec"]
14
14
  s.homepage = "https://github.com/callenrosario/tabularasa"
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Tabularasa", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = "tabularasa"
18
18
  s.rubygems_version = "1.8.11"
19
- s.summary = "Gives the ability to convert activerecord objects into csv"
20
- s.test_files = ["test/test_helper.rb", "test/to_csv_test.rb"]
19
+ s.summary = "Gives the ability to convert homogeneous activerecord and/or hash objects into csv"
21
20
 
22
21
  if s.respond_to? :specification_version then
23
22
  s.specification_version = 3
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabularasa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Chris Rosario, Ary Djmal
8
+ - Chris Rosario
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-23 00:00:00.000000000Z
12
+ date: 2011-11-29 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: This Rails gem gives you the ability to call to_csv to a collection of
15
- activerecords
16
- email: arydjmal@gmail.com
15
+ homogenous activerecords and/or hashes
16
+ email: callenrosario@gmail.com
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files:
@@ -25,10 +25,8 @@ files:
25
25
  - Rakefile
26
26
  - init.rb
27
27
  - lib/tabularasa.rb
28
- - tabularasa.gemspec
29
- - test/test_helper.rb
30
- - test/to_csv_test.rb
31
28
  - Manifest
29
+ - tabularasa.gemspec
32
30
  homepage: https://github.com/callenrosario/tabularasa
33
31
  licenses: []
34
32
  post_install_message:
@@ -58,7 +56,6 @@ rubyforge_project: tabularasa
58
56
  rubygems_version: 1.8.11
59
57
  signing_key:
60
58
  specification_version: 3
61
- summary: Gives the ability to convert activerecord objects into csv
62
- test_files:
63
- - test/test_helper.rb
64
- - test/to_csv_test.rb
59
+ summary: Gives the ability to convert homogeneous activerecord and/or hash objects
60
+ into csv
61
+ test_files: []
data/test/test_helper.rb DELETED
@@ -1,37 +0,0 @@
1
- begin
2
- require 'rubygems'
3
- require 'test/unit'
4
- require 'active_support'
5
- require 'active_support/inflector'
6
- if RUBY_VERSION >= "1.9.0"
7
- require 'csv'
8
- else
9
- require 'fastercsv'
10
- end
11
- require File.dirname(__FILE__) + '/../lib/to_csv'
12
- rescue LoadError
13
- puts 'to_csv tests rely on active_support, and fastercsv if ruby version < 1.9'
14
- end
15
-
16
- class User
17
- COLUMNS = %w(id name age)
18
-
19
- attr_accessor *COLUMNS
20
-
21
- def self.human_attribute_name(attribute)
22
- attribute.to_s.humanize
23
- end
24
-
25
- def initialize(params={})
26
- params.each { |key, value| self.send("#{key}=", value); }
27
- self
28
- end
29
-
30
- def attributes
31
- COLUMNS.inject({}) { |attributes, attribute| attributes.merge(attribute => send(attribute)) }
32
- end
33
-
34
- def is_old?
35
- age > 40
36
- end
37
- end
data/test/to_csv_test.rb DELETED
@@ -1,50 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class ToCsvTest < Test::Unit::TestCase
4
- def setup
5
- @users = [
6
- User.new(:id => 1, :name => 'Ary', :age => 25),
7
- User.new(:id => 2, :name => 'Nati', :age => 22)
8
- ]
9
- end
10
-
11
- def test_with_empty_array
12
- assert_equal( "", [].to_csv )
13
- end
14
-
15
- def test_with_csv_options
16
- assert_equal( "Age\tId\tName\n25\t1\tAry\n22\t2\tNati\n", @users.to_csv({}, :col_sep => "\t"))
17
- end
18
-
19
- def test_with_no_options
20
- assert_equal( "Age,Id,Name\n25,1,Ary\n22,2,Nati\n", @users.to_csv )
21
- end
22
-
23
- def test_with_no_headers
24
- assert_equal( "25,1,Ary\n22,2,Nati\n", @users.to_csv(:headers => false) )
25
- end
26
-
27
- def test_with_only
28
- assert_equal( "Name\nAry\nNati\n", @users.to_csv(:only => :name) )
29
- end
30
-
31
- def test_with_empty_only
32
- assert_equal( "", @users.to_csv(:only => "") )
33
- end
34
-
35
- def test_with_only_and_wrong_column_names
36
- assert_equal( "Name\nAry\nNati\n", @users.to_csv(:only => [:name, :yoyo]) )
37
- end
38
-
39
- def test_with_except
40
- assert_equal( "Age\n25\n22\n", @users.to_csv(:except => [:id, :name]) )
41
- end
42
-
43
- def test_with_except_and_only_should_listen_to_only
44
- assert_equal( "Name\nAry\nNati\n", @users.to_csv(:except => [:id, :name], :only => :name) )
45
- end
46
-
47
- def test_with_except
48
- assert_equal( "Age,Id,Name,Is old?\n25,1,Ary,false\n22,2,Nati,false\n", @users.to_csv(:methods => [:is_old?]) )
49
- end
50
- end