traim 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 +4 -4
- data/README.md +10 -10
- data/lib/traim.rb +9 -8
- data/traim.gemspec +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: f905b5beabf9f73c766a104fcf98618ff5849263
|
4
|
+
data.tar.gz: ab123a3a6068bd2a45e9ffd1acbff56e6f22ee99
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 615d1dff8a603429d0f8e19259ef416fe838bf611ee94a3c40d6dc885300cf1c0a5b02393627163a39d6dfb5586dc5846fe8b09e19504e3aaa4d1078552058a5
|
7
|
+
data.tar.gz: ffbe8f31ef7893f9463a8b03328bbcdc45d1471853b30243b8a5a90efe3d9a83011a899229450699a92d60f223168d0ed07242a3d4ea03e97c344c293a9ceb52
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# Traim
|
2
2
|
|
3
|
-
Traim is a microframework for
|
3
|
+
Traim is a microframework for building a RESTful API service from your existing Active Record models.
|
4
4
|
|
5
5
|
## Getting started
|
6
6
|
|
7
7
|
### Installation
|
8
8
|
``` ruby
|
9
|
-
gem install
|
9
|
+
gem install traim
|
10
10
|
```
|
11
11
|
|
12
12
|
### Usage
|
@@ -21,7 +21,7 @@ end
|
|
21
21
|
class User < ActiveRecord::Base
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
Traim.application do
|
25
25
|
resource :users do
|
26
26
|
# Inject user model
|
27
27
|
model User
|
@@ -40,11 +40,11 @@ Train.application do
|
|
40
40
|
action :update
|
41
41
|
|
42
42
|
# DELETE /users/:id
|
43
|
-
action :
|
43
|
+
action :destroy
|
44
44
|
end
|
45
45
|
end
|
46
46
|
```
|
47
|
-
|
47
|
+
Put your Active Record configuration in
|
48
48
|
` config/database.yml `
|
49
49
|
|
50
50
|
To run it, you can create a config.ru file
|
@@ -57,10 +57,10 @@ run Traim
|
|
57
57
|
|
58
58
|
Then run `rackup`.
|
59
59
|
|
60
|
-
Now, you already get basic
|
60
|
+
Now, you already get a basic CRUD RESTful API from the User ActiveRecord model.
|
61
61
|
|
62
62
|
## Customizable action
|
63
|
-
By default, `action` can be easily used to create an endpoint for CRUD operations.
|
63
|
+
By default, `action` can be easily used to create an endpoint for CRUD operations. You can write your own endpoint as well.
|
64
64
|
``` ruby
|
65
65
|
Traim.application do
|
66
66
|
resources :users do
|
@@ -84,7 +84,7 @@ Response
|
|
84
84
|
```
|
85
85
|
|
86
86
|
## Associations
|
87
|
-
|
87
|
+
Create a nested JSON reponse with an Active Record association
|
88
88
|
``` ruby
|
89
89
|
class User < ActiveRecord::Base
|
90
90
|
has_many :books
|
@@ -118,7 +118,7 @@ Response
|
|
118
118
|
``` json
|
119
119
|
{
|
120
120
|
"id": 1,
|
121
|
-
"name": "travis"
|
121
|
+
"name": "travis",
|
122
122
|
"books": [
|
123
123
|
{"isbn": "978-1-61-729109-8"},
|
124
124
|
{"isbn": "561-6-28-209847-7"},
|
@@ -232,7 +232,7 @@ Traim.application do
|
|
232
232
|
end
|
233
233
|
```
|
234
234
|
|
235
|
-
##
|
235
|
+
## Virtual attributes
|
236
236
|
Built-in attribute is generate response fields from model. Visual can help you present fields outside of model attributes.
|
237
237
|
``` ruby
|
238
238
|
Traim.application do
|
data/lib/traim.rb
CHANGED
@@ -314,7 +314,9 @@ class Traim
|
|
314
314
|
fields << {name: name, type: 'connection'}
|
315
315
|
end
|
316
316
|
|
317
|
-
def to_hash(object, resources,
|
317
|
+
def to_hash(object, resources, nested_associations = [])
|
318
|
+
return if object.nil?
|
319
|
+
|
318
320
|
fields.inject({}) do | hash, attr|
|
319
321
|
name = attr[:name]
|
320
322
|
hash[name] = if attr[:type] == 'attribute'
|
@@ -324,18 +326,17 @@ class Traim
|
|
324
326
|
execute(object, &attr[:block])
|
325
327
|
end
|
326
328
|
elsif attr[:type] == 'association'
|
327
|
-
raise Error if
|
328
|
-
|
329
|
-
nest_associations << name
|
329
|
+
raise Error if nested_associations.include?(name)
|
330
|
+
nested_associations << name
|
330
331
|
object.send(name).map do |association|
|
331
|
-
resources[name].to_hash(association,
|
332
|
+
resources[name].to_hash(association, resources, nested_associations.dup)
|
332
333
|
end
|
333
334
|
else
|
334
335
|
resource_name = name.to_s.pluralize.to_sym
|
335
|
-
raise Error.new(message: "Inifinite Association") if
|
336
|
+
raise Error.new(message: "Inifinite Association") if nested_associations.include?(resource_name)
|
336
337
|
raise Error if object.class.reflections[name.to_s].blank?
|
337
|
-
|
338
|
-
resources[resource_name].to_hash(object.send(name),
|
338
|
+
nested_associations << resource_name
|
339
|
+
resources[resource_name].to_hash(object.send(name), resources, nested_associations.dup)
|
339
340
|
end
|
340
341
|
hash
|
341
342
|
end
|
data/traim.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: traim
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Liu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|