mongoid_to_csv 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm use ruby-1.9.2-p0@mongoid_to_csv
1
+ rvm use ruby-1.9.2-p318@mongoid_to_csv
@@ -23,11 +23,11 @@ to_csv() returns the entire contents including the header ready to be written to
23
23
  title,director_id
24
24
  The Kids Are All Right,4
25
25
 
26
- Note that #to_csv is called like a scope or query. The following will NOT give you the same results:
26
+ For normal arrays, `to_csv` works the same way.
27
27
 
28
28
  Movie.all.to_csv
29
-
30
- This will use Ruby's Array#to_csv method.
29
+ # Same as
30
+ Movie.to_csv
31
31
 
32
32
  === Attribute#to_csv
33
33
 
@@ -48,9 +48,7 @@ Note that object.send(attribute_name) is used, so datetime fields will be return
48
48
 
49
49
  == Compatibility
50
50
 
51
- Tested with Mongoid v2.0.2
52
-
53
- http://gem-testers.org/gems/mongoid_to_csv
51
+ Tested with Ruby 1.9.2-p290 and Mongoid v2.0.2
54
52
 
55
53
  == Related gems
56
54
 
@@ -2,12 +2,20 @@ require 'mongoid'
2
2
  require 'csv'
3
3
 
4
4
  module MongoidToCSV
5
+
5
6
  # Return full CSV content with headers as string.
6
7
  # Defined as class method which will have chained scopes applied.
7
8
  def to_csv
9
+ documents_to_csv(all, fields)
10
+ end
11
+
12
+ module_function
13
+
14
+ def documents_to_csv(documents, fields = documents.first.class.fields)
15
+ doc_class = documents.first.class
8
16
  csv_columns = fields.keys - %w{_id created_at updated_at _type}
9
17
  header_row = csv_columns.to_csv
10
- records_rows = all.map do |record|
18
+ records_rows = documents.map do |record|
11
19
  csv_columns.map do |column|
12
20
  value = record.send(column)
13
21
  value = value.to_csv if value.respond_to?(:to_csv)
@@ -16,6 +24,7 @@ module MongoidToCSV
16
24
  end.join
17
25
  header_row + records_rows
18
26
  end
27
+
19
28
  end
20
29
 
21
30
  module Mongoid::Document
@@ -33,3 +42,14 @@ class Mongoid::Relation
33
42
  end
34
43
  end
35
44
  end
45
+
46
+ module ArrayToCSV
47
+ def to_csv
48
+ if first.is_a?(Mongoid::Document)
49
+ MongoidToCSV.documents_to_csv(self)
50
+ else
51
+ super
52
+ end
53
+ end
54
+ end
55
+ Array.send :include, ArrayToCSV
@@ -1,3 +1,3 @@
1
1
  module MongoidToCSV
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -19,6 +19,9 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
+ if ENV['RUBY_VERSION'] < 'ruby-1.9.2-p318'
23
+ s.add_dependency 'fastercsv'
24
+ end
22
25
  s.add_dependency 'mongoid', '>= 2.0.0.rc.7'
23
26
 
24
27
  s.add_development_dependency 'rspec', '2.6.0'
@@ -37,4 +37,8 @@ describe MongoidToCSV do
37
37
  csv_string.should_not include(date.to_s)
38
38
  end
39
39
 
40
+ it 'works on an array' do
41
+ Movie.all.to_csv.should == Movie.to_csv
42
+ end
43
+
40
44
  end
metadata CHANGED
@@ -1,72 +1,67 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mongoid_to_csv
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
4
5
  prerelease:
5
- version: 0.1.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Jared Ning
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-07-20 00:00:00 -05:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-11 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: mongoid
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70342848827680 !ruby/object:Gem::Requirement
20
17
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
24
21
  version: 2.0.0.rc.7
25
22
  type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
23
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70342848827680
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70342848827180 !ruby/object:Gem::Requirement
31
28
  none: false
32
- requirements:
33
- - - "="
34
- - !ruby/object:Gem::Version
29
+ requirements:
30
+ - - =
31
+ - !ruby/object:Gem::Version
35
32
  version: 2.6.0
36
33
  type: :development
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
39
- name: mongo
40
34
  prerelease: false
41
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70342848827180
36
+ - !ruby/object:Gem::Dependency
37
+ name: mongo
38
+ requirement: &70342848826720 !ruby/object:Gem::Requirement
42
39
  none: false
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
46
43
  version: 1.3.1
47
44
  type: :development
48
- version_requirements: *id003
49
- - !ruby/object:Gem::Dependency
50
- name: bson_ext
51
45
  prerelease: false
52
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *70342848826720
47
+ - !ruby/object:Gem::Dependency
48
+ name: bson_ext
49
+ requirement: &70342848826340 !ruby/object:Gem::Requirement
53
50
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
58
55
  type: :development
59
- version_requirements: *id004
56
+ prerelease: false
57
+ version_requirements: *70342848826340
60
58
  description: Simple Mongoid Model to_csv() class method that preserves scopes
61
- email:
59
+ email:
62
60
  - jared@redningja.com
63
61
  executables: []
64
-
65
62
  extensions: []
66
-
67
63
  extra_rdoc_files: []
68
-
69
- files:
64
+ files:
70
65
  - .gitignore
71
66
  - .rvmrc
72
67
  - Gemfile
@@ -83,35 +78,31 @@ files:
83
78
  - spec/support/macros.rb
84
79
  - spec/support/models/date.rb
85
80
  - spec/support/models/movie.rb
86
- has_rdoc: true
87
81
  homepage: https://github.com/ordinaryzelig/mongoid_to_csv
88
82
  licenses: []
89
-
90
83
  post_install_message:
91
84
  rdoc_options: []
92
-
93
- require_paths:
85
+ require_paths:
94
86
  - lib
95
- required_ruby_version: !ruby/object:Gem::Requirement
87
+ required_ruby_version: !ruby/object:Gem::Requirement
96
88
  none: false
97
- requirements:
98
- - - ">="
99
- - !ruby/object:Gem::Version
100
- version: "0"
101
- required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
94
  none: false
103
- requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- version: "0"
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
107
99
  requirements: []
108
-
109
100
  rubyforge_project: mongoid_to_csv
110
- rubygems_version: 1.6.1
101
+ rubygems_version: 1.8.15
111
102
  signing_key:
112
103
  specification_version: 3
113
104
  summary: Simple Mongoid Model to_csv() class method that preserves scopes
114
- test_files:
105
+ test_files:
115
106
  - spec/database.rb
116
107
  - spec/mongoid_to_csv_spec.rb
117
108
  - spec/spec_helper.rb