motion-json-api 0.0.1 → 0.0.2

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: b00c290ef84e915cf55ea3e6f7d3a9e68a096951
4
- data.tar.gz: 95b4a777808fe8751731487f047604f46648b3ad
3
+ metadata.gz: 7dd1b8babd9d470b1b7af3fd3106e26496a81734
4
+ data.tar.gz: 1cc071905c1f335c2a7c821a1b6f701e6a32f2de
5
5
  SHA512:
6
- metadata.gz: 9184db4c31abc1e0646ea541d82d3f6d862fb5203f0fb6d51799ceaf16d7f75ea986b91e29d0e65fcab46a5b9f32c07a102a8069d3c9485b350f4d738a51fe6a
7
- data.tar.gz: da1a3ed70b72a6af029eb7ef090dd1f03ea357cbc76e544c90404ff2f3cfbe1a8c6d462559e5af16c52fc9c7f9194f969d0ace50898e47c195cef464e2ae34fe
6
+ metadata.gz: 761ba2811df1c545547ba321d9788e1d06e4f33bdc49f425577d51bf62d14f8351f8f4cddd26385d1674d60134c78a2fac711f75bb19b60b99e56fdca747d3ad
7
+ data.tar.gz: 15c6a28c9c2abbe4b78160d207904277c66ae69641f09c561cd185eeac2c0363745cbbaebe23d2671431b1ea6b8e7c8d1e37cf4d134b5ff948e3c65bdda3707f
@@ -1,8 +1,11 @@
1
- require 'motion-json-api/exceptions'
2
- require 'motion-json-api/resource'
3
-
4
- module MotionJsonApi
5
- def self.parse(json)
6
- Resource._object_handler(json, json.fetch("data", []), json.fetch("included", []))
1
+ if defined?(Motion::Project::Config)
2
+ Motion::Project::App.setup do |app|
3
+ Dir.glob(File.join(File.dirname(__FILE__), "motion-json-api/**/*.rb")).each do |file|
4
+ app.files.unshift(file)
5
+ end
7
6
  end
7
+ else
8
+ require 'motion-json-api/exceptions'
9
+ require 'motion-json-api/descendants'
10
+ require 'motion-json-api/resource'
8
11
  end
@@ -0,0 +1,73 @@
1
+ # Taken from https://github.com/rubymotion/motion-support/blob/master/motion/descendants_tracker.rb
2
+
3
+ # Copyright (c) 2005-2013 David Heinemeier Hansson
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module MotionJsonApi
25
+ # This module provides an internal implementation to track descendants
26
+ # which is faster than iterating through ObjectSpace.
27
+ module Descendants
28
+ @@direct_descendants = {}
29
+
30
+ class << self
31
+ def direct_descendants(klass)
32
+ @@direct_descendants[klass] || []
33
+ end
34
+
35
+ def descendants(klass)
36
+ arr = []
37
+ accumulate_descendants(klass, arr)
38
+ arr
39
+ end
40
+
41
+ def clear
42
+ @@direct_descendants.clear
43
+ end
44
+
45
+ # This is the only method that is not thread safe, but is only ever called
46
+ # during the eager loading phase.
47
+ def store_inherited(klass, descendant)
48
+ (@@direct_descendants[klass] ||= []) << descendant
49
+ end
50
+
51
+ private
52
+ def accumulate_descendants(klass, acc)
53
+ if direct_descendants = @@direct_descendants[klass]
54
+ acc.concat(direct_descendants)
55
+ direct_descendants.each { |direct_descendant| accumulate_descendants(direct_descendant, acc) }
56
+ end
57
+ end
58
+ end
59
+
60
+ def inherited(base)
61
+ Descendants.store_inherited(self, base)
62
+ super
63
+ end
64
+
65
+ def direct_descendants
66
+ Descendants.direct_descendants(self)
67
+ end
68
+
69
+ def descendants
70
+ Descendants.descendants(self)
71
+ end
72
+ end
73
+ end
@@ -1,5 +1,11 @@
1
1
  module MotionJsonApi
2
+ def self.parse(json)
3
+ Resource._object_handler(json, json.fetch("data", []), json.fetch("included", []))
4
+ end
5
+
2
6
  class Resource
7
+ extend Descendants
8
+
3
9
  attr_accessor :id
4
10
  attr_accessor :attributes
5
11
  attr_accessor :relationships
@@ -37,10 +43,15 @@ module MotionJsonApi
37
43
  key = options.fetch(:as, relation).to_s
38
44
  define_method(key) do
39
45
  relationship = self.relationships.fetch(key)
46
+
40
47
  data = relationship.fetch("data")
41
- object = _find_in_included(data["id"], data["type"])
42
- payload = {"data" => object, "links" => relationship.fetch("links", {})}
43
- Resource._object_handler(payload, self.top_level, self.included)
48
+ if data
49
+ object = _find_in_included(data["id"], data["type"])
50
+ payload = {"data" => object, "links" => relationship.fetch("links", {})}
51
+ Resource._object_handler(payload, self.top_level, self.included)
52
+ else
53
+ nil
54
+ end
44
55
  end
45
56
  end
46
57
 
@@ -78,12 +89,13 @@ module MotionJsonApi
78
89
  end
79
90
 
80
91
  def self._descendants
81
- ObjectSpace.each_object(Class).select { |klass| klass < self }
92
+ Resource.descendants
82
93
  end
83
94
 
84
95
  def self._object_handler(object, top_level, included = nil)
85
96
  included ||= self.included || object.fetch("included", [])
86
- top_level ||= self.top_level || [object["data"]].flatten
97
+ top_level ||= self.top_level || object["data"]
98
+ top_level = [top_level].flatten
87
99
 
88
100
  case object["data"]
89
101
  when Array
@@ -1,3 +1,3 @@
1
1
  module MotionJsonApi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-json-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joffrey Jaffeux
@@ -63,6 +63,7 @@ files:
63
63
  - LICENSE.txt
64
64
  - README.md
65
65
  - lib/motion-json-api.rb
66
+ - lib/motion-json-api/descendants.rb
66
67
  - lib/motion-json-api/exceptions.rb
67
68
  - lib/motion-json-api/resource.rb
68
69
  - lib/motion-json-api/version.rb