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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '039bcb95b5c5b71d03c25ead00660671ad9a2db2'
4
- data.tar.gz: bc0abb1fe5a9dd7b119367e5b91a0c1480e4e2ca
3
+ metadata.gz: 8da746332e94869adf5d9637999866f05c4b2c0a
4
+ data.tar.gz: ef0dd97cc32c359b48d62e5f6de88ca36e95f630
5
5
  SHA512:
6
- metadata.gz: 55ccb1660bcd748ddea0cb153e1b3342be87fbdee5b3a0e793b1e3a50b85e548c69f81bcedd8a893710fdb3342ef938782d46e8266b0cb9ce9255c8c1f74c917
7
- data.tar.gz: a84cf5543ad7f79b3b02e8361139f263b1f7ea949971fb75c4bea93a14bf345fed0b0b997e2ea6ee46db3a4310769245dccbaf082be1ec891db9987109eb1a74
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
- person = mapper.data # This is the main document data as a new object
74
- others = mapper.included # These are all the other resources.
75
- mapper.save_all # Attempts to save both data and included.
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 on GitHub at https://github.com/[USERNAME]/jsonapi_mapper.
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
@@ -0,0 +1,7 @@
1
+ dependencies:
2
+ pre:
3
+ - gem install bundler
4
+
5
+ database:
6
+ override:
7
+ - echo "no database"
@@ -1,3 +1,3 @@
1
1
  module JsonapiMapper
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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.is_a?(Array) ? main.map(&:object) : main.try(:object)
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
- data.is_a?(Array) ? data.each(&:save) : data.try(:save)
159
- included.try(:each, &:save)
162
+ return false unless all.all?(&:valid?)
163
+ all.each(&:save)
164
+ true
160
165
  end
161
166
 
162
167
  def collection?
163
- data.is_a?(Array)
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.0
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-27 00:00:00.000000000 Z
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
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.4.1
5
- before_install: gem install bundler -v 1.15.4