traim 0.3.0 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7f9d2dc6a750a774b11b5d3902146d3a5b7cbb7
4
- data.tar.gz: fa2d4cb1d990e357481b137d9c1797c6f25ffe3f
3
+ metadata.gz: 1093282c876de373d85f4fdb421003afced47daf
4
+ data.tar.gz: f26ab0488f24a0985e1cf8e5b4bd4c8dbec1bf00
5
5
  SHA512:
6
- metadata.gz: 042f77d48d944dad9f3c3bd641a942efe5343aa96b890f37169eca4fc8a772bbef47a643a1ac8bf76fb9c315203e06ee7125739fd2cfdcbf872d63ae890b7b2d
7
- data.tar.gz: d69f5b48785ea17cf80b6c62260ad75371b6d68a742d2558551239c1d4fec7449c8959a4162086bed8ae29530adf4e2ff594593f9f0455d388a16349f6b8bdb5
6
+ metadata.gz: 1291114f6985f46faa1b350f2186463887e462fd644f3b2a2b87a3d876cf750e3add302019a2decc339216f31f86715b9810f1ba9571eaa7e3d97955f57c1800
7
+ data.tar.gz: b938ba3802c656bee133aec89bdb5deecc9548f8f150df00e6c96de994bdd57d1f37ac0d2f43282459048f33b2dc6dad84d90a43e405d53229cda57774739343
data/README.md CHANGED
@@ -220,13 +220,15 @@ Traim.application do
220
220
  helpers do
221
221
  def auth(user_id)
222
222
  raise BadRequestError.new(message: "unauthenticated request") unless model.exists?(id: user_id)
223
+
224
+ # attribute can be added in helpers as well
225
+ attribute :name
223
226
  end
224
227
  end
225
228
 
226
229
  model User
227
230
 
228
231
  attribute :id
229
- attribute :name
230
232
 
231
233
  action :show do
232
234
  auth(params["id"])
@@ -341,6 +341,7 @@ class Traim
341
341
  @request = request
342
342
  @headers = {}
343
343
  @actions = actions
344
+ @fields = []
344
345
 
345
346
  ok
346
347
 
@@ -348,6 +349,7 @@ class Traim
348
349
  @helper = helper
349
350
  @helper.request = @request
350
351
  @helper.status = @status
352
+ @helper.fields = @fields
351
353
  @helper.model = @model
352
354
  end
353
355
  end
@@ -358,6 +360,7 @@ class Traim
358
360
  attr_accessor :request
359
361
  attr_accessor :helper
360
362
  attr_accessor :status
363
+ attr_accessor :fields
361
364
 
362
365
  def logger; Traim.logger end
363
366
 
@@ -373,7 +376,6 @@ class Traim
373
376
 
374
377
  def actions(name); @actions[name] end
375
378
 
376
- def fields; @fields ||= [] end
377
379
  def attribute(name, &block)
378
380
  fields << {name: name, type: 'attribute', block: block}
379
381
  end
@@ -450,6 +452,17 @@ class Traim
450
452
  attr_accessor :request
451
453
  attr_accessor :status
452
454
  attr_accessor :model
455
+ attr_accessor :fields
453
456
 
457
+ def fields; @fields ||= [] end
458
+ def attribute(name, &block)
459
+ fields << {name: name, type: 'attribute', block: block}
460
+ end
461
+ def has_many(name, options={}, &block)
462
+ fields << {name: name, type: 'association', resource: options[:resource], block: block}
463
+ end
464
+ def has_one(name, options={}, &block)
465
+ fields << {name: name, type: 'connection', resource: options[:resource], block: block}
466
+ end
454
467
  end
455
468
  end
@@ -452,6 +452,29 @@ test "custom resource for association" do |user|
452
452
  assert result["second_book"]["isbn"] == "isbn_2"
453
453
  end
454
454
 
455
+ test "add attributes in helper" do |user|
456
+ app = Traim.application do
457
+ helpers do
458
+ def add_user_name
459
+ attribute :name
460
+ end
461
+ end
462
+ resources :users do
463
+ model User
464
+
465
+ attribute :id
466
+
467
+ action :show do
468
+ helper.add_user_name
469
+ record
470
+ end
471
+ end
472
+ end
473
+
474
+ _, headers, response = mock_request(app, "/users/#{user.id}", "GET")
475
+ result = JSON.parse(response.first)
476
+ assert result["name"] == "kolo"
477
+ end
455
478
 
456
479
  Book.delete_all
457
480
  User.delete_all
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "traim"
3
- s.version = "0.3.0"
3
+ s.version = "0.3.1"
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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Liu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-09 00:00:00.000000000 Z
11
+ date: 2018-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack