rest_model 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -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, href: proc {"http://external.service.com/customers/#{customer_id}"}
11
+ end
12
+
13
+ @service = Service.new(id: 123, customer_id: 999)
14
+
15
+ inspect_rest_model(@service)
@@ -0,0 +1,16 @@
1
+ $:.push 'examples'; require 'helper'
2
+
3
+ class Service < RestModel
4
+ property :name
5
+ belongs_to :customer
6
+ end
7
+
8
+ class Customer < RestModel
9
+ id field: :customer_id, type: Integer
10
+ property :login
11
+ has_many :services, href: proc {"http://external.service.com/customers/#{id}/services"}
12
+ end
13
+
14
+ @root = Customer.from_source!({customer_id: 123, login: 'jackiechan2010'}).first
15
+
16
+ inspect_rest_model(@root)
@@ -0,0 +1,16 @@
1
+ $:.push 'examples'; require 'helper'
2
+
3
+ class Billing < RestModel
4
+ property :login
5
+ belongs_to :customer
6
+ end
7
+
8
+ class Customer < RestModel
9
+ id field: :customer_id, type: Integer
10
+ property :login
11
+ has_one :billing, href: proc {"http://external.service.com/customers/#{id}/billing"}
12
+ end
13
+
14
+ @root = Customer.from_source!({customer_id: 123, login: 'jackiechan2010'}).first
15
+
16
+ inspect_rest_model(@root)
@@ -0,0 +1,16 @@
1
+ $:.push 'examples'; require 'helper'
2
+
3
+ class Billing < RestModel
4
+ property :login
5
+ belongs_to :customer
6
+ end
7
+
8
+ class Customer < RestModel
9
+ id field: :customer_id, type: Integer
10
+ property :login
11
+ has_one :billing
12
+ end
13
+
14
+ @root = Customer.from_source!({customer_id: 123, login: 'jackiechan2010'}).first
15
+
16
+ inspect_rest_model(@root)
@@ -28,10 +28,14 @@ class RestModel
28
28
  end
29
29
 
30
30
  def href(parent)
31
- uri = has? ? "#{parent.class.resource_name}/#{resource_id(parent)}/#{relation_name(parent)}"
32
- : "#{relation_name(parent).to_s.pluralize}/#{resource_id(parent)}"
31
+ if options[:href]
32
+ parent.instance_eval(&options[:href])
33
+ else
34
+ host = "#{RestModel::Configuration.host}"
33
35
 
34
- "#{RestModel::Configuration.host}/#{uri}"
36
+ has? ? "#{host}/#{parent.class.resource_name}/#{resource_id(parent)}/#{relation_name(parent)}"
37
+ : "#{host}/#{relation_name(parent).to_s.pluralize}/#{resource_id(parent)}"
38
+ end
35
39
  end
36
40
 
37
41
  def resource_id(parent)
@@ -1,3 +1,3 @@
1
1
  class RestModel
2
- VERSION = "0.1.13"
2
+ VERSION = "0.1.14"
3
3
  end
@@ -49,7 +49,7 @@ describe "belongs_to" do
49
49
  end
50
50
  end
51
51
 
52
- describe_example "belongs_to/resource_id_from_source", wip: true do
52
+ describe_example "belongs_to/resource_id_from_source" do
53
53
  it 'parses properly' do
54
54
  @service.id.should == 123
55
55
  @service.customer_id.should == 999
@@ -72,4 +72,28 @@ describe "belongs_to" do
72
72
  end
73
73
  end
74
74
  end
75
+
76
+ describe_example "belongs_to/href_options" do
77
+ it 'parses properly' do
78
+ @service.id.should == 123
79
+ @service.customer_id.should == 999
80
+ end
81
+
82
+ describe "#resource" do
83
+ subject {@service.resource}
84
+
85
+ it 'has a link' do
86
+ subject[:link].should be_an(Array)
87
+ end
88
+
89
+ it "has a customer relation" do
90
+ subject[:link].any? {|l| l[:rel] == :customer}.should be_true
91
+ end
92
+
93
+ it "has a href for customer relation" do
94
+ link = subject[:link].find {|l| l[:rel] == :customer}
95
+ link[:href].should =~ %r{http://external.service.com/customers/999}
96
+ end
97
+ end
98
+ end
75
99
  end
@@ -26,4 +26,28 @@ describe "has_many" do
26
26
  end
27
27
  end
28
28
  end
29
+
30
+ describe_example "has_many/href_options" do
31
+ it 'parses properly' do
32
+ @root.id.should == 123
33
+ @root.login.should == 'jackiechan2010'
34
+ end
35
+
36
+ describe "#resource" do
37
+ subject {@root.resource}
38
+
39
+ it 'has a link' do
40
+ subject[:link].should be_an(Array)
41
+ end
42
+
43
+ it "has a services relation" do
44
+ subject[:link].any? {|l| l[:rel] == :services}.should be_true
45
+ end
46
+
47
+ it "has a href for services relation" do
48
+ link = subject[:link].find {|l| l[:rel] == :services}
49
+ link[:href].should =~ %r{http://external.service.com/customers/123/services}
50
+ end
51
+ end
52
+ end
29
53
  end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe "has_many" do
4
+ describe_example "has_one/simple" do
5
+ it 'parses properly' do
6
+ @root.id.should == 123
7
+ @root.login.should == 'jackiechan2010'
8
+ end
9
+
10
+ describe "#resource" do
11
+ subject {@root.resource}
12
+
13
+ it 'has a link' do
14
+ subject[:link].should be_an(Array)
15
+ end
16
+
17
+ it "has a billing relation" do
18
+ subject[:link].any? {|l| l[:rel] == :billing}.should be_true
19
+ end
20
+
21
+ it "has a href for billing relation" do
22
+ link = subject[:link].find {|l| l[:rel] == :billing}
23
+ link[:href].should =~ %r{/customers/123/billing}
24
+ end
25
+ end
26
+ end
27
+
28
+ describe_example "has_one/href_options" do
29
+ it 'parses properly' do
30
+ @root.id.should == 123
31
+ @root.login.should == 'jackiechan2010'
32
+ end
33
+
34
+ describe "#resource" do
35
+ subject {@root.resource}
36
+
37
+ it 'has a link' do
38
+ subject[:link].should be_an(Array)
39
+ end
40
+
41
+ it "has a billing relation" do
42
+ subject[:link].any? {|l| l[:rel] == :billing}.should be_true
43
+ end
44
+
45
+ it "has a href for billing relation" do
46
+ link = subject[:link].find {|l| l[:rel] == :billing}
47
+ link[:href].should =~ %r{http://external.service.com/customers/123/billing}
48
+ end
49
+ end
50
+ end
51
+ 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.13
4
+ version: 0.1.14
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-24 00:00:00.000000000Z
14
+ date: 2011-11-26 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
18
- requirement: &70126490239840 !ruby/object:Gem::Requirement
18
+ requirement: &70289539178140 !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: *70126490239840
26
+ version_requirements: *70289539178140
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: i18n
29
- requirement: &70126490239340 !ruby/object:Gem::Requirement
29
+ requirement: &70289539177640 !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: *70126490239340
37
+ version_requirements: *70289539177640
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rspec
40
- requirement: &70126490238880 !ruby/object:Gem::Requirement
40
+ requirement: &70289539177180 !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: *70126490238880
48
+ version_requirements: *70289539177180
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: guard
51
- requirement: &70126490238420 !ruby/object:Gem::Requirement
51
+ requirement: &70289539176720 !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: *70126490238420
59
+ version_requirements: *70289539176720
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: guard-rspec
62
- requirement: &70126490237960 !ruby/object:Gem::Requirement
62
+ requirement: &70289539176260 !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: *70126490237960
70
+ version_requirements: *70289539176260
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: growl
73
- requirement: &70126490237500 !ruby/object:Gem::Requirement
73
+ requirement: &70289535995500 !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: *70126490237500
81
+ version_requirements: *70289535995500
82
82
  description: ''
83
83
  email:
84
84
  - victorcrodrigues@gmail.com
@@ -94,6 +94,7 @@ files:
94
94
  - README.md
95
95
  - Rakefile
96
96
  - examples/all.rb
97
+ - examples/belongs_to/href_options.rb
97
98
  - examples/belongs_to/resource_id_from_source.rb
98
99
  - examples/belongs_to/resource_id_options.rb
99
100
  - examples/belongs_to/simple.rb
@@ -110,7 +111,10 @@ files:
110
111
  - examples/embeds_one/with_class_name.rb
111
112
  - examples/embeds_one/with_if.rb
112
113
  - examples/embeds_one/with_start_key.rb
114
+ - examples/has_many/href_options.rb
113
115
  - examples/has_many/simple.rb
116
+ - examples/has_one/href_options.rb
117
+ - examples/has_one/simple.rb
114
118
  - examples/helper.rb
115
119
  - examples/initialize/embeds_many.rb
116
120
  - examples/initialize/embeds_many_array.rb
@@ -184,6 +188,7 @@ files:
184
188
  - spec/integration/embeds_many_spec.rb
185
189
  - spec/integration/embeds_one_spec.rb
186
190
  - spec/integration/has_many_spec.rb
191
+ - spec/integration/has_one_spec.rb
187
192
  - spec/integration/property_spec.rb
188
193
  - spec/integration/summarization_spec.rb
189
194
  - spec/integration/to_source_spec.rb
@@ -245,6 +250,7 @@ test_files:
245
250
  - spec/integration/embeds_many_spec.rb
246
251
  - spec/integration/embeds_one_spec.rb
247
252
  - spec/integration/has_many_spec.rb
253
+ - spec/integration/has_one_spec.rb
248
254
  - spec/integration/property_spec.rb
249
255
  - spec/integration/summarization_spec.rb
250
256
  - spec/integration/to_source_spec.rb