mongoid_to_csv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.2-p0@mongoid_to_csv
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid_to_csv.gemspec
4
+ gemspec
@@ -0,0 +1,59 @@
1
+ = Mongoid.to_csv
2
+
3
+ == Description
4
+
5
+ A simple Mongoid::Base to_csv() class method that preserves scopes.
6
+ to_csv() returns the entire contents including the header ready to be written to file.
7
+
8
+ == Usage
9
+
10
+ # Assuming a Movie model with title and director_id columns.
11
+ Movie.to_csv
12
+ # would return:
13
+ title,director_id
14
+ title,director_id
15
+ Black Swan,0
16
+ Inception,1
17
+ The Fighter,2
18
+ The King's Speech,3
19
+ The Kids Are All Right,4
20
+
21
+ Movie.bad.to_csv
22
+ # would return:
23
+ title,director_id
24
+ The Kids Are All Right,4
25
+
26
+ Note that #to_csv is called like a scope or query. The following will NOT give you the same results:
27
+
28
+ Movie.all.to_csv
29
+
30
+ This will use Ruby's Array#to_csv method.
31
+
32
+ === Attribute#to_csv
33
+
34
+ After a model object's attributes are collected, to_csv is called on the resulting array. However, this poses a problem because it will blindly convert the attributes to a string -- i.e. call to_s on them. If one of your attributes is a Date, then calling to_s may produce unwanted output. For example, if you have Date::DATE_FORMATS[:default] = '%d %B, %Y' your dates will have the month written out like 'January', 'February', etc. To counter this, this gem will make an attempt to call to_csv() on each attribute. To get YYYY-MM-DD output, you could do something like:
35
+
36
+ class Date
37
+ def to_csv
38
+ strftime('%Y-%m-%d')
39
+ end
40
+ end
41
+
42
+ Note that object.send(attribute_name) is used, so datetime fields will be returned as ActiveSupport::TimeWithZone objects.
43
+
44
+ == TODO
45
+
46
+ * Options to specify columns to be included (currently, id and timestamp columns are excluded).
47
+ * Combine with active_record_to_csv somehow since they are essentially doing the same thing.
48
+
49
+ == Compatibility
50
+
51
+ Tested with Mongoid v2.0.2
52
+
53
+ http://gem-testers.org/gems/mongoid_to_csv
54
+
55
+ == Related gems
56
+
57
+ * https://github.com/ordinaryzelig/mongoid_csv
58
+ * https://github.com/ordinaryzelig/orm_from_csv
59
+ * https://github.com/ordinaryzelig/active_record_to_csv
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,35 @@
1
+ require 'mongoid'
2
+ require 'csv'
3
+
4
+ module MongoidToCSV
5
+ # Return full CSV content with headers as string.
6
+ # Defined as class method which will have chained scopes applied.
7
+ def to_csv
8
+ csv_columns = fields.keys - %w{_id created_at updated_at _type}
9
+ header_row = csv_columns.to_csv
10
+ records_rows = all.map do |record|
11
+ csv_columns.map do |column|
12
+ value = record.send(column)
13
+ value = value.to_csv if value.respond_to?(:to_csv)
14
+ value
15
+ end.to_csv
16
+ end.join
17
+ header_row + records_rows
18
+ end
19
+ end
20
+
21
+ module Mongoid::Document
22
+ def self.included(target)
23
+ target.extend MongoidToCSV
24
+ end
25
+ end
26
+
27
+ # Define Relation#to_csv so that method_missing will not
28
+ # delegate to array.
29
+ class Mongoid::Relation
30
+ def to_csv
31
+ scoping do
32
+ @klass.to_csv
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidToCSV
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid_to_csv/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid_to_csv"
7
+ s.version = MongoidToCSV::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jared Ning"]
10
+ s.email = ["jared@redningja.com"]
11
+ s.homepage = "https://github.com/ordinaryzelig/mongoid_to_csv"
12
+ s.summary = %q{Simple Mongoid Model to_csv() class method that preserves scopes}
13
+ s.description = %q{Simple Mongoid Model to_csv() class method that preserves scopes}
14
+
15
+ s.rubyforge_project = "mongoid_to_csv"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'mongoid', '>= 2.0.0.rc.7'
23
+
24
+ s.add_development_dependency 'rspec', '2.6.0'
25
+ s.add_development_dependency 'mongo', '>= 1.3.1'
26
+ s.add_development_dependency 'bson_ext'
27
+ end
@@ -0,0 +1,9 @@
1
+ # Initialize/connect.
2
+ host = 'localhost'
3
+ port = Mongo::Connection::DEFAULT_PORT
4
+ database_name = 'mongoid_to_csv'
5
+ connection = Mongo::Connection.new(host, port)
6
+ # Drop database if it exists.
7
+ connection.drop_database(database_name)
8
+ db = connection.db(database_name)
9
+ Mongoid.database = db
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe MongoidToCSV do
4
+
5
+ before :all do
6
+ [
7
+ 'Black Swan',
8
+ 'Inception',
9
+ 'The Fighter',
10
+ "The King's Speech",
11
+ 'The Kids Are All Right'
12
+ ].each_with_index do |title, i|
13
+ Movie.create!(
14
+ :title => title,
15
+ :released_on => Date.new(2010, 1, i + 1)
16
+ )
17
+ end
18
+ end
19
+
20
+ it 'should convert records to csv' do
21
+ Movie.to_csv.should eq(content_of(fixtures_file('movies.csv')))
22
+ end
23
+
24
+ it' should preserve scope' do
25
+ Movie.bad.to_csv.should eq(content_of(fixtures_file('movies_bad.csv')))
26
+ end
27
+
28
+ it 'should not include id and timestamp fields' do
29
+ header = Movie.to_csv.lines.first
30
+ header.should_not =~ /^id|created_at|updated_at/
31
+ end
32
+
33
+ it 'should call to_csv on each attribute if it responds to it' do
34
+ date = Movie.limit(1).first.released_on
35
+ csv_string = Movie.limit(1).to_csv
36
+ csv_string.should include(date.to_csv)
37
+ csv_string.should_not include(date.to_s)
38
+ end
39
+
40
+ end
@@ -0,0 +1,12 @@
1
+ require 'pathname'
2
+
3
+ require File.join(Pathname(__FILE__).dirname.expand_path, '../lib/mongoid_to_csv')
4
+
5
+ require 'database'
6
+
7
+ # require support .rb files.
8
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ config.include Macros
12
+ end
@@ -0,0 +1,6 @@
1
+ title,released_on
2
+ Black Swan,2010/01/01
3
+ Inception,2010/01/02
4
+ The Fighter,2010/01/03
5
+ The King's Speech,2010/01/04
6
+ The Kids Are All Right,2010/01/05
@@ -0,0 +1,2 @@
1
+ title,released_on
2
+ The Kids Are All Right,2010/01/05
@@ -0,0 +1,11 @@
1
+ module Macros
2
+
3
+ def content_of(file)
4
+ file.read
5
+ end
6
+
7
+ def fixtures_file(file_name)
8
+ File.open(File.join(Pathname(__FILE__).dirname.expand_path, 'fixtures', file_name))
9
+ end
10
+
11
+ end
@@ -0,0 +1,5 @@
1
+ class Date
2
+ def to_csv
3
+ strftime('%Y/%m/%d')
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ class Movie
2
+
3
+ include Mongoid::Document
4
+
5
+ field :title
6
+ field :released_on, type: Date
7
+ include Mongoid::Timestamps
8
+
9
+ scope :bad, where(:title => 'The Kids Are All Right')
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_to_csv
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jared Ning
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-20 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: mongoid
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.0.0.rc.7
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - "="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.6.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: mongo
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.1
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: bson_ext
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ description: Simple Mongoid Model to_csv() class method that preserves scopes
61
+ email:
62
+ - jared@redningja.com
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files: []
68
+
69
+ files:
70
+ - .gitignore
71
+ - .rvmrc
72
+ - Gemfile
73
+ - README.rdoc
74
+ - Rakefile
75
+ - lib/mongoid_to_csv.rb
76
+ - lib/mongoid_to_csv/version.rb
77
+ - mongoid_to_csv.gemspec
78
+ - spec/database.rb
79
+ - spec/mongoid_to_csv_spec.rb
80
+ - spec/spec_helper.rb
81
+ - spec/support/fixtures/movies.csv
82
+ - spec/support/fixtures/movies_bad.csv
83
+ - spec/support/macros.rb
84
+ - spec/support/models/date.rb
85
+ - spec/support/models/movie.rb
86
+ has_rdoc: true
87
+ homepage: https://github.com/ordinaryzelig/mongoid_to_csv
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project: mongoid_to_csv
110
+ rubygems_version: 1.6.1
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Simple Mongoid Model to_csv() class method that preserves scopes
114
+ test_files:
115
+ - spec/database.rb
116
+ - spec/mongoid_to_csv_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/support/fixtures/movies.csv
119
+ - spec/support/fixtures/movies_bad.csv
120
+ - spec/support/macros.rb
121
+ - spec/support/models/date.rb
122
+ - spec/support/models/movie.rb