grape-resource-definition 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/README.md +15 -14
- data/lib/grape_resource_definition/resource_definition.rb +13 -12
- data/lib/grape_resource_definition/version.rb +1 -1
- data/spec/resource_definition_spec.rb +4 -8
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41afde0e8f7d1d7ab968f5b684b8069803ab4849
|
4
|
+
data.tar.gz: acc19e78cd6b4945d5ea3ceeb85b35d627075049
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e0d0e016a4e31d9c140c8504971a3fa33460ef2b789b6dad0766c7d6cc7a98efd01745e29206e20b2300a40747fe4e9f439aaa014404fbd7d919d1d4085de41
|
7
|
+
data.tar.gz: 5975764e11ea342c41a288b14daf4594791ff61aefbb0aa30ee5028be83b106e05330d1e7786f1d2d02ca404885f8ac0397c8f909292f717a3342fcfc8d5da64
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Grape::ResourceDefinition
|
2
2
|
=========================
|
3
|
-
|
4
|
-
(
|
3
|
+
![](https://travis-ci.org/joaquimadraz/grape-resource-definition.svg)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/joaquimadraz/grape-resource-definition/badges/gpa.svg)](https://codeclimate.com/github/joaquimadraz/grape-resource-definition)
|
5
5
|
|
6
6
|
Based on Praxis principle, that design and implementation should be separated, I've created this gem to add the possibility to define the params validation & coercion outside the route's class.
|
7
7
|
|
@@ -20,11 +20,11 @@ class Posts < Grape::API
|
|
20
20
|
optional :order_by, type: String
|
21
21
|
optional :order_dir, type: String
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
get '/posts' do
|
25
25
|
# Logic to get those posts
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
end
|
29
29
|
```
|
30
30
|
|
@@ -36,13 +36,13 @@ So.. with this gem you can define your resource definition outside your route an
|
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
module Resources
|
39
|
-
|
39
|
+
|
40
40
|
module Posts
|
41
|
-
|
41
|
+
|
42
42
|
include Grape::ResourceDefinition
|
43
|
-
|
43
|
+
|
44
44
|
resource_define :index do
|
45
|
-
|
45
|
+
|
46
46
|
desc 'Get all blog posts',
|
47
47
|
http_codes: [
|
48
48
|
[200, 'Ok']
|
@@ -53,14 +53,14 @@ module Resources
|
|
53
53
|
optional :order_by, type: String
|
54
54
|
optional :order_dir, type: String
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
end
|
62
62
|
```
|
63
|
-
|
63
|
+
|
64
64
|
...and your route links to that resource:
|
65
65
|
|
66
66
|
```ruby
|
@@ -72,11 +72,11 @@ class Posts < Grape::API
|
|
72
72
|
get '/posts' do
|
73
73
|
# Logic to get those posts
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
end
|
77
77
|
```
|
78
78
|
|
79
|
-
I've made this really simple, the rest it's up to you. I don't want to add constraints on that.
|
79
|
+
I've made this really simple, the rest it's up to you. I don't want to add constraints on that.
|
80
80
|
|
81
81
|
## Installation
|
82
82
|
|
@@ -92,3 +92,4 @@ Or install it yourself as:
|
|
92
92
|
|
93
93
|
$ gem install grape-resource-definition
|
94
94
|
|
95
|
+
|
@@ -3,16 +3,17 @@ module Grape
|
|
3
3
|
|
4
4
|
module ClassMethods
|
5
5
|
|
6
|
-
def definitions
|
7
|
-
model_name = Grape::ResourceDefinition.get_class_name(self)
|
8
|
-
Grape::ResourceDefinition.defined_resources[model_name]
|
9
|
-
end
|
10
|
-
|
11
6
|
def resource_define(name, ¶ms_block)
|
7
|
+
definitions = \
|
8
|
+
Grape::ResourceDefinition.defined_resources["#{self}"]
|
9
|
+
|
12
10
|
definitions[name] = params_block
|
13
11
|
end
|
14
12
|
|
15
13
|
def define(name)
|
14
|
+
definitions = \
|
15
|
+
Grape::ResourceDefinition.defined_resources["#{self::RESOURCE_DEFINITION}"]
|
16
|
+
|
16
17
|
if definitions.nil?
|
17
18
|
raise NoResourceDefinition, "No resource definition for #{self}"
|
18
19
|
end
|
@@ -31,19 +32,13 @@ module Grape
|
|
31
32
|
end
|
32
33
|
|
33
34
|
def self.included(other)
|
34
|
-
model_name = get_class_name(other)
|
35
|
-
|
36
35
|
@defined_resources ||= {}
|
37
|
-
@defined_resources[
|
36
|
+
@defined_resources["#{other}"] = {}
|
38
37
|
|
39
38
|
other.include ClassMethods
|
40
39
|
other.extend ClassMethods
|
41
40
|
end
|
42
41
|
|
43
|
-
def self.get_class_name(klass)
|
44
|
-
"#{klass.name}".gsub('::Resources', '').to_sym
|
45
|
-
end
|
46
|
-
|
47
42
|
end
|
48
43
|
end
|
49
44
|
|
@@ -51,6 +46,12 @@ module Grape
|
|
51
46
|
class API
|
52
47
|
|
53
48
|
def self.resource_definition(resource_definition_module)
|
49
|
+
begin
|
50
|
+
self.const_get('RESOURCE_DEFINITION')
|
51
|
+
rescue NameError => e
|
52
|
+
self.const_set('RESOURCE_DEFINITION', resource_definition_module)
|
53
|
+
end
|
54
|
+
|
54
55
|
self.class_eval do
|
55
56
|
extend resource_definition_module
|
56
57
|
end
|
@@ -45,19 +45,15 @@ describe Grape::ResourceDefinition do
|
|
45
45
|
Grape::ResourceDefinition.defined_resources
|
46
46
|
end
|
47
47
|
|
48
|
-
it 'should have
|
49
|
-
|
48
|
+
it 'should have Resources::Todos resource with :show and :index definition' do
|
50
49
|
expect(defined_resources).to \
|
51
|
-
have_key(
|
50
|
+
have_key('Resources::Todos')
|
52
51
|
|
53
|
-
expect(defined_resources[
|
52
|
+
expect(defined_resources['Resources::Todos']).to \
|
54
53
|
have_key(:index)
|
55
54
|
|
56
|
-
expect(defined_resources[
|
55
|
+
expect(defined_resources['Resources::Todos']).to \
|
57
56
|
have_key(:show)
|
58
|
-
|
59
|
-
# expect(subject).to receive(:define).with(:show)
|
60
|
-
|
61
57
|
end
|
62
58
|
|
63
59
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-resource-definition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joaquim Adráz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- ".rspec"
|
78
78
|
- ".ruby-gemset"
|
79
79
|
- ".ruby-version"
|
80
|
+
- ".travis.yml"
|
80
81
|
- Gemfile
|
81
82
|
- LICENSE
|
82
83
|
- README.md
|