export_csv 0.0.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in export_csv.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Pravin Mishra
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # ExportCsv
2
+
3
+ ExportCsv is a small library intended to simplify the common steps involved with export table data to CSV files. ExportCsv is compatible with recent 1.8 versions of Ruby as well as Ruby 1.9+
4
+
5
+ ###### Additional gem also provide rake db tasks. The command rake db:export_to_csv model=User pulls a database table into a csv.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'export_csv'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install export_csv
20
+
21
+ ## Example
22
+
23
+ ### Example EXPORT CSV File
24
+
25
+ export_to_csv('parent.csv', 'Parent',["id", "created_at", "updated_at"], options = { :separator=> ',', options = { :separator=> ',', :headers => ["fname","lname"]}})
26
+
27
+ ### Example EXPORT CSV File using rake
28
+
29
+ rake db:export_to_csv model=User
30
+
31
+ ## Usage
32
+
33
+ ### Example EXPORT CSV File
34
+
35
+ #### That gem provide export_to_csv(filename, model, escape_attributes, [options = {}]) method in your controller.
36
+
37
+ filename - String, which define output file's name. Gem will append .csv extension if file does not has extension
38
+ or having other extension other than csv.
39
+
40
+ model - Model name, Of date you want to export.
41
+
42
+ escape_attributes - The array of attribute which you want to exclude in csv header and data.
43
+
44
+ options - Optional argument. Expects it will be some kind of hash. Processed keys:
45
+ :separator - sets custom separator(by default is ",")
46
+ :headers - sets header row. Expected array with columns' names
47
+
48
+ ### Example EXPORT CSV File using rake
49
+
50
+
51
+ If you have a table you want to backup into a csv in the rails db with a model: Model
52
+
53
+ rake db:export_to_csv model=Model
54
+
55
+ the call writes into a csv file Model.csv located in the app root directory
56
+
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'export_csv/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "export_csv"
8
+ gem.version = ExportCsv::VERSION
9
+ gem.authors = ["Pravin Mishra"]
10
+ gem.email = ["diatm.pravin.it.07.27@gmail.com"]
11
+ gem.description = %q{ExportCsv is a small library intended to simplify the common steps involved with export table data to CSV files. ExportCsv is compatible with recent 1.8 versions of Ruby as well as Ruby 1.9+}
12
+ gem.summary = %q{Export data from multiple table}
13
+ gem.homepage = "https://github.com/diatmpravin/export_csv.git"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rspec"
21
+ end
@@ -0,0 +1,67 @@
1
+ require "export_csv/file"
2
+
3
+ module ExportCsv
4
+ if RUBY_VERSION =~ /1.8/
5
+ require 'fastercsv'
6
+ CSV = FasterCSV
7
+ else
8
+ require 'csv'
9
+ end
10
+
11
+ module Base
12
+
13
+ private
14
+
15
+ def export_to_csv(filename, model, escape_att ,options = {})
16
+ csv_options = {}
17
+ csv_options[:col_sep] = options[:separator] || ','
18
+
19
+ headers = options[:headers]
20
+ csv_options[:headers] = headers.present?
21
+
22
+ filename = Extension.ext filename
23
+
24
+ begin
25
+ model_dir = Dir['**/models/**/*.rb'].detect {|f| model == File.basename(f, '.*').camelize}
26
+ if !model_dir.eql?(nil)
27
+ table = File.basename(model_dir, '.*').camelize.constantize
28
+ objects = table.all
29
+ begin
30
+ data = CSV.generate(csv_options) do |csv|
31
+ if csv_options[:headers]
32
+ csv << headers
33
+ else
34
+ headers = table.column_names.delete_if {|k, v| escape_att.include? k }
35
+ csv << headers.map {|k| k.gsub(/_/, ' ').capitalize}
36
+ end
37
+ row = Array.new
38
+ objects.each do |obj|
39
+ headers.each do |col|
40
+ begin
41
+ row << obj[col]
42
+ rescue Exception => e
43
+ row << 'Invalid data field'
44
+ end
45
+ end
46
+ csv << row
47
+ row.clear
48
+ end
49
+ end
50
+ rescue SystemCallError
51
+ $stderr.print "IO failed: " + $!
52
+ raise
53
+ end
54
+ else
55
+ puts "Table #{ENV['model']} could not be found"
56
+ end
57
+ rescue SystemCallError
58
+ $stderr.print "IO failed: " + $!
59
+ raise
60
+ end
61
+
62
+ send_data(data, :filename => filename,:type => 'text/csv')
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,16 @@
1
+ class Extension
2
+
3
+ class << self
4
+
5
+ def ext file
6
+ @ext = File.extname(file)
7
+ if !@ext.nil? and !@ext.blank? and @ext.to_s == ".csv"
8
+ return file
9
+ else
10
+ return file.split('.').first + '.' + 'csv'
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,3 @@
1
+ module ExportCsv
2
+ VERSION = "0.0.1.alpha"
3
+ end
data/lib/export_csv.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "export_csv/version"
2
+ require "rake_export/railtie"
3
+ require 'active_support'
4
+
5
+ module ExportCsv
6
+ extend ActiveSupport::Autoload
7
+ autoload :Base
8
+ end
9
+
10
+ ActiveSupport.on_load(:action_controller) do
11
+ include ExportCsv::Base
12
+ end
@@ -0,0 +1,25 @@
1
+ require 'csv'
2
+
3
+ namespace :db do
4
+ desc "Export database table into a csv file"
5
+ task :export_to_csv => :environment do
6
+ model_dir = Dir['**/models/**/*.rb'].detect {|f| ENV['model'] == File.basename(f, '.*').camelize}
7
+ if !model_dir.eql?(nil)
8
+ table = File.basename(model_dir, '.*').camelize.constantize
9
+ objects = table.all
10
+ CSV.open("#{table}.csv", "wb") do |csv|
11
+ csv << table.column_names
12
+ row = Array.new
13
+ objects.each do |obj|
14
+ table.column_names.each do |col|
15
+ row << obj[col]
16
+ end
17
+ csv << row
18
+ row.clear
19
+ end
20
+ end
21
+ else
22
+ puts "Table #{ENV['model']} could not be found"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module ExportCsv
2
+ class Railtie < ::Rails::Railtie
3
+ rake_tasks do
4
+ load "rake_export/export_import.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ require 'export_csv'
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: export_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Pravin Mishra
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ExportCsv is a small library intended to simplify the common steps involved
31
+ with export table data to CSV files. ExportCsv is compatible with recent 1.8 versions
32
+ of Ruby as well as Ruby 1.9+
33
+ email:
34
+ - diatm.pravin.it.07.27@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - export_csv.gemspec
45
+ - lib/export_csv.rb
46
+ - lib/export_csv/base.rb
47
+ - lib/export_csv/file.rb
48
+ - lib/export_csv/version.rb
49
+ - lib/rake_export/export_import.rake
50
+ - lib/rake_export/railtie.rb
51
+ - spec/spec_helper.rb
52
+ homepage: https://github.com/diatmpravin/export_csv.git
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>'
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.1
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.24
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Export data from multiple table
76
+ test_files:
77
+ - spec/spec_helper.rb