csv_creator 0.9.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 50fa518f5b034a07d9faed2f2553312ca89e1474
4
+ data.tar.gz: 789b0250a03153444bbd47ca2c0e15b3f9e21f68
5
+ SHA512:
6
+ metadata.gz: bef1657c111cedc0c8f1a43ea81129a50f9c2d7a8f5ba6c18d9ce4c70196b7189b34f2562dfa0a5f30d602c3fbbe1a27df0991bb339998589937f5aae8d19e10
7
+ data.tar.gz: 6062236d4b0c8b626d2803d527072b247504ab7b1a3f9b2972a65e939baf8f5f341ce28dfcb6f10370ebbb550ca003c7e5efaac71109df74e84af2cdbc18829d
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
11
+ *.gem
12
+ projectFilesBackup
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csv_creator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Dorian Sawa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # CsvCreator
2
+
3
+ Description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'csv_creator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install csv_creator
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/csv_creator.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
38
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "csv_creator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csv_creator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csv_creator"
8
+ spec.version = CsvCreator::VERSION
9
+ spec.authors = ["Dorian Sawa"]
10
+ spec.email = ["dorian.sawa@gmail.com"]
11
+
12
+ spec.summary = %q{This gem can help you with generating CSV file from collections of objects.}
13
+ spec.description = %q{CsvCreator provides nice, configurable interface which will generate CSV file for you, from collection you want (database or array with non-db related objects).}
14
+ spec.homepage = "https://github.com/dsawa/csv_creator"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|.idea)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "activesupport", "~> 4.0", ">= 4.0"
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.10"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest", "~> 5.7"
35
+ spec.add_development_dependency "pry", "~> 0.10.1"
36
+ end
@@ -0,0 +1,71 @@
1
+ module CsvCreator
2
+ class Creator
3
+ attr_reader :collection
4
+
5
+ def initialize(collection, options)
6
+ if options[:order].empty?
7
+ @collection = collection
8
+ else
9
+ @collection = sort_collection(collection, options[:order], options[:order_direction])
10
+ end
11
+ end
12
+
13
+ def generate_csv(options)
14
+ CSV.generate(options[:csv]) do |csv|
15
+ csv << csv_headers(options)
16
+ csv_data(options).each { |data| csv << data }
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def sort_collection(collection, fields, direction)
23
+ coll = collection.sort_by { |el| fields.map { |field| el.send(field) } }
24
+ direction.to_sym == :desc ? coll.reverse : coll
25
+ end
26
+
27
+ def csv_headers(options)
28
+ set_headers(options)
29
+
30
+ if options[:translate].empty?
31
+ @headers
32
+ else
33
+ field_to_translate = options[:translate].keys
34
+ @headers.map do |header|
35
+ field_to_translate.include?(header) ? options[:translate][header] : header
36
+ end
37
+ end
38
+ end
39
+
40
+ def set_headers(options)
41
+ headers = options[:only] ? options[:only].map { |field| field.to_sym } : get_attribute_names
42
+ headers -= options[:without].map { |field| field.to_sym } if options[:without]
43
+
44
+ @headers = headers
45
+ end
46
+
47
+ def get_attribute_names
48
+ element = collection.first
49
+
50
+ if element && element.respond_to?(:attribute_names)
51
+ element.attribute_names.map { |attr| attr.to_sym }
52
+ elsif element
53
+ element.instance_variables.map { |v| v.to_s.gsub('@', '').to_sym }
54
+ else
55
+ []
56
+ end
57
+ end
58
+
59
+ def csv_data(options)
60
+ collection.map do |element|
61
+ @headers.map do |header|
62
+ if options[:callbacks] && options[:callbacks][header]
63
+ options[:callbacks][header].yield element
64
+ else
65
+ element.send(header)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module CsvCreator
2
+ VERSION = '0.9.1'
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'csv_creator/version'
2
+ require 'csv_creator/creator'
3
+ require 'csv'
4
+ require 'active_support/core_ext/hash'
5
+ require 'pry'
6
+
7
+ module CsvCreator
8
+ def self.create(collection, options = {})
9
+ options_with_defaults = options.reverse_merge({
10
+ csv: { force_quotes: true, headers: true },
11
+ only: nil,
12
+ without: nil,
13
+ order: [],
14
+ order_direction: :asc,
15
+ translate: {},
16
+ callbacks: {}
17
+ })
18
+
19
+ Creator.new(collection, options_with_defaults).generate_csv(options_with_defaults)
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_creator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - Dorian Sawa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: '4.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.10'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.10'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.7'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.7'
75
+ - !ruby/object:Gem::Dependency
76
+ name: pry
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.10.1
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.10.1
89
+ description: CsvCreator provides nice, configurable interface which will generate
90
+ CSV file for you, from collection you want (database or array with non-db related
91
+ objects).
92
+ email:
93
+ - dorian.sawa@gmail.com
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - ".gitignore"
99
+ - ".travis.yml"
100
+ - Gemfile
101
+ - LICENSE.txt
102
+ - README.md
103
+ - Rakefile
104
+ - bin/console
105
+ - bin/setup
106
+ - csv_creator.gemspec
107
+ - lib/csv_creator.rb
108
+ - lib/csv_creator/creator.rb
109
+ - lib/csv_creator/version.rb
110
+ homepage: https://github.com/dsawa/csv_creator
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ allowed_push_host: https://rubygems.org
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.8
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: This gem can help you with generating CSV file from collections of objects.
135
+ test_files: []