json_api_model 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +9 -0
- data/Gemfile +0 -3
- data/Gemfile.lock +1 -1
- data/README.md +15 -4
- data/lib/json_api_model/model.rb +16 -0
- data/lib/json_api_model/scope.rb +2 -6
- data/lib/json_api_model/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: 89df9b9d8d8f4ae17c7bcd671f8f272b0339c5bc
|
4
|
+
data.tar.gz: 7db7eac6d7c3d4398fc3b912f23eed7f90c631f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eb9b10ac0e5a56e2bb2307e7e2b9cde7fce35d2a409d34df9490e616a01902d801274032539db5062bfc62634a865ef7045276836972566671e43e657a0cab9
|
7
|
+
data.tar.gz: 03f7d39b4504ff47099aaec9fec4b9934f58cd31314a6ad1cd0712784c5b5f4e994332b477f00ebc06025147bfa07b4deb47d81b6b2977af559244b845691a75
|
data/.travis.yml
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
sudo: false
|
2
|
+
env:
|
3
|
+
global:
|
4
|
+
- CC_TEST_REPORTER_ID=c174e3bfd117918fe3df02942dea49339cc60d47bcca258b764431d7474f9fc1
|
2
5
|
language: ruby
|
3
6
|
rvm:
|
4
7
|
- 2.4.3
|
8
|
+
before_script:
|
9
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
10
|
+
- chmod +x ./cc-test-reporter
|
11
|
+
- ./cc-test-reporter before-build
|
5
12
|
before_install: gem install bundler -v 1.16.1
|
13
|
+
after_script:
|
14
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# JsonApiModel
|
2
2
|
|
3
|
+
[](https://travis-ci.org/gaorlov/json_api_model)
|
4
|
+
[](https://codeclimate.com/github/gaorlov/json_api_model/maintainability)
|
5
|
+
[](https://codeclimate.com/github/gaorlov/json_api_model/test_coverage)
|
6
|
+
|
3
7
|
Much like `ActiveRecord` is an ORM on top of your database, [`JSON API Client`](https://github.com/JsonApiClient/json_api_client) is an ORM specific to a service. This gem is the `app/models/` on top of `json_api_client`.
|
4
8
|
|
5
9
|
Yes, you can put business in the client, but if you need to distrubute the gem, you will want that to live somewhere else. This gem provides a thin wrapper layer to let you do that.
|
@@ -20,9 +24,6 @@ Or install it yourself as:
|
|
20
24
|
|
21
25
|
$ gem install json_api_model
|
22
26
|
|
23
|
-
## Disclaimer
|
24
|
-
|
25
|
-
This is a work in progress. Right now only fetching data works. This will soon change.
|
26
27
|
|
27
28
|
## Usage
|
28
29
|
|
@@ -46,7 +47,7 @@ module UserService
|
|
46
47
|
end
|
47
48
|
```
|
48
49
|
|
49
|
-
And the interaction with it is
|
50
|
+
And the interaction with it is identical as how you would work with the client:
|
50
51
|
|
51
52
|
```ruby
|
52
53
|
# fetching looks identical to as json_api_client (because it thinly wraps it)
|
@@ -60,6 +61,16 @@ user.lucky_number
|
|
60
61
|
# but also transparently access the client properties
|
61
62
|
user.id
|
62
63
|
# => 8
|
64
|
+
|
65
|
+
# creating new users
|
66
|
+
new_user = UserService::User.new name: "greg"
|
67
|
+
# => #<UserService::User:0x000055e1fc1c8c00 @client=#<UserService::Client::User:@attributes={"type"=>"users", "name"=>"greg"}>>
|
68
|
+
new_user.save
|
69
|
+
# => true
|
70
|
+
|
71
|
+
# and now that the record is saved, you can search for it and get back a JsonApiModel back to keep working with
|
72
|
+
UserService::User.where( name: "greg" ).first
|
73
|
+
# => #<UserService::User:0x000055e1fc1c8c00 @client=#<UserService::Client::User:@attributes={"type"=>"users", "name"=>"greg", "id"=>"9"}>>
|
63
74
|
```
|
64
75
|
|
65
76
|
### Rendering
|
data/lib/json_api_model/model.rb
CHANGED
@@ -20,6 +20,20 @@ module JsonApiModel
|
|
20
20
|
client_class.connection true, &block
|
21
21
|
end
|
22
22
|
|
23
|
+
def method_missing( m, *args, &block )
|
24
|
+
result = client_class.send m, *args, &block
|
25
|
+
case result
|
26
|
+
when JsonApiClient::ResultSet
|
27
|
+
JsonApiModel::ResultSet.new( result, self )
|
28
|
+
when client_class
|
29
|
+
new_from_client result
|
30
|
+
else
|
31
|
+
result
|
32
|
+
end
|
33
|
+
rescue NoMethodError
|
34
|
+
raise NoMethodError, "No method `#{m}' found in #{self} or #{client_class}"
|
35
|
+
end
|
36
|
+
|
23
37
|
private
|
24
38
|
|
25
39
|
def _new_scope
|
@@ -35,6 +49,8 @@ module JsonApiModel
|
|
35
49
|
|
36
50
|
def method_missing( m, *args, &block )
|
37
51
|
client.send m, *args, &block
|
52
|
+
rescue NoMethodError
|
53
|
+
raise NoMethodError, "No method `#{m}' found in #{self} or #{client}"
|
38
54
|
end
|
39
55
|
|
40
56
|
def as_json
|
data/lib/json_api_model/scope.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module JsonApiModel
|
2
2
|
class Scope
|
3
3
|
def initialize( model_class )
|
4
|
-
@model_class
|
5
|
-
@client_scope
|
4
|
+
@model_class = model_class
|
5
|
+
@client_scope = JsonApiClient::Query::Builder.new( model_class.client_class )
|
6
6
|
end
|
7
7
|
|
8
8
|
def to_a
|
@@ -33,10 +33,6 @@ module JsonApiModel
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def build
|
37
|
-
@model_class.new_from_client @client_scope.build
|
38
|
-
end
|
39
|
-
|
40
36
|
def params
|
41
37
|
@client_scope.params
|
42
38
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Orlov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json_api_client
|