active_export 0.3.0 → 0.4.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/README.md +102 -53
- data/active_export.gemspec +2 -2
- data/lib/active_export.rb +1 -1
- data/lib/active_export/version.rb +1 -1
- data/lib/generators/active_export/install_generator.rb +17 -0
- data/lib/generators/active_export/templates/config.rb +26 -0
- data/lib/generators/active_export/templates/default.yml +21 -0
- metadata +7 -4
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
# ActiveExport
|
1
|
+
# ActiveExport
|
2
2
|
|
3
|
-
|
3
|
+
[](http://travis-ci.org/kengos/active_export)
|
4
|
+
[](https://codeclimate.com/github/kengos/active_export)
|
4
5
|
|
5
|
-
|
6
|
+
ActiveExport generate CSV/XML/YAML String or CSV/XML/YAML file.
|
7
|
+
|
8
|
+
You can write the logic of generating csv or xml, yaml to a YAML file.
|
6
9
|
|
7
10
|
Another Support:
|
8
11
|
|
@@ -10,14 +13,39 @@ Another Support:
|
|
10
13
|
* when the value of csv data is null or blank or true or false, change another label<br>
|
11
14
|
ex) nil to '', blank to 'empty', true to 'Yes', false to 'No'<br>
|
12
15
|
|
13
|
-
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
gem 'active_export'
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install active_export
|
29
|
+
|
30
|
+
Generate the config initializer file and `default.yml`
|
31
|
+
|
32
|
+
$ rails g active_export:install
|
33
|
+
|
34
|
+
## Example
|
14
35
|
|
15
36
|
````ruby
|
16
37
|
ActiveExport::Csv.export(Book.scoped, source_name, namespace)
|
38
|
+
# => csv string
|
39
|
+
# "Title","AuthorName","Price(in tax)","Published"
|
40
|
+
# "Ruby","Bob","28","2012-08-01"
|
41
|
+
# "Rails","Alice","18","2012-07-01"
|
17
42
|
````
|
18
43
|
|
19
|
-
|
20
|
-
|
44
|
+
Price(in tax) `28` is `(book.price * 1.095).ceil.to_i` result.
|
45
|
+
|
46
|
+
YAML file:
|
47
|
+
|
48
|
+
````
|
21
49
|
namespace:
|
22
50
|
label_prefix: 'book'
|
23
51
|
methods:
|
@@ -25,7 +53,7 @@ namespace:
|
|
25
53
|
- author.name
|
26
54
|
- price: '(price * 1.095).ceil.to_i'
|
27
55
|
- created_at.strftime('%Y-%m-%d')
|
28
|
-
|
56
|
+
````
|
29
57
|
|
30
58
|
Write the same way without the ActiveExport:
|
31
59
|
|
@@ -44,76 +72,76 @@ CSV.generate do |csv|
|
|
44
72
|
end
|
45
73
|
````
|
46
74
|
|
47
|
-
##
|
75
|
+
## Usage
|
48
76
|
|
49
|
-
|
77
|
+
```ruby
|
78
|
+
# Exporting Csv String
|
79
|
+
ActiveExport::Csv.export(Book.scoped, source_name, namespace)
|
50
80
|
|
51
|
-
|
81
|
+
# Exporting Csv File
|
82
|
+
ActiveExport::Csv.export_file(Book.scoped, source_name, namespace, filename)
|
52
83
|
|
53
|
-
|
84
|
+
# Exporting Xml String
|
85
|
+
ActiveExport::Xml.export(Book.scoped, source_name, namespace)
|
54
86
|
|
55
|
-
|
87
|
+
# Exporting Xml File
|
88
|
+
ActiveExport::Xml.export_file(Book.scoped, source_name, namespace, filename)
|
56
89
|
|
57
|
-
|
90
|
+
# Exporting Yaml String
|
91
|
+
ActiveExport::Yaml.export(Book.scoped, source_name, namespace)
|
58
92
|
|
59
|
-
|
93
|
+
# Exporting Yaml File
|
94
|
+
ActiveExport::Yaml.export_file(Book.scoped, source_name, namespace, filename)
|
95
|
+
```
|
60
96
|
|
61
|
-
##
|
97
|
+
## ActiveExport::Csv
|
98
|
+
|
99
|
+
Support 2 methods:
|
100
|
+
|
101
|
+
* `export(data, source_name, namespace, options = {})` ... Generate Csv string
|
102
|
+
* `export_file(data, source_name, namespace, filename, options = {})` ... Generate Csv file
|
62
103
|
|
63
|
-
|
104
|
+
options:
|
64
105
|
|
65
|
-
|
106
|
+
* `:eval_methods` ... override export method from YAML file.
|
107
|
+
* `:label_keys` ... override csv header label from YAML file.
|
108
|
+
* `:label_prefix` ... override csv header label prefix from YAML file.
|
109
|
+
* `:csv_options` ... Csv generate options.
|
110
|
+
* `:header` ... false to not export Csv header labels.
|
66
111
|
|
67
|
-
|
112
|
+
features:
|
68
113
|
|
69
|
-
|
70
|
-
ActiveExportconfigure do |config|
|
71
|
-
config.sources = { default: Rails.root.join('config', 'active_export.yml') }
|
72
|
-
# config.default_csv_optoins = { col_sep: ',', row_sep: "\n", force_quotes: true }
|
73
|
-
# config.default_find_in_batches_options = {} # default
|
74
|
-
# config.default_value_label_scope = [:default_value_labels] # default
|
75
|
-
# config.always_reload = false # default
|
76
|
-
# config.no_source_raise_error = false # default
|
77
|
-
end
|
78
|
-
````
|
114
|
+
* Support encoding
|
79
115
|
|
80
|
-
|
116
|
+
## ActiveExport::Xml
|
81
117
|
|
82
|
-
|
118
|
+
Support 2 methods:
|
83
119
|
|
84
|
-
|
120
|
+
* `ActiveExport::Xml.export(Book.scoped, source_name, namespace)` ... Generate Xml string
|
121
|
+
* `ActiveExport::Xml.export_file(Book.scoped, source_name, namespace, filename)` ... Generate Xml file
|
85
122
|
|
86
|
-
|
87
|
-
namespace_1:
|
88
|
-
label_prefix: 'book'
|
89
|
-
methods:
|
90
|
-
- method
|
91
|
-
- method
|
92
|
-
...
|
93
|
-
namespace_2:
|
94
|
-
label_prefix: ...
|
95
|
-
...
|
96
|
-
</pre>
|
123
|
+
options:
|
97
124
|
|
98
|
-
|
125
|
+
* TODO
|
99
126
|
|
100
|
-
|
101
|
-
ActiveExport::Csv.export_file(Book.scoped, :default, :namespace_1, filename)
|
127
|
+
features:
|
102
128
|
|
103
|
-
|
129
|
+
* Support encoding
|
130
|
+
|
131
|
+
## ActiveExport::Yaml
|
104
132
|
|
105
133
|
Support 2 methods:
|
106
134
|
|
107
|
-
* `export(
|
108
|
-
* `export_file(
|
135
|
+
* `ActiveExport::Yaml.export(Book.scoped, source_name, namespace)` ... Generate Yaml string
|
136
|
+
* `ActiveExport::Yaml.export_file(Book.scoped, source_name, namespace, filename)` ... Generate Yaml file
|
109
137
|
|
110
138
|
options:
|
111
139
|
|
112
|
-
*
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
*
|
140
|
+
* TODO
|
141
|
+
|
142
|
+
features:
|
143
|
+
|
144
|
+
* exporting i18n labels
|
117
145
|
|
118
146
|
## YAML file format
|
119
147
|
|
@@ -123,8 +151,29 @@ namespace:
|
|
123
151
|
methods:
|
124
152
|
- method_name
|
125
153
|
- label_name: method_name
|
154
|
+
# If using Xml export
|
155
|
+
xml_format:
|
156
|
+
encoding: 'UTF-8'
|
157
|
+
header: |
|
158
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
159
|
+
<records>
|
160
|
+
footer: |
|
161
|
+
</records>
|
162
|
+
body: |
|
163
|
+
<record>
|
164
|
+
<name>%%label_name%%</name>
|
165
|
+
</record>
|
166
|
+
|
167
|
+
namespace_2:
|
168
|
+
label_prefix: ...
|
169
|
+
...
|
170
|
+
````
|
126
171
|
</pre>
|
127
172
|
|
173
|
+
### xml_format:
|
174
|
+
|
175
|
+
TODO
|
176
|
+
|
128
177
|
### I18n field priority
|
129
178
|
|
130
179
|
1. `active_export.#{source_name}.#{namespace}.(label_prefix_)#{key}`
|
data/active_export.gemspec
CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/active_export/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["kengos"]
|
6
6
|
gem.email = ["kengo@kengos.jp"]
|
7
|
-
gem.description = %q{Export to CSV from ActiveRecord collections.}
|
8
|
-
gem.summary = %q{Export to CSV from ActiveRecord collections.}
|
7
|
+
gem.description = %q{Export to CSV/YAML/XML from ActiveRecord collections. }
|
8
|
+
gem.summary = %q{Export to CSV/YAML/XML from ActiveRecord collections.}
|
9
9
|
gem.homepage = "https://github.com/kengos/active_export"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
data/lib/active_export.rb
CHANGED
@@ -11,7 +11,7 @@ module ActiveExport
|
|
11
11
|
|
12
12
|
class << self
|
13
13
|
# @example
|
14
|
-
#
|
14
|
+
# ActiveExport.configure do |config|
|
15
15
|
# # ActiveExport export configuration files.
|
16
16
|
# config.sources = { default: Rails.root.join('config', 'active_export.yml') }
|
17
17
|
#
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# @see http://asciicasts.com/episodes/218-making-generators-in-rails-3
|
4
|
+
module ActiveExport
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
desc "Copy ActiveExport default files"
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
def copy_initializers
|
11
|
+
copy_file 'config.rb', 'config/initializers/active_export.rb'
|
12
|
+
copy_file 'default.yml', 'config/active_export/default.yml'
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
ActiveExport.configure do |config|
|
4
|
+
# ActiveExport export configuration files.
|
5
|
+
config.sources = {
|
6
|
+
default: Rails.root.join('config', 'active_export', 'default.yml')
|
7
|
+
# xml: Rails.root.join('config', 'active_export', 'xml.yml')
|
8
|
+
}
|
9
|
+
|
10
|
+
# @see CSV.new options
|
11
|
+
#config.default_csv_optoins = { col_sep: ',', row_sep: "\n", force_quotes: true }
|
12
|
+
|
13
|
+
# @see http://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_in_batches
|
14
|
+
#default_find_in_batches_options = {} # default
|
15
|
+
|
16
|
+
# Value where the result is set if null or blank or true, false
|
17
|
+
# loading i18n label from active_export.default_value_labels[:nil or :blank or :true or :false]
|
18
|
+
#default_value_label_scope = [:default_value_labels]
|
19
|
+
|
20
|
+
# true ... ActiveExport no cached yml data. Every time load yml file.
|
21
|
+
# false ... ActiveExport cached yml data.
|
22
|
+
#config.always_reload = false # default
|
23
|
+
|
24
|
+
# true ... not found sources[:source_name] to raise error
|
25
|
+
#config.no_source_raise_error = false # default
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Using Xml export, you need write `xml_format:`
|
2
|
+
# Example:
|
3
|
+
# book_1:
|
4
|
+
# label_prefix: 'book'
|
5
|
+
# methods:
|
6
|
+
# - name
|
7
|
+
# - author.name
|
8
|
+
# - price: 'price * 1.095'
|
9
|
+
# xml_format:
|
10
|
+
# encoding: 'UTF-8'
|
11
|
+
# header: |
|
12
|
+
# <?xml version="1.0" encoding="UTF-8"?>
|
13
|
+
# <records>
|
14
|
+
# footer: |
|
15
|
+
# </records>
|
16
|
+
# body: |
|
17
|
+
# <book>
|
18
|
+
# <name>%%name%%</name>
|
19
|
+
# <author_name>%%author.name%%</author_name>
|
20
|
+
# <price>%%price%%</price>
|
21
|
+
# </book>
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_export
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 3.0.0
|
30
|
-
description: Export to CSV from ActiveRecord collections.
|
30
|
+
description: ! 'Export to CSV/YAML/XML from ActiveRecord collections. '
|
31
31
|
email:
|
32
32
|
- kengo@kengos.jp
|
33
33
|
executables: []
|
@@ -50,6 +50,9 @@ files:
|
|
50
50
|
- lib/active_export/version.rb
|
51
51
|
- lib/active_export/xml.rb
|
52
52
|
- lib/active_export/yaml.rb
|
53
|
+
- lib/generators/active_export/install_generator.rb
|
54
|
+
- lib/generators/active_export/templates/config.rb
|
55
|
+
- lib/generators/active_export/templates/default.yml
|
53
56
|
- spec/active_export/base_spec.rb
|
54
57
|
- spec/active_export/csv_spec.rb
|
55
58
|
- spec/active_export/rails_support_spec.rb
|
@@ -94,7 +97,7 @@ rubyforge_project:
|
|
94
97
|
rubygems_version: 1.8.24
|
95
98
|
signing_key:
|
96
99
|
specification_version: 3
|
97
|
-
summary: Export to CSV from ActiveRecord collections.
|
100
|
+
summary: Export to CSV/YAML/XML from ActiveRecord collections.
|
98
101
|
test_files:
|
99
102
|
- spec/active_export/base_spec.rb
|
100
103
|
- spec/active_export/csv_spec.rb
|