active_report 4.0.2 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -4
- data/active_report.gemspec +2 -2
- data/bin/console +1 -0
- data/bin/rake +2 -2
- data/lib/active_report.rb +3 -1
- data/lib/active_report/array.rb +20 -16
- data/lib/active_report/base.rb +51 -36
- data/lib/active_report/configuration.rb +27 -0
- data/lib/active_report/hash.rb +21 -21
- data/lib/active_report/record.rb +21 -16
- data/lib/active_report/version.rb +3 -1
- data/lib/generators/active_report/install_generator.rb +2 -0
- data/lib/generators/active_report/templates/install.rb +5 -3
- metadata +4 -32
- data/lib/active_report/settings.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 455d82fc5a50a79fdc57477851140306cb49b0ab
|
4
|
+
data.tar.gz: 7a9d01cb237fde5d1426f67e1afdff84da99820c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0260ae9c52fbbc1a87bbbdd574d5e5ac3f6ebf6af08c49efa56a38db2a284ff131fdd33456c9f46b8b8396303dd97ec5271a99e165a19cef29e820e10344c10c
|
7
|
+
data.tar.gz: c9ce8bfe558375696e48b2e05d24c1c47fe111b2f27adbd08de8814fa25b6e32caa1bd2d8e99d0c0caaf0f471e281dd17537c6f50730c2b01cdddb5bd83b34fe
|
data/README.md
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/active_report.svg)](http://badge.fury.io/rb/active_report)
|
4
4
|
[![Build Status](https://travis-ci.org/drexed/active_report.svg?branch=master)](https://travis-ci.org/drexed/active_report)
|
5
|
-
[![Coverage Status](https://coveralls.io/repos/github/drexed/active_report/badge.svg?branch=master)](https://coveralls.io/github/drexed/active_report?branch=master)
|
6
5
|
|
7
6
|
ActiveReport is a library to export CSV's out of arrays, hashes, and records and vice versa.
|
8
7
|
|
@@ -36,9 +35,9 @@ Or install it yourself as:
|
|
36
35
|
`../config/initalizers/active_report.rb`
|
37
36
|
|
38
37
|
```ruby
|
39
|
-
ActiveReport
|
40
|
-
config.
|
41
|
-
config.
|
38
|
+
ActiveReport.configure do |config|
|
39
|
+
config.csv_force_encoding = true
|
40
|
+
config.csv_options = { encoding: 'UTF-8' }
|
42
41
|
end
|
43
42
|
```
|
44
43
|
|
data/active_report.gemspec
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# coding: utf-8
|
2
4
|
|
3
5
|
lib = File.expand_path('../lib', __FILE__)
|
@@ -23,10 +25,8 @@ Gem::Specification.new do |spec|
|
|
23
25
|
spec.require_paths = %w[lib support]
|
24
26
|
|
25
27
|
spec.add_runtime_dependency 'activerecord'
|
26
|
-
spec.add_runtime_dependency 'dry-configurable'
|
27
28
|
|
28
29
|
spec.add_development_dependency 'bundler'
|
29
|
-
spec.add_development_dependency 'coveralls'
|
30
30
|
spec.add_development_dependency 'rake'
|
31
31
|
spec.add_development_dependency 'rspec'
|
32
32
|
spec.add_development_dependency 'sqlite3'
|
data/bin/console
CHANGED
data/bin/rake
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
5
6
|
# The application 'rake' is installed as part of a gem, and
|
6
7
|
# this file is here to facilitate running it.
|
7
|
-
#
|
8
8
|
|
9
9
|
require 'pathname'
|
10
10
|
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath)
|
data/lib/active_report.rb
CHANGED
data/lib/active_report/array.rb
CHANGED
@@ -1,40 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ActiveReport::Array < ActiveReport::Base
|
2
4
|
|
3
|
-
attr_accessor :
|
5
|
+
attr_accessor :data, :headers, :options
|
4
6
|
|
5
|
-
def initialize(
|
6
|
-
@
|
7
|
+
def initialize(data, headers: nil, options: {})
|
8
|
+
@data = data
|
7
9
|
@headers = headers
|
8
|
-
@options =
|
10
|
+
@options = csv_options.merge(options)
|
9
11
|
end
|
10
12
|
|
11
|
-
def self.export(
|
12
|
-
new(
|
13
|
+
def self.export(data, headers: nil, options: {})
|
14
|
+
klass = new(data, headers: headers, options: options)
|
15
|
+
klass.export
|
13
16
|
end
|
14
17
|
|
15
|
-
def self.import(
|
16
|
-
new(
|
18
|
+
def self.import(data, headers: nil, options: {})
|
19
|
+
klass = new(data, headers: headers, options: options)
|
20
|
+
klass.import
|
17
21
|
end
|
18
22
|
|
19
23
|
def export
|
20
|
-
@
|
24
|
+
@data = munge_first(@data)
|
21
25
|
|
22
26
|
CSV.generate(@options) do |csv|
|
23
27
|
csv << @headers unless @headers.nil?
|
24
|
-
@
|
28
|
+
@data.lazy.each { |cell| csv << cell }
|
25
29
|
end
|
26
30
|
end
|
27
31
|
|
28
32
|
def import
|
29
|
-
|
33
|
+
array = merge(@headers)
|
30
34
|
|
31
|
-
CSV.foreach(@
|
32
|
-
|
33
|
-
|
35
|
+
CSV.foreach(@data, @options) do |row|
|
36
|
+
row = encode_to_utf8(row) if csv_force_encoding?
|
37
|
+
array.push(row)
|
34
38
|
end
|
35
39
|
|
36
|
-
|
37
|
-
metatransform(
|
40
|
+
array = array.flatten if array.size < 2
|
41
|
+
metatransform(array)
|
38
42
|
end
|
39
43
|
|
40
44
|
end
|
data/lib/active_report/base.rb
CHANGED
@@ -1,7 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ActiveReport::Base
|
2
4
|
|
3
5
|
@@evaluate = false
|
4
6
|
|
7
|
+
def csv_options
|
8
|
+
ActiveReport.configuration.csv_options
|
9
|
+
end
|
10
|
+
|
11
|
+
def csv_force_encoding?
|
12
|
+
ActiveReport.configuration.csv_force_encoding
|
13
|
+
end
|
14
|
+
|
5
15
|
def self.evaluate(value = true)
|
6
16
|
@@evaluate = value
|
7
17
|
self
|
@@ -9,15 +19,12 @@ class ActiveReport::Base
|
|
9
19
|
|
10
20
|
private
|
11
21
|
|
12
|
-
def duplicate_options
|
13
|
-
ActiveReport::Settings.config.options.dup
|
14
|
-
end
|
15
|
-
|
16
22
|
# rubocop:disable Performance/StringReplacement
|
17
23
|
def encode_to_utf8(line)
|
18
24
|
line.map do |chr|
|
19
25
|
next if chr.nil?
|
20
|
-
|
26
|
+
|
27
|
+
chr = chr.tr('"', '')
|
21
28
|
chr.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
|
22
29
|
end
|
23
30
|
end
|
@@ -31,30 +38,19 @@ class ActiveReport::Base
|
|
31
38
|
|
32
39
|
def filter(object)
|
33
40
|
if @only.empty?
|
34
|
-
|
41
|
+
return if @except.empty?
|
42
|
+
object.delete_if { |key, _| @except.include?(key) }
|
35
43
|
else
|
36
44
|
object.keep_if { |key, _| @only.include?(key) }
|
37
45
|
end
|
38
46
|
end
|
39
47
|
|
40
|
-
def filter_first(object)
|
41
|
-
if @only.empty?
|
42
|
-
object.first.delete_if { |key, _| @except.include?(key) } unless @except.empty?
|
43
|
-
else
|
44
|
-
object.first.keep_if { |key, _| @only.include?(key) }
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def force_encoding?
|
49
|
-
ActiveReport::Settings.config.force_encoding
|
50
|
-
end
|
51
|
-
|
52
48
|
def humanize(object)
|
53
49
|
object.to_s.tr('_', ' ').capitalize
|
54
50
|
end
|
55
51
|
|
56
52
|
def merge(object)
|
57
|
-
[].
|
53
|
+
[object].compact
|
58
54
|
end
|
59
55
|
|
60
56
|
# rubocop:disable Security/Eval, Lint/RescueException
|
@@ -65,34 +61,53 @@ class ActiveReport::Base
|
|
65
61
|
end
|
66
62
|
# rubocop:enable Security/Eval, Lint/RescueException
|
67
63
|
|
64
|
+
def metaform_array(datum)
|
65
|
+
datum.map { |val| metaform(val) }
|
66
|
+
end
|
67
|
+
|
68
|
+
def metaform_hash(datum)
|
69
|
+
datum.lazy.each { |key, val| datum[key] = metaform(val) }
|
70
|
+
end
|
71
|
+
|
72
|
+
def metamorph_array(datum)
|
73
|
+
case datum.first.class.name
|
74
|
+
when 'Array' then datum.map { |arr| metaform_array(arr) }
|
75
|
+
when 'Hash' then datum.map { |hsh| metaform_hash(hsh) }
|
76
|
+
else metaform_array(datum)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
68
80
|
def metamorph(datum)
|
69
81
|
case datum.class.name
|
70
|
-
when 'Array'
|
71
|
-
|
72
|
-
|
73
|
-
elsif datum.first.is_a?(Hash)
|
74
|
-
datum.map { |hsh| hsh.each { |key, val| hsh.store(key, metaform(val)) } }
|
75
|
-
else
|
76
|
-
datum.map { |val| metaform(val) }
|
77
|
-
end
|
78
|
-
when 'Hash'
|
79
|
-
datum.each { |key, val| datum.store(key, metaform(val)) }
|
80
|
-
else
|
81
|
-
metaform(datum)
|
82
|
+
when 'Array' then metamorph_array(datum)
|
83
|
+
when 'Hash' then metaform_hash(datum)
|
84
|
+
else metaform(datum)
|
82
85
|
end
|
83
86
|
end
|
84
87
|
|
85
88
|
def metatransform(datum)
|
86
|
-
return
|
89
|
+
return if datum.empty?
|
87
90
|
evaluate? ? metamorph(datum) : datum
|
88
91
|
end
|
89
92
|
|
90
|
-
def munge(
|
91
|
-
|
93
|
+
def munge(datum)
|
94
|
+
datum.is_a?(Array) ? datum : merge(datum)
|
95
|
+
end
|
96
|
+
|
97
|
+
def munge_first(datum)
|
98
|
+
datum.first.is_a?(Array) ? datum : merge(datum)
|
99
|
+
end
|
100
|
+
|
101
|
+
def filter_values(datum)
|
102
|
+
array = []
|
103
|
+
(filter(datum) || datum).each_value { |val| array << val }
|
104
|
+
array
|
92
105
|
end
|
93
106
|
|
94
|
-
def
|
95
|
-
|
107
|
+
def filter_humanize_keys(datum)
|
108
|
+
array = []
|
109
|
+
(filter(datum.first) || datum.first).each_key { |key| array << humanize(key) }
|
110
|
+
array
|
96
111
|
end
|
97
112
|
|
98
113
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveReport
|
4
|
+
class Configuration
|
5
|
+
|
6
|
+
attr_accessor :csv_force_encoding, :csv_options
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@csv_force_encoding = true
|
10
|
+
@csv_options = { encoding: 'UTF-8' }
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configuration
|
16
|
+
@configuration ||= Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configuration=(config)
|
20
|
+
@configuration = config
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configure
|
24
|
+
yield(configuration)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/lib/active_report/hash.rb
CHANGED
@@ -1,57 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class ActiveReport::Hash < ActiveReport::Base
|
2
4
|
|
3
5
|
attr_accessor :datum, :only, :except, :headers, :options
|
4
6
|
|
5
7
|
def initialize(datum, only: nil, except: nil, headers: nil, options: {})
|
6
8
|
@datum = datum
|
7
|
-
@
|
9
|
+
@only = munge(only)
|
10
|
+
@except = munge(except)
|
8
11
|
@headers = headers
|
9
|
-
@
|
10
|
-
@options = duplicate_options.merge!(options)
|
12
|
+
@options = csv_options.merge(options)
|
11
13
|
end
|
12
14
|
|
13
15
|
def self.export(datum, only: nil, except: nil, headers: nil, options: {})
|
14
|
-
new(datum, only: only, except: except, headers: headers, options: options)
|
16
|
+
klass = new(datum, only: only, except: except, headers: headers, options: options)
|
17
|
+
klass.export
|
15
18
|
end
|
16
19
|
|
17
20
|
def self.import(datum, only: nil, except: nil, headers: nil, options: {})
|
18
|
-
new(datum, only: only, except: except, headers: headers, options: options)
|
21
|
+
klass = new(datum, only: only, except: except, headers: headers, options: options)
|
22
|
+
klass.import
|
19
23
|
end
|
20
24
|
|
21
25
|
def export
|
22
26
|
@datum = munge(@datum)
|
23
|
-
@only = munge(@only)
|
24
|
-
@except = munge(@except)
|
25
27
|
|
26
28
|
CSV.generate(@options) do |csv|
|
27
|
-
csv << (@headers || (
|
28
|
-
@datum.lazy.each { |data| csv << (
|
29
|
+
csv << (@headers || filter_humanize_keys(@datum))
|
30
|
+
@datum.lazy.each { |data| csv << filter_values(data) }
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
34
|
def import
|
33
|
-
|
34
|
-
|
35
|
+
array = []
|
36
|
+
line = 0
|
35
37
|
|
36
|
-
datum
|
37
|
-
|
38
|
-
data = encode_to_utf8(data) if force_encoding?
|
38
|
+
CSV.foreach(@datum, @options) do |data|
|
39
|
+
data = encode_to_utf8(data) if csv_force_encoding?
|
39
40
|
|
40
41
|
if @headers.nil? && line.zero?
|
41
42
|
@headers = data
|
42
43
|
else
|
43
44
|
subdata = {}
|
44
|
-
@headers.lazy.each_with_index
|
45
|
-
subdata.store(header.to_s, data[idx])
|
46
|
-
end
|
45
|
+
@headers.lazy.each_with_index { |header, idx| subdata[header.to_s] = data[idx] }
|
47
46
|
filter(subdata)
|
48
|
-
|
47
|
+
array.push(subdata)
|
49
48
|
end
|
49
|
+
|
50
|
+
line += 1
|
50
51
|
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
datum
|
53
|
+
array = array.first if array.size == 1
|
54
|
+
metatransform(array)
|
55
55
|
end
|
56
56
|
|
57
57
|
end
|
data/lib/active_report/record.rb
CHANGED
@@ -1,23 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'json'
|
4
|
+
|
2
5
|
class ActiveReport::Record < ActiveReport::Base
|
3
6
|
|
4
7
|
attr_accessor :datum, :model, :only, :except, :headers, :options
|
5
8
|
|
6
9
|
def initialize(datum, model: nil, only: nil, except: nil, headers: nil, options: {})
|
7
10
|
@datum = datum
|
8
|
-
@except = except
|
9
|
-
@headers = headers
|
10
11
|
@model = model
|
11
|
-
@only = only
|
12
|
-
@
|
12
|
+
@only = munge(only)
|
13
|
+
@except = munge(except)
|
14
|
+
@headers = headers
|
15
|
+
@options = csv_options.merge(options)
|
13
16
|
end
|
14
17
|
|
15
18
|
def self.export(datum, only: nil, except: nil, headers: nil, options: {})
|
16
|
-
new(datum, only: only, except: except, headers: headers, options: options)
|
19
|
+
klass = new(datum, only: only, except: except, headers: headers, options: options)
|
20
|
+
klass.export
|
17
21
|
end
|
18
22
|
|
19
23
|
def self.import(datum, only: nil, except: nil, headers: nil, options: {}, model: nil)
|
20
|
-
new(datum, only: only, except: except, headers: headers, options: options, model: model)
|
24
|
+
klass = new(datum, only: only, except: except, headers: headers, options: options, model: model)
|
25
|
+
klass.import
|
21
26
|
end
|
22
27
|
|
23
28
|
def export
|
@@ -27,30 +32,30 @@ class ActiveReport::Record < ActiveReport::Base
|
|
27
32
|
merge(@datum.attributes)
|
28
33
|
end
|
29
34
|
|
30
|
-
@only
|
31
|
-
@except
|
35
|
+
@only.map!(&:to_s)
|
36
|
+
@except.map!(&:to_s)
|
32
37
|
|
33
38
|
CSV.generate(@options) do |csv|
|
34
|
-
csv << (@headers || (
|
35
|
-
@datum.lazy.each { |data| csv << (
|
39
|
+
csv << (@headers || filter_humanize_keys(@datum))
|
40
|
+
@datum.lazy.each { |data| csv << filter_values(data) }
|
36
41
|
end
|
37
42
|
end
|
38
43
|
|
39
44
|
def import
|
40
45
|
if @model.nil? || (@model.superclass != ActiveRecord::Base)
|
41
|
-
raise ArgumentError,
|
46
|
+
raise ArgumentError,
|
47
|
+
'Model must be an ActiveRecord::Base object.'
|
42
48
|
end
|
43
49
|
|
44
50
|
@datum = ActiveReport::Hash.import(@datum, headers: @headers, options: @options)
|
45
51
|
@datum = munge(@datum)
|
46
|
-
@only = munge(@only)
|
47
|
-
@except = munge(@except)
|
48
52
|
|
49
53
|
@datum.lazy.each do |data|
|
50
54
|
params = {}
|
51
|
-
|
52
|
-
|
53
|
-
|
55
|
+
|
56
|
+
data.lazy.each do |key, value|
|
57
|
+
key = key.to_s.downcase.gsub(/ |-/, '_').to_sym
|
58
|
+
params[key] = value
|
54
59
|
end
|
55
60
|
|
56
61
|
filter(params)
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ActiveReport.configure do |config|
|
4
|
+
config.csv_force_encoding = true
|
5
|
+
config.csv_options = { encoding: 'UTF-8' }
|
4
6
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_report
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05
|
11
|
+
date: 2017-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: dry-configurable
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: bundler
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,20 +38,6 @@ dependencies:
|
|
52
38
|
- - ">="
|
53
39
|
- !ruby/object:Gem::Version
|
54
40
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: coveralls
|
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
41
|
- !ruby/object:Gem::Dependency
|
70
42
|
name: rake
|
71
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -205,9 +177,9 @@ files:
|
|
205
177
|
- lib/active_report.rb
|
206
178
|
- lib/active_report/array.rb
|
207
179
|
- lib/active_report/base.rb
|
180
|
+
- lib/active_report/configuration.rb
|
208
181
|
- lib/active_report/hash.rb
|
209
182
|
- lib/active_report/record.rb
|
210
|
-
- lib/active_report/settings.rb
|
211
183
|
- lib/active_report/version.rb
|
212
184
|
- lib/generators/active_report/install_generator.rb
|
213
185
|
- lib/generators/active_report/templates/install.rb
|
@@ -232,7 +204,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
232
204
|
version: '0'
|
233
205
|
requirements: []
|
234
206
|
rubyforge_project:
|
235
|
-
rubygems_version: 2.6.
|
207
|
+
rubygems_version: 2.6.13
|
236
208
|
signing_key:
|
237
209
|
specification_version: 4
|
238
210
|
summary: Gem for exporting/importing ruby objects to flat files vice versa.
|