normalizr_ruby 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/normalizr_ruby/converter.rb +29 -18
- data/lib/normalizr_ruby/schema.rb +12 -0
- data/lib/normalizr_ruby/version.rb +1 -1
- 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: 95de21e33d5f39694c35f6b4b7f2093522ecb1a2
|
4
|
+
data.tar.gz: c93846067ce9d15c17f7dca3bf869638cee991b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 429c8be4c5264a7462b42ebb95b63a5124d0317bdc9f5731b67736c7048cd417fd87d5864943c83e1ed893b2cf2a845c888f8d1ce12be123525c3b95e9df5f21
|
7
|
+
data.tar.gz: 65a392bdd66dcf4197ea9fa319edfbe6eb8db4a78faa5dcafddb296bd20f1eaeb6409633621164f169692600bf7f39dc958d0d0e8753154ed34fc269ec16a94d
|
@@ -2,17 +2,6 @@ module NormalizrRuby
|
|
2
2
|
class Converter
|
3
3
|
class SchemaNotFound < StandardError; end
|
4
4
|
|
5
|
-
def self.get_schema_class(resource)
|
6
|
-
resource_class = resource.class
|
7
|
-
resource_class = resource_class.base_class if resource_class.respond_to?(:base_class)
|
8
|
-
resource_class_name = resource_class.name
|
9
|
-
schema_class = "#{resource_class_name}Schema".safe_constantize
|
10
|
-
if schema_class.nil?
|
11
|
-
raise SchemaNotFound, "#{resource_class_name} is not found."
|
12
|
-
end
|
13
|
-
schema_class
|
14
|
-
end
|
15
|
-
|
16
5
|
def initialize(context)
|
17
6
|
@context = context
|
18
7
|
@entities = {}
|
@@ -29,20 +18,42 @@ module NormalizrRuby
|
|
29
18
|
resource
|
30
19
|
end
|
31
20
|
|
21
|
+
private
|
22
|
+
|
23
|
+
def not_found(klass)
|
24
|
+
raise SchemaNotFound, "#{klass.name}Schema is not found."
|
25
|
+
end
|
26
|
+
|
27
|
+
def schema_class(resource)
|
28
|
+
klass = resource.class
|
29
|
+
klass = klass.base_class if klass.respond_to?(:base_class)
|
30
|
+
schema_klass = "#{klass.name}Schema".safe_constantize
|
31
|
+
not_found(klass) if schema_klass.nil?
|
32
|
+
schema_klass
|
33
|
+
end
|
34
|
+
|
35
|
+
def init_schema(resource, options)
|
36
|
+
schema_klass = options[:schema].presence || schema_class(resource)
|
37
|
+
schema_klass.new(resource, @context, options.except(:schema))
|
38
|
+
end
|
39
|
+
|
32
40
|
def walk(resource, options)
|
33
41
|
result = nil
|
34
42
|
if resource.respond_to?(:map)
|
35
43
|
result = resource.map {|r| walk(r, options)}
|
36
44
|
else
|
37
|
-
|
38
|
-
schema = schema_class.new(resource, @context, options.except(:schema))
|
39
|
-
result = schema.object.id
|
40
|
-
entity_key = schema.object.class.base_class.name.pluralize.to_sym
|
45
|
+
schema = init_schema(resource, options)
|
41
46
|
hash = schema.attributes
|
42
|
-
schema.associations.each do |
|
43
|
-
|
44
|
-
|
47
|
+
schema.associations.each do |assoc, assoc_options|
|
48
|
+
assoc_resource = schema.association_resource(assoc)
|
49
|
+
assoc_result = nil
|
50
|
+
unless assoc_resource.nil?
|
51
|
+
assoc_result = walk(assoc_resource, assoc_options)
|
52
|
+
end
|
53
|
+
hash[assoc] = assoc_result
|
45
54
|
end
|
55
|
+
result = schema.object.id
|
56
|
+
entity_key = schema.entity_key
|
46
57
|
@entities[entity_key] ||= {}
|
47
58
|
@entities[entity_key][result] = hash
|
48
59
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: normalizr_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- shinya takahashi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|