danielvlopes-smart_csv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Daniel Lopes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ = smart_csv
2
+
3
+ The main idea of this project is provide a simple way to export your models as CSV files.
4
+ It start as a part of a mine project, but after sometimes a need change from simple CSV to Ruport.
5
+ For this reason, I will not work in this project anymore.
6
+
7
+ But if it is good for you, take a look on the source code. Some basic features already working, like (but not tested yet):
8
+
9
+ * convert Arrays to csv
10
+ * convert instances of models to csv row
11
+ * render csv file based in a Rails controller.
12
+
13
+ == Copyright
14
+
15
+ Copyright (c) 2009 Daniel Lopes. See LICENSE for details.
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "smart_csv"
8
+ gem.summary = %Q{ Export your Rails models in CSV format}
9
+ gem.email = "danielvlopes@gmail.com"
10
+ gem.homepage = "http://github.com/danielvlopes/smart_csv"
11
+ gem.authors = ["Daniel Lopes"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "smart_csv #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ require 'smart_csv/array'
2
+ require 'smart_csv/exportable'
3
+ require 'smart_csv/render'
@@ -0,0 +1,14 @@
1
+ require 'fastercsv'
2
+
3
+ class Array
4
+ def to_csv(options = {})
5
+ if all? { |e| e.respond_to?(:to_row) }
6
+ FasterCSV.generate do |csv|
7
+ csv << first.export_columns(options[:format])
8
+ csv << map { |e| e.to_row(options[:format]) }
9
+ end
10
+ else
11
+ FasterCSV.generate_line(self, options)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ module SmartCsv
2
+ module Exportable
3
+
4
+ module ClassMethods
5
+ def self.to_csv(*args)
6
+ find(:all).to_csv(*args)
7
+ end
8
+ end
9
+
10
+ module InstanceMethods
11
+ def export_columns(format = nil)
12
+ self.class.content_columns.map(&:name) - ['created_at', 'updated_at']
13
+ end
14
+
15
+ def to_row(format = nil)
16
+ export_columns(format).map { |c| self.send(c) }
17
+ end
18
+ end
19
+
20
+ def self.included(receiver)
21
+ receiver.extend ClassMethods
22
+ receiver.send :include, InstanceMethods
23
+ end
24
+
25
+ end
26
+ end
27
+
28
+ ActiveRecord::Base.send :include, SmartCsv::Exportable
@@ -0,0 +1,14 @@
1
+ require 'activesupport'
2
+
3
+ if defined?(ActionController)
4
+
5
+ ActionController::Base.class_eval do
6
+ def render_with_csv(options = nil, extra_options = {}, &block)
7
+ return render_without_csv(options, extra_options, &block) unless (options.respond_to? '[]') and options[:csv]
8
+ data = options.delete(:csv)
9
+ send_data Array(data).to_csv, options.merge(:type => :csv)
10
+ end
11
+ alias_method_chain :render, :csv
12
+ end
13
+
14
+ end
@@ -0,0 +1 @@
1
+ require "smart_csv"
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{smart_csv}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Daniel Lopes"]
9
+ s.date = %q{2009-06-11}
10
+ s.email = %q{danielvlopes@gmail.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "lib/smart_csv.rb",
23
+ "lib/smart_csv/array.rb",
24
+ "lib/smart_csv/exportable.rb",
25
+ "lib/smart_csv/render.rb",
26
+ "rails/init.rb",
27
+ "smart_csv.gemspec",
28
+ "spec/factories.rb",
29
+ "spec/libs/book.rb",
30
+ "spec/smart_csv_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/danielvlopes/smart_csv}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.3}
37
+ s.summary = %q{Export your Rails models in CSV format}
38
+ s.test_files = [
39
+ "spec/factories.rb",
40
+ "spec/libs/book.rb",
41
+ "spec/smart_csv_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ Factory.define :book do |b|
2
+ b.name { Factory.next(:name) }
3
+ b.description 'Really great book about something'
4
+ end
5
+
6
+ Factory.sequence :name do |n|
7
+ "book#{n}"
8
+ end
@@ -0,0 +1,2 @@
1
+ class Book < ActiveRecord::Base
2
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "SmartCsv" do
4
+
5
+ it "should create a csv for array items" do
6
+ 3.times {Factory.create(:book)}
7
+ Book.all.to_csv.should_not be_nil
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require "ruby-debug"
3
+ require "active_record"
4
+ require 'factory_girl'
5
+ require 'spec'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'smart_csv'
10
+
11
+ Spec::Runner.configure do |config|
12
+ end
13
+
14
+ ActiveRecord::Schema.verbose = false
15
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
16
+ ActiveRecord::Base.configurations = true
17
+ ActiveRecord::Schema.define(:version => 1) do
18
+ create_table :books do |t|
19
+ t.string :name
20
+ t.string :description
21
+ t.datetime :created_at
22
+ t.datetime :updated_at
23
+ end
24
+ end
25
+ require File.dirname(__FILE__) + '/libs/book'
26
+
27
+
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: danielvlopes-smart_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Lopes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: danielvlopes@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/smart_csv.rb
33
+ - lib/smart_csv/array.rb
34
+ - lib/smart_csv/exportable.rb
35
+ - lib/smart_csv/render.rb
36
+ - rails/init.rb
37
+ - smart_csv.gemspec
38
+ - spec/factories.rb
39
+ - spec/libs/book.rb
40
+ - spec/smart_csv_spec.rb
41
+ - spec/spec_helper.rb
42
+ has_rdoc: false
43
+ homepage: http://github.com/danielvlopes/smart_csv
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Export your Rails models in CSV format
68
+ test_files:
69
+ - spec/factories.rb
70
+ - spec/libs/book.rb
71
+ - spec/smart_csv_spec.rb
72
+ - spec/spec_helper.rb