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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ea8f30ecfd30fc530cea831dbb691d2e73cc28b
4
- data.tar.gz: 44532b0fe53c3aeb53b0a6e6220bb79838c32ae8
3
+ metadata.gz: 95de21e33d5f39694c35f6b4b7f2093522ecb1a2
4
+ data.tar.gz: c93846067ce9d15c17f7dca3bf869638cee991b0
5
5
  SHA512:
6
- metadata.gz: 786a73bc1a511a3df9141a8a40ddd21b5126b7341d2e11de19fbc8bd3d84a169f7d598bbc8dc27902a9123ca0fc87f22879a0d88e103ba26e3f7867da9a84fec
7
- data.tar.gz: 0035319b088c36d218b17fdb3cad644663cd787f1fdd51e5dd4d252334b2869737a6074d6aefbe011a34ac7ae88160eecbd190b3f24c3884178857251a388fc2
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
- schema_class = options[:schema].presence || self.class.get_schema_class(resource)
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 |association, assoc_options|
43
- association_result = walk(schema.object.send(association), assoc_options)
44
- hash[association] = association_result
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
@@ -45,5 +45,17 @@ module NormalizrRuby
45
45
  end
46
46
  hash
47
47
  end
48
+
49
+ def entity_key
50
+ object.class
51
+ .base_class
52
+ .name
53
+ .pluralize
54
+ .to_sym
55
+ end
56
+
57
+ def association_resource(association_key)
58
+ object.send(association_key)
59
+ end
48
60
  end
49
61
  end
@@ -1,3 +1,3 @@
1
1
  module NormalizrRuby
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  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.2
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-08-29 00:00:00.000000000 Z
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport