jsonapi_mapper 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -4
- data/circle.yml +7 -0
- data/lib/jsonapi_mapper/version.rb +1 -1
- data/lib/jsonapi_mapper.rb +25 -8
- metadata +3 -3
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8da746332e94869adf5d9637999866f05c4b2c0a
|
4
|
+
data.tar.gz: ef0dd97cc32c359b48d62e5f6de88ca36e95f630
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6401a0a6a12a03c4874fcac5ef69aa9b89a3449b31265448e5092b2b75c2d912ff87d672155c426e38ec300b6af784279db7e2e7e9b3948a4b1a2dde90098730
|
7
|
+
data.tar.gz: a6d2495852c6ce11489138bc779a1eab3b67741b05981ff58328f1e5d7c41be94f218a950a4b3a3e2c664c484cf5b36a5efb41046d19b4d44bf2df473d13d4f6
|
data/README.md
CHANGED
@@ -70,9 +70,21 @@ See the specs directory for more examples.
|
|
70
70
|
pet_dogs: [:name, country: 'argentina']
|
71
71
|
)
|
72
72
|
|
73
|
-
|
74
|
-
|
75
|
-
|
73
|
+
# The document data lives in mapper.data
|
74
|
+
# It's always an array, even if the document had a single resource.
|
75
|
+
# If you want to check wether the document had a single resource
|
76
|
+
# or a collection as its primary data you can use the following methods.
|
77
|
+
mapper.collection? # Was primary document data a collection?
|
78
|
+
mapper.single? # Was primary document data a single resource?
|
79
|
+
|
80
|
+
person = mapper.data.first
|
81
|
+
|
82
|
+
# The rest of the included resources live in mapper.included
|
83
|
+
others = mapper.included
|
84
|
+
|
85
|
+
# Attempts to save both data and included. Returns false if there
|
86
|
+
# were any validation errors.
|
87
|
+
mapper.save_all
|
76
88
|
|
77
89
|
# Four people have been created
|
78
90
|
Person.count.should == 4
|
@@ -145,7 +157,11 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
145
157
|
|
146
158
|
## Contributing
|
147
159
|
|
148
|
-
Bug reports and pull requests are welcome
|
160
|
+
Bug reports and pull requests are welcome here.
|
161
|
+
|
162
|
+
## Code Status
|
163
|
+
|
164
|
+
[![Build Status](https://circleci.com/gh/bitex-la/jsonapi-mapper.png)](https://circleci.com/gh/bitex-la/jsonapi-mapper)
|
149
165
|
|
150
166
|
## License
|
151
167
|
|
data/circle.yml
ADDED
data/lib/jsonapi_mapper.rb
CHANGED
@@ -28,22 +28,22 @@ module JsonapiMapper
|
|
28
28
|
|
29
29
|
setup_types(rules)
|
30
30
|
|
31
|
-
main = if data = document[:data]
|
31
|
+
main = if data = self.document[:data]
|
32
32
|
if data.is_a?(Array)
|
33
33
|
data.map{|r| build_resource(r) }.compact
|
34
34
|
else
|
35
|
-
build_resource(data)
|
35
|
+
[ build_resource(data) ].compact
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
rest = if included = document[:included]
|
39
|
+
rest = if included = self.document[:included]
|
40
40
|
included.map{|r| build_resource(r) }.compact
|
41
41
|
end
|
42
42
|
|
43
43
|
resources.each{|_,r| assign_relationships(r) }
|
44
44
|
|
45
|
-
self.data = main.
|
46
|
-
self.included = rest.try(:map, &:object)
|
45
|
+
self.data = main.try(:map, &:object) || []
|
46
|
+
self.included = rest.try(:map, &:object) || []
|
47
47
|
end
|
48
48
|
|
49
49
|
def setup_types(rules)
|
@@ -154,13 +154,30 @@ module JsonapiMapper
|
|
154
154
|
renames.fetch(:attributes, {}).fetch(type, {}).fetch(attr, attr)
|
155
155
|
end
|
156
156
|
|
157
|
+
def all
|
158
|
+
(data + included)
|
159
|
+
end
|
160
|
+
|
157
161
|
def save_all
|
158
|
-
|
159
|
-
|
162
|
+
return false unless all.all?(&:valid?)
|
163
|
+
all.each(&:save)
|
164
|
+
true
|
160
165
|
end
|
161
166
|
|
162
167
|
def collection?
|
163
|
-
data.
|
168
|
+
data.size > 1
|
169
|
+
end
|
170
|
+
|
171
|
+
def single?
|
172
|
+
!collection?
|
173
|
+
end
|
174
|
+
|
175
|
+
def map_data(cls, &blk)
|
176
|
+
data.select{|o| o.is_a?(cls)}.map(&blk)
|
177
|
+
end
|
178
|
+
|
179
|
+
def map_all(cls, &blk)
|
180
|
+
all.select{|o| o.is_a?(cls)}.map(&blk)
|
164
181
|
end
|
165
182
|
end
|
166
183
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nubis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -163,13 +163,13 @@ extra_rdoc_files: []
|
|
163
163
|
files:
|
164
164
|
- ".gitignore"
|
165
165
|
- ".rspec"
|
166
|
-
- ".travis.yml"
|
167
166
|
- Gemfile
|
168
167
|
- LICENSE.txt
|
169
168
|
- README.md
|
170
169
|
- Rakefile
|
171
170
|
- bin/console
|
172
171
|
- bin/setup
|
172
|
+
- circle.yml
|
173
173
|
- jsonapi_mapper.gemspec
|
174
174
|
- lib/jsonapi_mapper.rb
|
175
175
|
- lib/jsonapi_mapper/version.rb
|