to_csv 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +24 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +34 -0
- data/LICENSE +20 -0
- data/README.rdoc +48 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/lib/to_csv.rb +32 -0
- data/lib/to_csv/interceptor.rb +30 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/to_csv_spec.rb +49 -0
- data/to_csv.gemspec +64 -0
- metadata +128 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (3.0.0.beta4)
|
5
|
+
columnize (0.3.1)
|
6
|
+
fastercsv (1.5.3)
|
7
|
+
gemcutter (0.6.1)
|
8
|
+
git (1.2.5)
|
9
|
+
jeweler (1.4.0)
|
10
|
+
gemcutter (>= 0.1.0)
|
11
|
+
git (>= 1.2.5)
|
12
|
+
rubyforge (>= 2.0.0)
|
13
|
+
json_pure (1.4.6)
|
14
|
+
linecache (0.43)
|
15
|
+
rake (0.8.7)
|
16
|
+
rspec (1.3.0)
|
17
|
+
ruby-debug (0.10.3)
|
18
|
+
columnize (>= 0.1)
|
19
|
+
ruby-debug-base (~> 0.10.3.0)
|
20
|
+
ruby-debug-base (0.10.3)
|
21
|
+
linecache (>= 0.3)
|
22
|
+
rubyforge (2.0.4)
|
23
|
+
json_pure (>= 1.1.7)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
activesupport (= 3.0.0.beta4)
|
30
|
+
fastercsv
|
31
|
+
jeweler
|
32
|
+
rake
|
33
|
+
rspec
|
34
|
+
ruby-debug
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Stefan Penner
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
= to_csv
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
class Email
|
6
|
+
include ToCsv
|
7
|
+
attr_accessor :name, :data, :some_boolean
|
8
|
+
|
9
|
+
csv do
|
10
|
+
name
|
11
|
+
data
|
12
|
+
some_boolean
|
13
|
+
end
|
14
|
+
|
15
|
+
csv :just_name do
|
16
|
+
name
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(name,data,some_boolean)
|
20
|
+
@name = name
|
21
|
+
@data = data
|
22
|
+
@some_boolean = some_boolean
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
@email = Email.new("Stefan Penner","asdf asdf", true)
|
28
|
+
|
29
|
+
@email.to_csv
|
30
|
+
> "Stefan Penner, asdf asdf, true\n"
|
31
|
+
|
32
|
+
@email.to_csv(:just_name)
|
33
|
+
> "Stefan Penner\n"
|
34
|
+
|
35
|
+
|
36
|
+
== Note on Patches/Pull Requests
|
37
|
+
|
38
|
+
* Fork the project.
|
39
|
+
* Make your feature addition or bug fix.
|
40
|
+
* Add tests for it. This is important so I don't break it in a
|
41
|
+
future version unintentionally.
|
42
|
+
* Commit, do not mess with rakefile, version, or history.
|
43
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
44
|
+
* Send me a pull request. Bonus points for topic branches.
|
45
|
+
|
46
|
+
== Copyright
|
47
|
+
|
48
|
+
Copyright (c) 2010 Stefan Penner. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -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 = "to_csv"
|
8
|
+
gem.summary = %Q{ yet another ruby to csv dsl}
|
9
|
+
gem.description = %Q{yet another ruby to csv dsl, still at the having fun stage}
|
10
|
+
gem.email = "stefan.penner@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/stefanpenner/to_csv"
|
12
|
+
gem.authors = ["Stefan Penner"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency('activesupport','>= 3.0.0.beta4')
|
15
|
+
gem.add_dependency('fastercsv')
|
16
|
+
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'spec/rake/spectask'
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
31
|
+
spec.libs << 'lib' << 'spec'
|
32
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
33
|
+
spec.rcov = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :spec => :check_dependencies
|
37
|
+
|
38
|
+
task :default => :spec
|
39
|
+
|
40
|
+
require 'rake/rdoctask'
|
41
|
+
Rake::RDocTask.new do |rdoc|
|
42
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "to_csv #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
data/lib/to_csv.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/class/inheritable_attributes'
|
3
|
+
require 'fastercsv' unless RUBY_VERSION >= "1.9"
|
4
|
+
require 'to_csv/interceptor'
|
5
|
+
|
6
|
+
module ToCsv
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
class_inheritable_accessor :csv_instructions
|
11
|
+
self.csv_instructions = Hash.new({})
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def csv(namespace=:default,&block)
|
16
|
+
csv_instructions[namespace] = block
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_csv(namespace = :default)
|
21
|
+
Interceptor.from(self).to_block(&csv_instructions[namespace]).with_result do |results,methods|
|
22
|
+
FasterCSV::generate_line(results)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_csv_header(namespace = :default)
|
27
|
+
Interceptor.from(self).to_block(&csv_instructions[namespace]).with_result do |results,methods|
|
28
|
+
FasterCSV::generate_line(methods)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ToCsv
|
2
|
+
class Interceptor
|
3
|
+
def initialize(object,block=nil)
|
4
|
+
@object = object
|
5
|
+
@block = block
|
6
|
+
@result = []
|
7
|
+
@method_audit = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from(object)
|
11
|
+
new(object)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_block(&block)
|
15
|
+
@block = block
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def with_result
|
20
|
+
yield instance_eval(&@block), @method_audit
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(sym,*args,&block)
|
24
|
+
@method_audit << sym
|
25
|
+
result = @object.send(sym,*args,&block)
|
26
|
+
@result << result
|
27
|
+
result
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler'
|
6
|
+
|
7
|
+
Bundler.setup
|
8
|
+
|
9
|
+
require 'to_csv'
|
10
|
+
require 'spec'
|
11
|
+
require 'spec/autorun'
|
12
|
+
|
13
|
+
Spec::Runner.configure do |config|
|
14
|
+
|
15
|
+
end
|
data/spec/to_csv_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class Email
|
4
|
+
include ToCsv
|
5
|
+
attr_accessor :name, :data, :some_boolean
|
6
|
+
|
7
|
+
csv do
|
8
|
+
name
|
9
|
+
data
|
10
|
+
some_boolean
|
11
|
+
end
|
12
|
+
|
13
|
+
csv :just_name do
|
14
|
+
name
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(name,data,some_boolean)
|
18
|
+
@name = name
|
19
|
+
@data = data
|
20
|
+
@some_boolean = some_boolean
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
describe ToCsv do
|
26
|
+
before(:each) do
|
27
|
+
@email = Email.new("Stefan Penner","asdf asdf", true)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "drops default CSV set to csv" do
|
31
|
+
@email.to_csv.should match("Stefan Penner,asdf asdf,true\n")
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "to_csv" do
|
35
|
+
it "without params should match with params of :default" do
|
36
|
+
@email.to_csv.should match(@email.to_csv(:default))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "to_csv_header" do
|
41
|
+
it "display the default methods called, in the call order" do
|
42
|
+
@email.to_csv_header.should match("name,data,some_boolean")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "drop just_name CSV set to csv" do
|
47
|
+
@email.to_csv(:just_name).should match("Stefan Penner")
|
48
|
+
end
|
49
|
+
end
|
data/to_csv.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{to_csv}
|
8
|
+
s.version = "0.2.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Stefan Penner"]
|
12
|
+
s.date = %q{2010-09-07}
|
13
|
+
s.description = %q{yet another ruby to csv dsl, still at the having fun stage}
|
14
|
+
s.email = %q{stefan.penner@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/to_csv.rb",
|
29
|
+
"lib/to_csv/interceptor.rb",
|
30
|
+
"spec/spec.opts",
|
31
|
+
"spec/spec_helper.rb",
|
32
|
+
"spec/to_csv_spec.rb",
|
33
|
+
"to_csv.gemspec"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/stefanpenner/to_csv}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{yet another ruby to csv dsl}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/spec_helper.rb",
|
42
|
+
"spec/to_csv_spec.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::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
|
52
|
+
s.add_runtime_dependency(%q<fastercsv>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
|
56
|
+
s.add_dependency(%q<fastercsv>, [">= 0"])
|
57
|
+
end
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
60
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
|
61
|
+
s.add_dependency(%q<fastercsv>, [">= 0"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Stefan Penner
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-07 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 13
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 2
|
31
|
+
- 9
|
32
|
+
version: 1.2.9
|
33
|
+
requirement: *id001
|
34
|
+
name: rspec
|
35
|
+
prerelease: false
|
36
|
+
type: :development
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: -1848230024
|
44
|
+
segments:
|
45
|
+
- 3
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
- beta4
|
49
|
+
version: 3.0.0.beta4
|
50
|
+
requirement: *id002
|
51
|
+
name: activesupport
|
52
|
+
prerelease: false
|
53
|
+
type: :runtime
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirement: *id003
|
65
|
+
name: fastercsv
|
66
|
+
prerelease: false
|
67
|
+
type: :runtime
|
68
|
+
description: yet another ruby to csv dsl, still at the having fun stage
|
69
|
+
email: stefan.penner@gmail.com
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files:
|
75
|
+
- LICENSE
|
76
|
+
- README.rdoc
|
77
|
+
files:
|
78
|
+
- .document
|
79
|
+
- .gitignore
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE
|
83
|
+
- README.rdoc
|
84
|
+
- Rakefile
|
85
|
+
- VERSION
|
86
|
+
- lib/to_csv.rb
|
87
|
+
- lib/to_csv/interceptor.rb
|
88
|
+
- spec/spec.opts
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/to_csv_spec.rb
|
91
|
+
- to_csv.gemspec
|
92
|
+
has_rdoc: true
|
93
|
+
homepage: http://github.com/stefanpenner/to_csv
|
94
|
+
licenses: []
|
95
|
+
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options:
|
98
|
+
- --charset=UTF-8
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
requirements: []
|
120
|
+
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.3.7
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: yet another ruby to csv dsl
|
126
|
+
test_files:
|
127
|
+
- spec/spec_helper.rb
|
128
|
+
- spec/to_csv_spec.rb
|