exporter 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b8edc0551e8c2615785995f42f8b534ad188c63
4
- data.tar.gz: aad26a14494806cdca3274e96e0dd93c9f2d26e2
3
+ metadata.gz: 884edf14fca8bc1375766805f2503837f25bed70
4
+ data.tar.gz: 5f16d07e1ea1aa2353f4d2ac087fd16645f3877d
5
5
  SHA512:
6
- metadata.gz: d443e04b11b5c3d96fddb7457282a48f86dc0bb628f1b6a42d0af0a0d24bebb4b0a4c2ec15b552603f9c9e1192a59e5db72daa9d2faed6a7277db2caed9d354a
7
- data.tar.gz: dde08124573c9c8dd5b281b5afd8cf17446c9c9c223ad7a8c50da8b7646284bdc5bb557626fa4d6c0e87087fac1042246b89809ab1ef2bff7958a69363ab21a4
6
+ metadata.gz: 0399f99d7aa32a2e83150961a3749923650fe6505ab3413c5e1de8d37625ae7f066dd36fc8c87d72f59752a39e270a22b543a8bd25b1bd0d494570673d10700c
7
+ data.tar.gz: 4368ff13565282a1122d4e3923a87b4e4e677ad108f1bdb0cb78a954ee24c00f191070138df9136eebd53143939b8eca762171314c216c8dd8e04207cead6b65
data/.rvmrc CHANGED
@@ -6,10 +6,10 @@
6
6
  # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
7
  # Only full ruby name is supported here, for short names use:
8
8
  # echo "rvm use 2.0.0" > .rvmrc
9
- environment_id="ruby-2.0.0-p247@gembuilder"
9
+ environment_id="ruby-2.0.0-p247@exporter"
10
10
 
11
11
  # Uncomment the following lines if you want to verify rvm version per project
12
- # rvmrc_rvm_version="1.22.3 (stable)" # 1.10.1 seems like a safe start
12
+ # rvmrc_rvm_version="1.22.10 (stable)" # 1.10.1 seems like a safe start
13
13
  # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
14
  # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
15
  # return 1
@@ -33,7 +33,7 @@ then
33
33
  if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
34
34
  then
35
35
  if [[ $- == *i* ]] # check for interactive shells
36
- then printf "%b" "Using: \E[32m$GEM_HOME\E[0m
36
+ then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
37
37
  " # show the user the ruby and gemset they are using in green
38
38
  else printf "%b" "Using: $GEM_HOME
39
39
  " # don't use colors in non-interactive shells
data/Gemfile CHANGED
@@ -2,8 +2,11 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in exporter.gemspec
4
4
  gemspec
5
- gem 'activerecord'
5
+
6
6
  group :test do
7
- gem 'rspec'
8
7
  gem 'coveralls', require: false
9
8
  end
9
+
10
+ group :development do
11
+ gem 'pry'
12
+ end
data/README.md CHANGED
@@ -4,9 +4,7 @@
4
4
  [![Code Climate](https://codeclimate.com/github/voanhduy1512/exporter.png)](https://codeclimate.com/github/voanhduy1512/exporter)
5
5
  [![Coverage Status](https://coveralls.io/repos/voanhduy1512/exporter/badge.png)](https://coveralls.io/r/voanhduy1512/exporter)
6
6
  [![Dependency Status](https://gemnasium.com/voanhduy1512/exporter.png)](https://gemnasium.com/voanhduy1512/exporter)
7
-
8
-
9
-
7
+ [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/voanhduy1512/exporter/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
10
8
 
11
9
  ## Installation
12
10
 
@@ -18,10 +16,17 @@ And then execute:
18
16
 
19
17
  $ bundle
20
18
 
21
- Or install it yourself as:
19
+ ## Usage
20
+ ``` ruby
21
+ # Here is the data which you want to export
22
+ users = User.all
23
+
24
+ # export it
25
+ Exporter.export(users, :csv).to_file(path_to_file)
26
+ ```
27
+
28
+ Only support export to csv now (more types are coming)
29
+
22
30
 
23
- $ gem install exporter
24
31
 
25
- ## Usage
26
32
 
27
- TODO: Write usage instructions here
@@ -18,7 +18,12 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'rake'
23
23
  spec.add_development_dependency 'sqlite3'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'factory_girl'
26
+
27
+ spec.add_dependency 'activerecord'
28
+ spec.add_dependency 'spreadsheet'
24
29
  end
@@ -1,9 +1,13 @@
1
+ require 'active_record'
2
+ require "csv"
1
3
  require "exporter/version"
2
4
  require "exporter/configuration"
3
5
  require "exporter/document"
4
6
  require "exporter/exporter"
5
7
  require "exporter/documents/csv_document"
8
+ require 'exporter/documents/excel_document'
6
9
  require "exporter/exporters/csv_exporter"
10
+ require "exporter/exporters/excel_exporter"
7
11
 
8
12
  module Exporter
9
13
  def self.configuration
@@ -11,7 +15,7 @@ module Exporter
11
15
  end
12
16
 
13
17
  def self.export(data, export_type, options={})
14
- exporter = @configuration.exporter(data.class, export_type)
18
+ exporter = @configuration.exporter(data, export_type)
15
19
  if exporter
16
20
  exporter.export(data, options)
17
21
  else
@@ -20,4 +24,8 @@ module Exporter
20
24
  end
21
25
  end
22
26
 
23
- Exporter.configuration.register(Array, :csv, Exporter::CsvExporter)
27
+ Exporter.configuration.register(Array, :csv, Exporter::CsvExporter)
28
+ Exporter.configuration.register(ActiveRecord::Relation, :csv, Exporter::CsvExporter)
29
+
30
+ Exporter.configuration.register(Array, :excel, Exporter::ExcelExporter)
31
+ Exporter.configuration.register(ActiveRecord::Relation, :excel, Exporter::ExcelExporter)
@@ -1,19 +1,34 @@
1
1
  module Exporter
2
2
  class Configuration
3
+
3
4
  def initialize
4
5
  @exporters = Hash.new
5
6
  end
6
7
 
7
8
  def register(data_type, export_type, exporter)
8
- @exporters[data_type] = {export_type => exporter}
9
+ if @exporters[data_type].nil?
10
+ @exporters[data_type] = {export_type => exporter}
11
+ else
12
+ @exporters[data_type][export_type] = exporter
13
+ end
14
+ end
15
+
16
+ def index(data, default_value, method)
17
+ @exporters.keys.each do |key|
18
+ return method.call(key) if data.kind_of?(key)
19
+ end
20
+ default_value
9
21
  end
10
22
 
11
- def can_export?(data_type, export_type)
12
- @exporters[data_type] && @exporters[data_type][export_type]
23
+ def can_export?(data, export_type)
24
+ can_export_proc = Proc.new{|key| @exporters[key][export_type].present?}
25
+ index(data, false, can_export_proc)
13
26
  end
14
27
 
15
- def exporter(data_type, export_type)
16
- can_export?(data_type, export_type) ? @exporters[data_type][export_type] : nil
28
+ def exporter(data, export_type)
29
+ exporter_proc = Proc.new {|key| @exporters[key][export_type]}
30
+ index(data, nil, exporter_proc)
17
31
  end
32
+
18
33
  end
19
- end
34
+ end
@@ -7,7 +7,7 @@ module Exporter
7
7
  end
8
8
 
9
9
  def to_file(path)
10
- File.open(path, 'w') {|f| f.write(@data) }
10
+ raise 'must override this method in subclass'
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,7 @@
1
+ module Exporter
2
+ class ExcelDocument < Document
3
+ def to_file(path)
4
+ @data.write path
5
+ end
6
+ end
7
+ end
@@ -1,14 +1,22 @@
1
1
  module Exporter
2
2
  class Exporter
3
3
  def self.export(data, options)
4
- document = process(data, options)
4
+ document = exporter.process(data, options)
5
5
  raise TypeError.new('process method must return an Document object') unless document.kind_of? Document
6
6
  document
7
7
  end
8
8
 
9
+ def self.exporter
10
+ @exporter ||= self.new
11
+ end
12
+
9
13
  protected
10
- def self.process(data, options)
14
+ def process(data, options)
11
15
  raise NotImplementedError.new("You must implement this process method.")
12
16
  end
17
+
18
+ def is_active_record?(data, options)
19
+ (data.kind_of? ActiveRecord::Relation) || (data.kind_of?(Array) && data[0].kind_of?(ActiveRecord::Base))
20
+ end
13
21
  end
14
22
  end
@@ -1,16 +1,11 @@
1
- require 'CSV'
2
1
  module Exporter
3
2
  class CsvExporter < Exporter
4
- def self.process(data, options)
5
3
 
6
- raise TypeError.new('data must be an array') unless data.class.eql? Array
7
- raise TypeError.new('Data must be an ActiveRecord') unless data[0].kind_of? ActiveRecord::Base
4
+ def process(data, options)
8
5
 
9
- if options[:columns]
10
- columns = options[:columns]
11
- else
12
- columns = data[0].class.attribute_names
13
- end
6
+ raise TypeError.new unless is_active_record?(data, options)
7
+
8
+ columns = options[:columns] || data[0].class.attribute_names
14
9
 
15
10
  data = CSV.generate do |csv|
16
11
  csv << columns
@@ -21,5 +16,6 @@ module Exporter
21
16
 
22
17
  CsvDocument.new(data)
23
18
  end
19
+
24
20
  end
25
- end
21
+ end
@@ -0,0 +1,24 @@
1
+ require 'spreadsheet'
2
+
3
+ module Exporter
4
+ class ExcelExporter < Exporter
5
+
6
+ def process(data, options)
7
+
8
+ raise TypeError.new unless is_active_record?(data, options)
9
+
10
+ columns = options[:columns] || data[0].class.attribute_names
11
+
12
+ book = Spreadsheet::Workbook.new
13
+ sheet = book.create_worksheet
14
+ sheet.name = options[:sheet_name] || 'sheet1'
15
+ sheet.row(0).concat columns
16
+ data.each_with_index do |row, index|
17
+ sheet.row(index+1).concat row.attributes.values_at(*columns)
18
+ end
19
+
20
+ ExcelDocument.new(book)
21
+ end
22
+
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Exporter
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ sequence(:name) { |n| "name#{n}" }
4
+ sequence(:email) {|e| "email#{e}"}
5
+ end
6
+ end
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
2
+
3
+ describe Exporter do
4
+
5
+ before(:all) {FactoryGirl.create(:user)}
6
+
7
+ let(:data){ User.all }
8
+
9
+ describe '.export' do
10
+ context 'ActiveRecord::Relation' do
11
+ it 'can export to csv' do
12
+ expect(Exporter.configuration.can_export?(data, :csv)).to eql(true)
13
+ end
14
+
15
+ it 'can export to excel' do
16
+ expect(Exporter.configuration.can_export?(data, :excel)).to eql(true)
17
+ end
18
+
19
+ it "can't export not register type" do
20
+ expect(Exporter.configuration.can_export?(data, :some_weird_type)).to eql(false)
21
+ end
22
+
23
+ context ':csv' do
24
+ it 'return an Exporter::Document subclass' do
25
+ document = Exporter.export(data, :csv)
26
+ expect(document).to be_kind_of(Exporter::Document)
27
+ end
28
+
29
+ it 'write to file correct' do
30
+ document = Exporter.export(data, :csv)
31
+ document.to_file('dummy.csv')
32
+ content = File.read('dummy.csv')
33
+ expect(content.length).to eql(document.data.length)
34
+ File.delete('dummy.csv')
35
+ end
36
+ end
37
+
38
+ context ':excel' do
39
+ it 'return an Exporter::Document subclass' do
40
+ document = Exporter.export(data, :excel)
41
+ expect(document).to be_kind_of(Exporter::Document)
42
+ end
43
+
44
+ it 'write to file correct' do
45
+ document = Exporter.export(data, :excel)
46
+ document.to_file('dummy.xls')
47
+ content = Spreadsheet.open('dummy.xls')
48
+ expect(content.worksheet(0).last_row_index).to eql(document.data.worksheet(0).last_row_index)
49
+ expect(content.worksheet(0).row(1)).to eql(document.data.worksheet(0).row(1))
50
+ File.delete('dummy.xls')
51
+ end
52
+ end
53
+ end
54
+
55
+ context 'Array of ActiveRecord::Base' do
56
+ it 'can export to csv' do
57
+ expect(Exporter.configuration.can_export?(data, :csv)).to eql(true)
58
+ end
59
+
60
+ it 'can export to excel' do
61
+ expect(Exporter.configuration.can_export?(data, :excel)).to eql(true)
62
+ end
63
+
64
+ it "can't export not register type" do
65
+ expect(Exporter.configuration.can_export?(data, :some_weird_type)).to eql(false)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -1,4 +1,2 @@
1
- module Exporter
2
- class User < ActiveRecord::Base
3
- end
4
- end
1
+ class User < ActiveRecord::Base
2
+ end
@@ -11,9 +11,9 @@ if ENV['TRAVIS']
11
11
  end
12
12
 
13
13
  require "exporter"
14
- require 'active_record'
14
+ require 'factory_girl'
15
15
  require 'models/user'
16
-
16
+ FactoryGirl.find_definitions
17
17
  RSpec.configure do |config|
18
18
  config.treat_symbols_as_metadata_keys_with_true_values = true
19
19
  config.run_all_when_everything_filtered = true
@@ -32,6 +32,7 @@ ActiveRecord::Base.establish_connection(
32
32
  :adapter => 'sqlite3',
33
33
  :database => ':memory:'
34
34
  )
35
+
35
36
  ActiveRecord::Schema.define do
36
37
  self.verbose = false
37
38
 
@@ -39,4 +40,4 @@ ActiveRecord::Schema.define do
39
40
  t.string :name
40
41
  t.string :email
41
42
  end
42
- end
43
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Võ Anh Duy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-29 00:00:00.000000000 Z
11
+ date: 2013-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,62 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: spreadsheet
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
55
111
  description: A simple gem for export data to other format
56
112
  email:
57
113
  - voanhduy1512@live.com
@@ -72,10 +128,13 @@ files:
72
128
  - lib/exporter/configuration.rb
73
129
  - lib/exporter/document.rb
74
130
  - lib/exporter/documents/csv_document.rb
131
+ - lib/exporter/documents/excel_document.rb
75
132
  - lib/exporter/exporter.rb
76
133
  - lib/exporter/exporters/csv_exporter.rb
134
+ - lib/exporter/exporters/excel_exporter.rb
77
135
  - lib/exporter/version.rb
78
- - spec/lib/exporter_spec.rb
136
+ - spec/factories/user.rb
137
+ - spec/features/exporter_spec.rb
79
138
  - spec/models/user.rb
80
139
  - spec/spec_helper.rb
81
140
  homepage: ''
@@ -98,11 +157,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
157
  version: '0'
99
158
  requirements: []
100
159
  rubyforge_project:
101
- rubygems_version: 2.0.6
160
+ rubygems_version: 2.1.8
102
161
  signing_key:
103
162
  specification_version: 4
104
163
  summary: A simple data exporter
105
164
  test_files:
106
- - spec/lib/exporter_spec.rb
165
+ - spec/factories/user.rb
166
+ - spec/features/exporter_spec.rb
107
167
  - spec/models/user.rb
108
168
  - spec/spec_helper.rb
@@ -1,18 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Exporter do
4
- let(:data){
5
- [
6
- Exporter::User.create(:name => 'aaa', :email => 'aaa@aaa.aaa'),
7
- Exporter::User.create(:name => 'bbb', :email => 'bbb@bbb.bbb')
8
- ]
9
- }
10
- describe '.export' do
11
- it 'return an Exporter::Document' do
12
- document = Exporter.export(data, :csv)
13
- puts document.kind_of? Exporter::Document
14
- puts document.class
15
- expect(document).to be_kind_of(Exporter::Document)
16
- end
17
- end
18
- end