ika 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +58 -1
- data/lib/ika/version.rb +1 -1
- data/lib/ika.rb +60 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d39bb3c1e707c8fc018731b5246d46661b3c747f
|
4
|
+
data.tar.gz: b7862d008f1095995e4c35c463d439685be3d80e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e64d100096aa7163ec6f56820ec3858f504b3eb9bd5d3a6a2b176c4711bdb4ea517b4b357b288007a8b20b65ada7aff34fb3dab06707a1a726c5823d8036852
|
7
|
+
data.tar.gz: 2d36246b1f9333fdc4a9fa57b7a881cf6b9e28ee3a8d8c7966d854997fc99fe732526bada4af56031d20e20da8b65fb73e18b9ff1389326ebea314ae94ed6c0e
|
data/README.md
CHANGED
@@ -1,3 +1,60 @@
|
|
1
|
-
|
1
|
+
# Ika
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/ika.svg)](http://badge.fury.io/rb/ika)
|
3
|
+
|
4
|
+
Ika implements the function that export/import ActiveModel data with json.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
In Rails, add it to your Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'ika'
|
12
|
+
```
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
In case: `Group` has many tags and `User` belongs to multiple groups with `GroupUsers` such as below.
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
has_many :group_users
|
21
|
+
has_many :groups, through: :group_users
|
22
|
+
end
|
23
|
+
|
24
|
+
class GroupUsers < ActiveRecord::Base
|
25
|
+
belongs_to :user
|
26
|
+
belongs_to :group
|
27
|
+
end
|
28
|
+
|
29
|
+
class Group < ActiveRecord::Base
|
30
|
+
has_many :group_users
|
31
|
+
has_many :users, through: :group_users
|
32
|
+
has_many :tags
|
33
|
+
end
|
34
|
+
|
35
|
+
class Tag < ActiveRecord::Base
|
36
|
+
belongs_to :group
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Now you can export with `export` method on your model or relation.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require 'json'
|
44
|
+
# with no options
|
45
|
+
|
46
|
+
JSON.parse User.export
|
47
|
+
# => [{"id":1,"name":"iruca3"},{"id":2,"name":"inkling"}]
|
48
|
+
JSON.parse User.where(id: 1).export
|
49
|
+
# => [{"id":1,"name":"iruca3"}]
|
50
|
+
JSON.parse User.find(id: 2).export
|
51
|
+
# => {"id":2,"name":"inkling"}
|
52
|
+
|
53
|
+
# with include option
|
54
|
+
JSON.parse User.export(include: :groups)
|
55
|
+
# => [{"id":1,"name":"iruca3","groups":[{"id":1,"name":"aqutras"},{"id":2,"name":"Splatoon"}]},{"id":2,"name":"inkling","groups":[{"id":2,"name":"Splatoon"}]}]
|
56
|
+
JSON.parse User.find(id: 1).export(include: [{groups: [:tags]}])
|
57
|
+
# => {"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"}]}]}
|
58
|
+
```
|
2
59
|
|
3
60
|
This project rocks and uses MIT-LICENSE.
|
data/lib/ika/version.rb
CHANGED
data/lib/ika.rb
CHANGED
@@ -1,14 +1,70 @@
|
|
1
1
|
module Ika
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
class ::Hash
|
5
|
+
def max_depth
|
6
|
+
max_depth = 1
|
7
|
+
depth_func = ->(hsh, cur_depth) do
|
8
|
+
max_depth = cur_depth if cur_depth > max_depth
|
9
|
+
hsh["children"].to_a.each{|h| depth_func.call(h, cur_depth+1)}
|
10
|
+
max_depth
|
11
|
+
end
|
12
|
+
depth_func.call(self, 0)
|
13
|
+
end
|
6
14
|
end
|
7
15
|
|
8
16
|
module ClassMethods
|
9
|
-
def export(options =
|
10
|
-
|
17
|
+
def export(options = {}, objects = nil)
|
18
|
+
all_symbol = true
|
19
|
+
options[:include] ||= []
|
20
|
+
options[:include] = [options[:include]] unless options[:include].is_a?(Array)
|
21
|
+
objects = self.includes(options[:include]) unless objects
|
22
|
+
options[:include].each do |opt|
|
23
|
+
all_symbol = false unless opt.is_a?(Symbol)
|
24
|
+
end
|
25
|
+
|
26
|
+
return objects.to_json(include: options[:include]) if all_symbol
|
27
|
+
|
28
|
+
whole_obj_arr = []
|
29
|
+
objects.each do |object|
|
30
|
+
obj_arr = {}
|
31
|
+
options[:include].each do |relation|
|
32
|
+
if relation.is_a?(::Hash)
|
33
|
+
relation.keys.each do |property|
|
34
|
+
obj_arr[property] = JSON.parse(object.try(property).export({include: relation[property]}, object.try(property)))
|
35
|
+
end
|
36
|
+
elsif relation.is_a?(Symbol)
|
37
|
+
obj_arr[relation] = JSON.parse(object.try(:relation).to_json(include: relation))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
whole_obj_arr.push(obj_arr)
|
41
|
+
end
|
42
|
+
JSON.generate(whole_obj_arr)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def export(options = {}, object = nil)
|
47
|
+
objects ||= self
|
48
|
+
all_symbol = true
|
49
|
+
options[:include] ||= []
|
50
|
+
options[:include] = [options[:include]] unless options[:include].is_a?(Array)
|
51
|
+
options[:include].each do |opt|
|
52
|
+
all_symbol = false unless opt.is_a?(Symbol)
|
53
|
+
end
|
54
|
+
|
55
|
+
return objects.to_json(include: options[:include]) if all_symbol
|
56
|
+
|
57
|
+
obj_hash = JSON.parse objects.to_json
|
58
|
+
options[:include].each do |relation|
|
59
|
+
if relation.is_a?(::Hash)
|
60
|
+
relation.keys.each do |property|
|
61
|
+
obj_hash[property] = JSON.parse(objects.try(property).includes(relation[property]).export({include: relation[property]}, objects.try(property)))
|
62
|
+
end
|
63
|
+
elsif relation.is_a?(Symbol)
|
64
|
+
obj_hash[relation] = JSON.parse(objects.try(relation).to_json)
|
65
|
+
end
|
11
66
|
end
|
67
|
+
JSON.generate(obj_hash)
|
12
68
|
end
|
13
69
|
end
|
14
70
|
|
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: 0.0.6
|
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-06-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|