rest_model 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/belongs_to/resource_id_from_source.rb +16 -0
- data/examples/belongs_to/resource_id_options.rb +19 -0
- data/examples/belongs_to/simple.rb +15 -0
- data/lib/rest_model/key/builder.rb +1 -0
- data/lib/rest_model/key/relation/builder.rb +1 -0
- data/lib/rest_model/key/relation/response.rb +17 -8
- data/lib/rest_model/key/relation.rb +0 -1
- data/lib/rest_model/response.rb +5 -5
- data/lib/rest_model/version.rb +1 -1
- data/lib/rest_model.rb +16 -1
- data/spec/integration/belongs_to_spec.rb +75 -0
- data/spec/unit/key/relation/response_spec.rb +2 -2
- metadata +19 -14
@@ -0,0 +1,16 @@
|
|
1
|
+
$:.push 'examples'; require 'helper'
|
2
|
+
|
3
|
+
class Customer < RestModel
|
4
|
+
id field: :customer_id, type: Integer
|
5
|
+
has_many :services
|
6
|
+
end
|
7
|
+
|
8
|
+
class Service < RestModel
|
9
|
+
id type: Integer
|
10
|
+
belongs_to :customer
|
11
|
+
property :customer_id, field: :client_id, type: Integer
|
12
|
+
end
|
13
|
+
|
14
|
+
@service = Service.from_source(id: 123, client_id: 999).first
|
15
|
+
|
16
|
+
inspect_rest_model(@service)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.push 'examples'; require 'helper'
|
2
|
+
|
3
|
+
class Customer < RestModel
|
4
|
+
id field: :customer_id, type: Integer
|
5
|
+
has_many :services
|
6
|
+
end
|
7
|
+
|
8
|
+
class Service < RestModel
|
9
|
+
id type: Integer
|
10
|
+
belongs_to :customer, resource_id: proc {resource_id}
|
11
|
+
|
12
|
+
def resource_id
|
13
|
+
123456789
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
@service = Service.new(id: 123)
|
18
|
+
|
19
|
+
inspect_rest_model(@service)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
$:.push 'examples'; require 'helper'
|
2
|
+
|
3
|
+
class Customer < RestModel
|
4
|
+
id field: :customer_id, type: Integer
|
5
|
+
has_many :services
|
6
|
+
end
|
7
|
+
|
8
|
+
class Service < RestModel
|
9
|
+
id type: Integer
|
10
|
+
belongs_to :customer
|
11
|
+
end
|
12
|
+
|
13
|
+
@service = Service.new(id: 123, customer_id: 999)
|
14
|
+
|
15
|
+
inspect_rest_model(@service)
|
@@ -4,11 +4,12 @@ class RestModel
|
|
4
4
|
def to_resource(parent)
|
5
5
|
included = parent.__send__(name)
|
6
6
|
return {} unless visible?(parent) and included
|
7
|
+
|
7
8
|
{name => resource_from_included(included)}
|
8
9
|
end
|
9
10
|
|
10
11
|
def to_relation(parent)
|
11
|
-
{rel: name, href: href(parent)}
|
12
|
+
{rel: name, href: href(parent)} if resource_id(parent)
|
12
13
|
end
|
13
14
|
|
14
15
|
private
|
@@ -17,21 +18,29 @@ class RestModel
|
|
17
18
|
relation = parent.class.relations.find do |key|
|
18
19
|
key.resource_class == self.resource_class
|
19
20
|
end
|
21
|
+
|
20
22
|
relation.name or fail
|
21
23
|
end
|
22
24
|
|
23
25
|
def resource_from_included(included)
|
24
26
|
options = {root: false}
|
25
|
-
one? ?
|
26
|
-
included.resource(options)
|
27
|
-
: included.map {|r| r.resource(options)}
|
27
|
+
one? ? included.resource(options) : included.map {|r| r.resource(options)}
|
28
28
|
end
|
29
29
|
|
30
30
|
def href(parent)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
uri = has? ? "#{parent.class.resource_name}/#{resource_id(parent)}/#{relation_name(parent)}"
|
32
|
+
: "#{relation_name(parent).to_s.pluralize}/#{resource_id(parent)}"
|
33
|
+
|
34
|
+
"#{RestModel::Configuration.host}/#{uri}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def resource_id(parent)
|
38
|
+
if has?
|
39
|
+
parent.send(parent.class.id_key.name)
|
40
|
+
else
|
41
|
+
options[:resource_id] ? parent.instance_eval(&options[:resource_id])
|
42
|
+
: parent.__send__("#{self.name}_id")
|
43
|
+
end
|
35
44
|
end
|
36
45
|
end
|
37
46
|
end
|
data/lib/rest_model/response.rb
CHANGED
@@ -11,18 +11,18 @@ class RestModel
|
|
11
11
|
end
|
12
12
|
|
13
13
|
if root and self.class.relations.any? and !options[:summarize]
|
14
|
-
|
14
|
+
links = build_links
|
15
|
+
resource.merge!({link: links}) if links.any?
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
def
|
20
|
-
self.class.relations.map {|key| key.to_relation(self)}
|
20
|
+
def build_links
|
21
|
+
self.class.relations.map {|key| key.to_relation(self)}.reject(&:nil?)
|
21
22
|
end
|
22
23
|
|
23
24
|
def resource_keys(options)
|
24
|
-
summarize?(options) ? self.class.summarized_keys + [Href.new]
|
25
|
-
: self.class.keys
|
25
|
+
summarize?(options) ? self.class.summarized_keys + [Href.new] : self.class.keys
|
26
26
|
end
|
27
27
|
|
28
28
|
def summarize?(options)
|
data/lib/rest_model/version.rb
CHANGED
data/lib/rest_model.rb
CHANGED
@@ -48,6 +48,8 @@ class RestModel
|
|
48
48
|
|
49
49
|
attrs = attrs.with_indifferent_access
|
50
50
|
|
51
|
+
assign_non_keys_attrs(attrs)
|
52
|
+
|
51
53
|
self.class.keys.each do |key|
|
52
54
|
__send__("#{key.name}=", key.from_hash(attrs[key.name])) if key.present?(self)
|
53
55
|
end
|
@@ -58,6 +60,8 @@ class RestModel
|
|
58
60
|
|
59
61
|
attrs = attrs.with_indifferent_access
|
60
62
|
|
63
|
+
assign_non_keys_attrs(attrs)
|
64
|
+
|
61
65
|
self.class.keys.each do |key|
|
62
66
|
value = attrs[key.name]
|
63
67
|
|
@@ -95,6 +99,17 @@ class RestModel
|
|
95
99
|
end
|
96
100
|
|
97
101
|
def self.not_allowed_names
|
98
|
-
%w(resource_id resource
|
102
|
+
%w(resource_id resource)
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def assign_non_keys_attrs(attrs)
|
108
|
+
key_names = self.class.keys.map {|k| k.name}
|
109
|
+
non_keys = attrs.reject {|k, v| key_names.member?(k.to_sym)}
|
110
|
+
|
111
|
+
non_keys.each do |key, value|
|
112
|
+
__send__("#{key}=", value) if self.respond_to?("#{key}=")
|
113
|
+
end
|
99
114
|
end
|
100
115
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "belongs_to" do
|
4
|
+
describe_example "belongs_to/simple" do
|
5
|
+
it 'parses properly' do
|
6
|
+
@service.id.should == 123
|
7
|
+
@service.customer_id.should == 999
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#resource" do
|
11
|
+
subject {@service.resource}
|
12
|
+
|
13
|
+
it 'has a link' do
|
14
|
+
subject[:link].should be_an(Array)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has a customer relation" do
|
18
|
+
subject[:link].any? {|l| l[:rel] == :customer}.should be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "has a href for customer relation" do
|
22
|
+
link = subject[:link].find {|l| l[:rel] == :customer}
|
23
|
+
link[:href].should =~ %r{/customers/999}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe_example "belongs_to/resource_id_options" do
|
29
|
+
it 'parses properly' do
|
30
|
+
@service.id.should == 123
|
31
|
+
@service.customer_id.should be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#resource" do
|
35
|
+
subject {@service.resource}
|
36
|
+
|
37
|
+
it 'has a link' do
|
38
|
+
subject[:link].should be_an(Array)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "has a customer relation" do
|
42
|
+
subject[:link].any? {|l| l[:rel] == :customer}.should be_true
|
43
|
+
end
|
44
|
+
|
45
|
+
it "has a href for customer relation" do
|
46
|
+
link = subject[:link].find {|l| l[:rel] == :customer}
|
47
|
+
link[:href].should =~ %r{/customers/123456789}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe_example "belongs_to/resource_id_from_source", wip: true do
|
53
|
+
it 'parses properly' do
|
54
|
+
@service.id.should == 123
|
55
|
+
@service.customer_id.should == 999
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#resource" do
|
59
|
+
subject {@service.resource}
|
60
|
+
|
61
|
+
it 'has a link' do
|
62
|
+
subject[:link].should be_an(Array)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "has a customer relation" do
|
66
|
+
subject[:link].any? {|l| l[:rel] == :customer}.should be_true
|
67
|
+
end
|
68
|
+
|
69
|
+
it "has a href for customer relation" do
|
70
|
+
link = subject[:link].find {|l| l[:rel] == :customer}
|
71
|
+
link[:href].should =~ %r{/customers/999}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -85,8 +85,8 @@ describe RestModel::Response do
|
|
85
85
|
|
86
86
|
subject {ExampleChild.keys[1]}
|
87
87
|
|
88
|
-
let(:example) {ExampleChild.new(id: 200)}
|
89
|
-
let(:result) {{rel: :example, href: "http://example.com/examples/
|
88
|
+
let(:example) {ExampleChild.new(id: 200, example_id: 123)}
|
89
|
+
let(:result) {{rel: :example, href: "http://example.com/examples/123"}}
|
90
90
|
|
91
91
|
it_behaves_like "a relation"
|
92
92
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-11-
|
14
|
+
date: 2011-11-24 00:00:00.000000000Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
18
|
-
requirement: &
|
18
|
+
requirement: &70204201993120 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: '3.0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *70204201993120
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: i18n
|
29
|
-
requirement: &
|
29
|
+
requirement: &70204201992620 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ! '>='
|
@@ -34,10 +34,10 @@ dependencies:
|
|
34
34
|
version: '0.5'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *70204201992620
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: rspec
|
40
|
-
requirement: &
|
40
|
+
requirement: &70204201992160 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
@@ -45,10 +45,10 @@ dependencies:
|
|
45
45
|
version: '2.6'
|
46
46
|
type: :development
|
47
47
|
prerelease: false
|
48
|
-
version_requirements: *
|
48
|
+
version_requirements: *70204201992160
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: guard
|
51
|
-
requirement: &
|
51
|
+
requirement: &70204201991700 !ruby/object:Gem::Requirement
|
52
52
|
none: false
|
53
53
|
requirements:
|
54
54
|
- - ~>
|
@@ -56,10 +56,10 @@ dependencies:
|
|
56
56
|
version: '0.5'
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
|
-
version_requirements: *
|
59
|
+
version_requirements: *70204201991700
|
60
60
|
- !ruby/object:Gem::Dependency
|
61
61
|
name: guard-rspec
|
62
|
-
requirement: &
|
62
|
+
requirement: &70204201991240 !ruby/object:Gem::Requirement
|
63
63
|
none: false
|
64
64
|
requirements:
|
65
65
|
- - ~>
|
@@ -67,10 +67,10 @@ dependencies:
|
|
67
67
|
version: '0.4'
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
|
-
version_requirements: *
|
70
|
+
version_requirements: *70204201991240
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
72
|
name: growl
|
73
|
-
requirement: &
|
73
|
+
requirement: &70204201990780 !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
76
76
|
- - ~>
|
@@ -78,7 +78,7 @@ dependencies:
|
|
78
78
|
version: '1.0'
|
79
79
|
type: :development
|
80
80
|
prerelease: false
|
81
|
-
version_requirements: *
|
81
|
+
version_requirements: *70204201990780
|
82
82
|
description: ''
|
83
83
|
email:
|
84
84
|
- victorcrodrigues@gmail.com
|
@@ -94,6 +94,9 @@ files:
|
|
94
94
|
- README.md
|
95
95
|
- Rakefile
|
96
96
|
- examples/all.rb
|
97
|
+
- examples/belongs_to/resource_id_from_source.rb
|
98
|
+
- examples/belongs_to/resource_id_options.rb
|
99
|
+
- examples/belongs_to/simple.rb
|
97
100
|
- examples/embeds_many/invisible.rb
|
98
101
|
- examples/embeds_many/simple.rb
|
99
102
|
- examples/embeds_many/with_array.rb
|
@@ -177,6 +180,7 @@ files:
|
|
177
180
|
- lib/rest_model/version.rb
|
178
181
|
- rest_model.gemspec
|
179
182
|
- spec/.DS_Store
|
183
|
+
- spec/integration/belongs_to_spec.rb
|
180
184
|
- spec/integration/embeds_many_spec.rb
|
181
185
|
- spec/integration/embeds_one_spec.rb
|
182
186
|
- spec/integration/has_many_spec.rb
|
@@ -237,6 +241,7 @@ signing_key:
|
|
237
241
|
specification_version: 3
|
238
242
|
summary: ''
|
239
243
|
test_files:
|
244
|
+
- spec/integration/belongs_to_spec.rb
|
240
245
|
- spec/integration/embeds_many_spec.rb
|
241
246
|
- spec/integration/embeds_one_spec.rb
|
242
247
|
- spec/integration/has_many_spec.rb
|