csvision 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csvision.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Enrique Vidal
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,69 @@
1
+ # CSVision
2
+
3
+ Convert a Hash into a CSV the easy way.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ `gem 'csvision'`
10
+
11
+ And then execute:
12
+
13
+ `bundle`
14
+
15
+ Or install it yourself as:
16
+
17
+ `gem install csvision`
18
+
19
+ ## Usage
20
+
21
+ CSVision has no prerequisites other than ruby itself, which is pretty cool just include the `CSVision` module into your app and you'll be good to go.
22
+
23
+ ### Hash
24
+
25
+ CSVision adds to `Hash` the `to_csv` method, anything that inherits from `Hash` will inherit this method as well, you do not need to open `Hash` and include `CSVision` in it, just add it anywhere in your code.
26
+
27
+ ```ruby
28
+ include CSVision
29
+ sample = { :name => 'foo', :last_name => 'bar', :age => 10 }
30
+ sample.to_csv #=> "\"last_name\",\"name\",\"age\"\n\"bar\",\"foo\",\"10\""
31
+ ```
32
+ ### Rails
33
+
34
+ CSVision adds the Rails the methods:
35
+
36
+ 1. `to_csv` at the model instance
37
+ 2. `to_csv` at the model class
38
+ 3. `add_csvision` at the model class
39
+
40
+ A rails model might look something like this:
41
+
42
+ ```ruby
43
+ class Product < ActiveRecord::Base
44
+ validates_presence_of :name, :permalink, :description
45
+ add_csvision :except => %w/updated_at created_at/
46
+ end
47
+ ```
48
+
49
+ CSVision lets you customize the way your CSV is formed, you can use any of the following options in the `add_csvision` method:
50
+
51
+ 1. `:only` to set only those parameters you wish to include.
52
+ 2. `:except` this does the opposite it excludes options from the list.
53
+ 3. `:delimeter` this set the field delimeter and it defaults to `"`
54
+ 4. `:separator` this set the field separator and it defults to `,`
55
+
56
+ ## TODO:
57
+
58
+ 1. Allow users to set their own list of parameters
59
+ 2. Add support for other ORM's
60
+ 3. Rails 3.x responder.
61
+
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake'
5
+ desc 'Default: run all tests.'
6
+ task :default => :test
7
+
8
+ require 'rake/testtask'
9
+ Rake::TestTask.new( :test ) do |test|
10
+ test.libs << 'test'
11
+ test.pattern = 'test/**/*_test.rb'
12
+ test.verbose = true
13
+ end
data/csvision.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+ require "csvision/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Enrique Vidal"]
7
+ gem.email = ["enrique@cloverinteractive.com"]
8
+ gem.description = %q{Gives Hash the ability to be turned into csv files}
9
+ gem.summary = %q{Adds support to hashes to be turned into csv}
10
+ gem.homepage = "http://cloverinteractive.github.com/csvision/"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "csvision"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = CSVision::VERSION
18
+
19
+ gem.add_development_dependency 'activerecord'
20
+ gem.add_development_dependency 'activesupport'
21
+ gem.add_development_dependency 'sqlite3'
22
+ gem.add_development_dependency 'mocha'
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'turn', '0.8.2'
25
+ end
@@ -0,0 +1,78 @@
1
+ module CSVision
2
+ def self.included(base)
3
+ base.extend CSVHelper
4
+ end
5
+
6
+ module CSVHelper
7
+ def add_csvision(options={})
8
+ return if included_modules.include? InstanceMethods
9
+ cattr_accessor :csv_except, :csv_only, :csv_delimeter, :csv_separator, :csv_headers
10
+
11
+ self.csv_only = options[:only]
12
+ self.csv_except = options[:except]
13
+ self.csv_delimeter = options[:delimeter] || '"'
14
+ self.csv_separator = options[:separator] || ','
15
+
16
+ include InstanceMethods
17
+ extend ClassMethods
18
+ end
19
+
20
+ module ClassMethods
21
+ def to_csv(options={})
22
+ options[:headers] ||= true
23
+ headers, csv_array = nil, []
24
+
25
+ self.find_each(:batch_size => options[:batch_size]) do |object|
26
+ headers ||= object.csvize( object.attributes.only( *csv_only ).keys, options ) if csv_only && !csv_only.empty?
27
+ headers ||= object.csvize( object.attributes.except( *csv_except ).keys, options ) if csv_except && !csv_except.empty?
28
+
29
+ csv_array << object.to_csv( options.merge(:headers => false) )
30
+ end
31
+ content = csv_array.join("\n")
32
+
33
+ if options[:headers]
34
+ headers + "\n" + content
35
+ else
36
+ content
37
+ end
38
+ end
39
+ end
40
+
41
+ module InstanceMethods
42
+ def to_csv(options={})
43
+ options[:delimeter] ||= '"'
44
+ options[:separator] ||= ','
45
+ options[:headers] = true if options[:headers].nil?
46
+ headers, values, rows = [], [], nil
47
+ @csv = ''
48
+
49
+ if self.respond_to?( :attributes ) && self.attributes.kind_of?( Hash )
50
+ rows = self.attributes.only( *csv_only ).to_a if csv_only && !csv_only.empty?
51
+ rows ||= self.attributes.except( *csv_except ).to_a if csv_except && !csv_except.empty?
52
+ rows ||= self.attributes.to_a
53
+ end
54
+
55
+ ( rows || self.to_a ).each do |(key, value)|
56
+ headers << key if options[:headers]
57
+ values << value
58
+ end
59
+
60
+ @csv = csvize(headers, options) + "\n" if options[:headers]
61
+ @csv += csvize(values, options)
62
+ end
63
+
64
+ def csvize(values, options)
65
+ delimeter = options[:delimeter] || csv_delimeter
66
+ separator = options[:separator] || csv_separator
67
+
68
+ values.map do |value|
69
+ delimeter + value.to_s + delimeter
70
+ end.join(separator)
71
+ end
72
+
73
+ def cached_csv
74
+ @csv
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module CSVision
2
+ VERSION = "0.1.0"
3
+ end
data/lib/csvision.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "csvision/version"
2
+ require "csvision/csv_helper"
3
+
4
+ module CSVision
5
+ ::ActiveRecord::Base.send( :extend, CSVHelper ) if defined? ::ActiveRecord::Base
6
+ ::Hash.send( :include, CSVHelper::InstanceMethods )
7
+ end
@@ -0,0 +1,4 @@
1
+ class Product < ActiveRecord::Base
2
+ validates_presence_of :name, :permalink, :description
3
+ add_csvision :except => %w/updated_at created_at/
4
+ end
@@ -0,0 +1,4 @@
1
+ test:
2
+ sqlite:
3
+ adapter: sqlite3
4
+ database: ":memory:"
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class CSVisionTest < ActiveSupport::TestCase
4
+ def setup
5
+ assert @hash_demo = ActiveSupport::OrderedHash.new
6
+ assert @hash_demo[:a] = 'a'
7
+ assert @hash_demo[:b] = 'b'
8
+ assert @hash_demo[:c] = 'c'
9
+ end
10
+
11
+ test "check plugin type" do
12
+ assert_kind_of Module, CSVision
13
+ end
14
+
15
+ test "hashes are successfully turned into csv" do
16
+ assert_match /[abc]\'\,/, @hash_demo.to_csv(:delimeter => "'")
17
+ end
18
+
19
+ test "can call get a csv cached after turned into csv once" do
20
+ assert_nil @hash_demo.cached_csv
21
+ assert_match /[abc]\'/, @hash_demo.to_csv(:delimeter => "'")
22
+
23
+ assert @hash_demo[:d] = 'd'
24
+ assert_not_equal @hash_demo.cached_csv, @hash_demo.to_csv
25
+ end
26
+
27
+ test "can create csv without headers" do
28
+ assert_match /[abc]\"\,/, @hash_demo.to_csv(:headers => false)
29
+ end
30
+
31
+ test "can set different separator and spacer" do
32
+ assert_match /[abc]\|/, @hash_demo.to_csv(:delimeter => '', :separator => '|')
33
+ end
34
+ end
data/test/db/schema.rb ADDED
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table "products", :force => true do |t|
3
+ t.string "name", :null => false
4
+ t.text "description", :null => false
5
+ t.string "permalink", :null => false
6
+ t.boolean "published", :default => false, :null => false
7
+ t.decimal "price", :precision => 8, :scale => 2, :default => 0.0, :null => false
8
+ t.datetime "created_at"
9
+ t.datetime "updated_at"
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class ProductTest < ActiveSupport::TestCase
4
+ def setup
5
+ 5.times { |i| assert Product.create!( :name => "name_#{i}", :permalink => "permalink_#{i}", :description => "description_#{i}") }
6
+ end
7
+
8
+ test "ActiveRecord::Base objects should respond to add_csvision" do
9
+ assert Product.respond_to? :add_csvision
10
+ end
11
+
12
+ test "ActiveRecord::Base classes should respond to to_csv" do
13
+ assert Product.respond_to? :to_csv
14
+ end
15
+
16
+ test "ActiveRecord object should respond to to_csv" do
17
+ assert product = Product.new
18
+ assert product.respond_to? :to_csv
19
+ end
20
+
21
+ test "ActiveRecord objects can set either csv_only or csv_except" do
22
+ # since csv_except is set to %w/creted_at updated_at/ csv should not contain these
23
+ assert product = Product.new
24
+ assert headers = product.to_csv.split("\n").first
25
+ assert ! headers.include?( 'created_at' )
26
+ assert ! headers.include?( 'updated_at' )
27
+ end
28
+
29
+ test "Can create a csv from a collection of products" do
30
+ assert products_csv = Product.to_csv
31
+ assert products_csv
32
+ assert_match /[a-z_]+\"\,/, products_csv
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'csvision'
4
+ require 'test/unit'
5
+ require 'active_record'
6
+ require 'active_support'
7
+ require 'active_support/dependencies'
8
+ require 'turn'
9
+
10
+ include CSVision
11
+
12
+ TEST_PATH = File.expand_path( File.join File.dirname( __FILE__ ) )
13
+ config = YAML::load( IO.read( File.join TEST_PATH, 'config', 'database.yml' ) )['test']['sqlite']
14
+
15
+ ActiveRecord::Base.establish_connection config
16
+ ActiveSupport::Dependencies.autoload_paths.unshift File.join( TEST_PATH, 'app', 'models' )
17
+
18
+ ActiveRecord::Base.silence do
19
+ ActiveRecord::Migration.verbose = false
20
+ load File.join( TEST_PATH, 'db','schema.rb' )
21
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csvision
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Enrique Vidal
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-10 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activesupport
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: sqlite3
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: mocha
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: rake
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :development
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: turn
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - "="
97
+ - !ruby/object:Gem::Version
98
+ hash: 59
99
+ segments:
100
+ - 0
101
+ - 8
102
+ - 2
103
+ version: 0.8.2
104
+ type: :development
105
+ version_requirements: *id006
106
+ description: Gives Hash the ability to be turned into csv files
107
+ email:
108
+ - enrique@cloverinteractive.com
109
+ executables: []
110
+
111
+ extensions: []
112
+
113
+ extra_rdoc_files: []
114
+
115
+ files:
116
+ - .gitignore
117
+ - Gemfile
118
+ - LICENSE
119
+ - README.md
120
+ - Rakefile
121
+ - csvision.gemspec
122
+ - lib/csvision.rb
123
+ - lib/csvision/csv_helper.rb
124
+ - lib/csvision/version.rb
125
+ - test/app/models/product.rb
126
+ - test/config/database.yml
127
+ - test/csvision_test.rb
128
+ - test/db/schema.rb
129
+ - test/product_test.rb
130
+ - test/test_helper.rb
131
+ homepage: http://cloverinteractive.github.com/csvision/
132
+ licenses: []
133
+
134
+ post_install_message:
135
+ rdoc_options: []
136
+
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ hash: 3
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ hash: 3
154
+ segments:
155
+ - 0
156
+ version: "0"
157
+ requirements: []
158
+
159
+ rubyforge_project:
160
+ rubygems_version: 1.8.15
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: Adds support to hashes to be turned into csv
164
+ test_files:
165
+ - test/app/models/product.rb
166
+ - test/config/database.yml
167
+ - test/csvision_test.rb
168
+ - test/db/schema.rb
169
+ - test/product_test.rb
170
+ - test/test_helper.rb