traim 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -10
  3. data/lib/traim.rb +9 -8
  4. data/traim.gemspec +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a3cd06341db0633e95bded7bd7e6a45a5348511
4
- data.tar.gz: 3ab56f6e5470c5fe0cb5693619a8251be0f3bf0c
3
+ metadata.gz: f905b5beabf9f73c766a104fcf98618ff5849263
4
+ data.tar.gz: ab123a3a6068bd2a45e9ffd1acbff56e6f22ee99
5
5
  SHA512:
6
- metadata.gz: a1d83bcf7a6c86db7f0d61ee147d1bcfebe3d4f913361443aca879d679b46cd3f7838cbcf1d6d84aa860c16c21f009afd6dda120fb5c544c8a26b93c9c1b38bc
7
- data.tar.gz: f7afe6353577ad2ac32e698bd7d4cc6584ac4110bf80ac5c86b1bbd94584bfaaea04e9aa9482e3881278d60b960ceb6495c9545f7f55a08f177f467ead290f5a
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 Building a RESTful API service from your existing ActiveRecord models.
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 train
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
- Train.application do
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 :destory
43
+ action :destroy
44
44
  end
45
45
  end
46
46
  ```
47
- put your activerecord config in
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 CURD RESTful API from the user ActiveRecord model.
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. you can write your own endpoint as well.
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
- create nestea json reponse with activerecord association
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
- ## Visual attributes
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
@@ -314,7 +314,9 @@ class Traim
314
314
  fields << {name: name, type: 'connection'}
315
315
  end
316
316
 
317
- def to_hash(object, resources, nest_associations = [])
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 nest_associations.include?(name)
328
- raise Error if object.class.reflections[name.to_s].blank?
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, nest_associations)
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 nest_associations.include?(resource_name)
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
- nest_associations << resource_name
338
- resources[resource_name].to_hash(object.send(name), nest_associations)
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "traim"
3
- s.version = "0.1.2"
3
+ s.version = "0.1.3"
4
4
  s.summary = %{Resource-oriented microframework for RESTful API}
5
5
  s.description = %Q{Resource-oriented microframework for RESTful API}
6
6
  s.authors = ["Travis Liu"]
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.2
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: 2017-07-22 00:00:00.000000000 Z
11
+ date: 2018-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack