ika 0.0.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +18 -17
- data/README.md +30 -3
- data/lib/carrierwave/base64uploader.rb +31 -0
- data/lib/carrierwave/serialization.rb +30 -0
- data/lib/ika/version.rb +1 -1
- data/lib/ika.rb +65 -12
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9d601e456604b7ef58cdc9b299c8be7039e2859
|
4
|
+
data.tar.gz: 4083a46fa4be849c07adbf671946cc66beddb4ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33ce07999c380e9b01f8a01d94b33996e1dc15ddd63b528d32edb1c87a2d9b98fc6cb9689e7e901eceb6b92d29811a760aa4b0c1c768e249398b15276fa2baa9
|
7
|
+
data.tar.gz: 22f9e29e4f51c65fa5784e06e529b4a7a7c2931c82e1473592e91910aa1046c1874640a4026b9ac1d0f0ae5598a13f85620152a09f59a34cc9f014d7056839b7
|
data/MIT-LICENSE
CHANGED
@@ -1,20 +1,21 @@
|
|
1
|
-
|
1
|
+
The MIT License (MIT)
|
2
2
|
|
3
|
-
|
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:
|
3
|
+
Copyright (c) 2015 Aqutras
|
10
4
|
|
11
|
-
|
12
|
-
|
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:
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[![Circle CI](https://circleci.com/gh/Aqutras/ika.svg?style=shield)](https://circleci.com/gh/Aqutras/ika)
|
4
4
|
[![Coverage Status](https://coveralls.io/repos/Aqutras/ika/badge.svg?branch=master)](https://coveralls.io/r/Aqutras/ika?branch=master)
|
5
5
|
|
6
|
-
Ika implements the function that export/import ActiveModel data with json.
|
6
|
+
Ika implements the function that export/import ActiveModel data with json. Ika also supports [carrierwave](https://github.com/carrierwaveuploader/carrierwave).
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -15,6 +15,10 @@ gem 'ika'
|
|
15
15
|
|
16
16
|
## Usage
|
17
17
|
|
18
|
+
You can use `export` or `import` method on your model or relation.
|
19
|
+
|
20
|
+
### Example
|
21
|
+
|
18
22
|
In case: `Group` has many tags and `User` belongs to multiple groups with `GroupUsers` such as below.
|
19
23
|
|
20
24
|
```ruby
|
@@ -39,7 +43,7 @@ class Tag < ActiveRecord::Base
|
|
39
43
|
end
|
40
44
|
```
|
41
45
|
|
42
|
-
Now you can export with `export` method on your model or relation.
|
46
|
+
Now you can export with `export` method on your model or relation and import with `import` method on your model.
|
43
47
|
|
44
48
|
```ruby
|
45
49
|
require 'json'
|
@@ -55,8 +59,31 @@ JSON.parse User.find(id: 2).export
|
|
55
59
|
# with include option
|
56
60
|
JSON.parse User.export(include: :groups)
|
57
61
|
# => [{"id":1,"name":"iruca3","groups":[{"id":1,"name":"aqutras"},{"id":2,"name":"Splatoon"}]},{"id":2,"name":"inkling","groups":[{"id":2,"name":"Splatoon"}]}]
|
58
|
-
JSON.parse
|
62
|
+
data = JSON.parse(User.find(id: 1).export(include: [{groups: [:tags]}]))
|
59
63
|
# => {"id":1,"name":"iruca3","groups":[{"id":1,"name":"aqutras","tags":[{"id":1,"name":"Company"}]},{"id":2,"name":"Splatoon","tags":[{"id":2,"name":"Game"},{"id":3,"name":"Inkling"}]}]}
|
64
|
+
|
65
|
+
# import (id, created_at and updated_at are completely imported with the same value)
|
66
|
+
User.destroy_all
|
67
|
+
Group.destroy_all
|
68
|
+
Tag.destroy_all
|
69
|
+
User.import(data)
|
70
|
+
|
71
|
+
# sync mode is available.
|
72
|
+
User.import(User.where(id: 1).export, sync: true)
|
73
|
+
User.exist?(id: 2)
|
74
|
+
# => false
|
60
75
|
```
|
61
76
|
|
77
|
+
## Others
|
78
|
+
|
79
|
+
* If the same id exists, Ika uses `UPDATE`.
|
80
|
+
* Uploaded files by `carrierwave` will be checked their md5 hash and do nothing if they exist and md5 is matched.
|
81
|
+
|
82
|
+
## Not yet implemented features
|
83
|
+
|
84
|
+
* Sync mode (If class method `import` is called with sync mode, delete all data that are not contained exporting json)
|
85
|
+
|
86
|
+
## License
|
87
|
+
|
88
|
+
Copyright (c) 2015 Aqutras
|
62
89
|
This project rocks and uses MIT-LICENSE.
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Base64Uploader
|
3
|
+
# https://gist.github.com/hilotter/6a4c356499b55e8eaf9a/
|
4
|
+
def base64_conversion(uri_str, filename = 'base64')
|
5
|
+
image_data = split_base64(uri_str)
|
6
|
+
image_data_string = image_data[:data]
|
7
|
+
image_data_binary = Base64.decode64(image_data_string)
|
8
|
+
|
9
|
+
temp_img_file = Tempfile.new(filename)
|
10
|
+
temp_img_file.binmode
|
11
|
+
temp_img_file << image_data_binary
|
12
|
+
temp_img_file.rewind
|
13
|
+
|
14
|
+
img_params = {:filename => "#{filename}", :type => image_data[:type], :tempfile => temp_img_file}
|
15
|
+
ActionDispatch::Http::UploadedFile.new(img_params)
|
16
|
+
end
|
17
|
+
|
18
|
+
def split_base64(uri_str)
|
19
|
+
if uri_str.match(%r{data:(.*?);(.*?),(.*)$})
|
20
|
+
uri = Hash.new
|
21
|
+
uri[:type] = $1
|
22
|
+
uri[:encoder] = $2
|
23
|
+
uri[:data] = $3
|
24
|
+
uri[:extension] = $1.split('/')[1]
|
25
|
+
return uri
|
26
|
+
else
|
27
|
+
return nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Uploader
|
3
|
+
class Base
|
4
|
+
@@json_with_raw_data = false
|
5
|
+
def self.json_with_raw_data=(bool)
|
6
|
+
@@json_with_raw_data = bool
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.json_with_raw_data
|
10
|
+
@@json_with_raw_data
|
11
|
+
end
|
12
|
+
|
13
|
+
def serializable_hash(options = nil)
|
14
|
+
if @@json_with_raw_data
|
15
|
+
if url
|
16
|
+
mime = MIME::Types.type_for(file.file)[0].to_s
|
17
|
+
md5 = Digest::MD5.file(file.file).to_s
|
18
|
+
filename = Pathname.new(file.file).basename.to_s
|
19
|
+
base64 = 'data:' + mime + ';base64,' + Base64.strict_encode64(read)
|
20
|
+
{url: url, name: filename, data: base64, md5: md5}
|
21
|
+
else
|
22
|
+
{url: nil, name: nil, data: nil, md5: nil}
|
23
|
+
end
|
24
|
+
else
|
25
|
+
{"url" => url}.merge Hash[versions.map { |name, version| [name, { "url" => version.url }] }]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/ika/version.rb
CHANGED
data/lib/ika.rb
CHANGED
@@ -1,20 +1,62 @@
|
|
1
|
+
if Rails.env == 'test'
|
2
|
+
require 'carrierwave'
|
3
|
+
end
|
4
|
+
require 'carrierwave/serialization'
|
5
|
+
require 'carrierwave/base64uploader'
|
6
|
+
|
1
7
|
module Ika
|
2
8
|
extend ActiveSupport::Concern
|
3
9
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
module ClassMethods
|
11
|
+
include ::CarrierWave::Base64Uploader
|
12
|
+
|
13
|
+
def import(json_or_array, options = {})
|
14
|
+
if json_or_array.is_a?(Array)
|
15
|
+
objects = json_or_array
|
16
|
+
else
|
17
|
+
objects = JSON.parse(json_or_array)
|
18
|
+
objects = [objects] unless objects.is_a?(Array)
|
19
|
+
end
|
20
|
+
|
21
|
+
ActiveRecord::Base.transaction do
|
22
|
+
if options && options[:sync]
|
23
|
+
remove_target_ids = all.pluck(:id)
|
24
|
+
else
|
25
|
+
remove_target_ids = []
|
26
|
+
end
|
27
|
+
objects.each do |object|
|
28
|
+
record_exists = true if exists?(id: object['id'].to_i)
|
29
|
+
|
30
|
+
object_params = {}
|
31
|
+
object.keys.each do |key|
|
32
|
+
if object[key].is_a?(Array)
|
33
|
+
reflections[key].klass.import(object[key])
|
34
|
+
else
|
35
|
+
if new.try(key.to_sym).class.superclass == CarrierWave::Uploader::Base
|
36
|
+
need_update = true
|
37
|
+
if File.exist?('public' + object[key]['url'])
|
38
|
+
md5 = Digest::MD5.file('public' + object[key]['url'])
|
39
|
+
need_update = false if md5 == object[key]['md5'] && record_exists == true
|
40
|
+
end
|
41
|
+
object_params[key] = base64_conversion(object[key]['data'], object[key]['name']) if need_update
|
42
|
+
else
|
43
|
+
object_params[key] = object[key]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
if record_exists
|
48
|
+
where(id: object['id'].to_i).first.update(object_params)
|
49
|
+
else
|
50
|
+
create(object_params)
|
51
|
+
end
|
52
|
+
remove_target_ids -= [object['id'].to_i]
|
53
|
+
end
|
54
|
+
where(id: remove_target_ids).destroy_all
|
11
55
|
end
|
12
|
-
depth_func.call(self, 0)
|
13
56
|
end
|
14
|
-
end
|
15
57
|
|
16
|
-
module ClassMethods
|
17
58
|
def export(options = {}, objects = nil)
|
59
|
+
CarrierWave::Uploader::Base.json_with_raw_data = true
|
18
60
|
all_symbol = true
|
19
61
|
options[:include] ||= []
|
20
62
|
options[:include] = [options[:include]] unless options[:include].is_a?(Array)
|
@@ -23,7 +65,11 @@ module Ika
|
|
23
65
|
all_symbol = false unless opt.is_a?(Symbol)
|
24
66
|
end
|
25
67
|
|
26
|
-
|
68
|
+
if all_symbol
|
69
|
+
json = objects.to_json(include: options[:include])
|
70
|
+
CarrierWave::Uploader::Base.json_with_raw_data = false
|
71
|
+
return json
|
72
|
+
end
|
27
73
|
|
28
74
|
whole_obj_arr = []
|
29
75
|
objects.each do |object|
|
@@ -39,11 +85,13 @@ module Ika
|
|
39
85
|
end
|
40
86
|
whole_obj_arr.push(obj_arr)
|
41
87
|
end
|
88
|
+
CarrierWave::Uploader::Base.json_with_raw_data = false
|
42
89
|
JSON.generate(whole_obj_arr)
|
43
90
|
end
|
44
91
|
end
|
45
92
|
|
46
93
|
def export(options = {}, object = nil)
|
94
|
+
CarrierWave::Uploader::Base.json_with_raw_data = true
|
47
95
|
objects ||= self
|
48
96
|
all_symbol = true
|
49
97
|
options[:include] ||= []
|
@@ -52,7 +100,11 @@ module Ika
|
|
52
100
|
all_symbol = false unless opt.is_a?(Symbol)
|
53
101
|
end
|
54
102
|
|
55
|
-
|
103
|
+
if all_symbol
|
104
|
+
json = objects.to_json(include: options[:include])
|
105
|
+
CarrierWave::Uploader::Base.json_with_raw_data = false
|
106
|
+
return json
|
107
|
+
end
|
56
108
|
|
57
109
|
obj_hash = JSON.parse objects.to_json
|
58
110
|
options[:include].each do |relation|
|
@@ -64,6 +116,7 @@ module Ika
|
|
64
116
|
obj_hash[relation] = JSON.parse(objects.try(relation).to_json)
|
65
117
|
end
|
66
118
|
end
|
119
|
+
CarrierWave::Uploader::Base.json_with_raw_data = false
|
67
120
|
JSON.generate(obj_hash)
|
68
121
|
end
|
69
122
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ika
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Makoto NAKAYA
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-06
|
12
|
+
date: 2015-07-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -95,6 +95,34 @@ dependencies:
|
|
95
95
|
- - ">="
|
96
96
|
- !ruby/object:Gem::Version
|
97
97
|
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: carrierwave
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: pry-byebug
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
98
126
|
description: Implement Import/Export feature to ActiveRecord models.
|
99
127
|
email:
|
100
128
|
- nakaya@aqte.net
|
@@ -106,6 +134,8 @@ files:
|
|
106
134
|
- MIT-LICENSE
|
107
135
|
- README.md
|
108
136
|
- Rakefile
|
137
|
+
- lib/carrierwave/base64uploader.rb
|
138
|
+
- lib/carrierwave/serialization.rb
|
109
139
|
- lib/ika.rb
|
110
140
|
- lib/ika/version.rb
|
111
141
|
- lib/tasks/ika_tasks.rake
|