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.
@@ -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)
@@ -7,6 +7,7 @@ class RestModel
7
7
 
8
8
  def key(key)
9
9
  fail "you can't define a key with name '#{key.name}'" if not_allowed?(key)
10
+
10
11
  key.model = self
11
12
  attr_accessor key.name
12
13
  self.keys << key
@@ -18,6 +18,7 @@ class RestModel
18
18
  def belongs_to(name, options = {})
19
19
  options.merge!(many: false, has: false)
20
20
  relation(name, options)
21
+ attr_accessor "#{name}_id"
21
22
  end
22
23
  end
23
24
  end
@@ -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
- id_key = parent.class.id_key.name
32
- has? ?
33
- "#{RestModel::Configuration.host}/#{parent.class.resource_name}/#{parent.send(id_key)}/#{relation_name(parent)}"
34
- : "#{RestModel::Configuration.host}/#{relation_name(parent).to_s.pluralize}/#{parent.send(id_key)}/#{parent.class.resource_name}"
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
@@ -21,6 +21,5 @@ class RestModel
21
21
  end
22
22
 
23
23
  alias :parse :from_source
24
-
25
24
  end
26
25
  end
@@ -11,18 +11,18 @@ class RestModel
11
11
  end
12
12
 
13
13
  if root and self.class.relations.any? and !options[:summarize]
14
- resource.merge!({link: link})
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 link
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)
@@ -1,3 +1,3 @@
1
1
  class RestModel
2
- VERSION = "0.1.11"
2
+ VERSION = "0.1.12"
3
3
  end
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 link)
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/200/example_children"}}
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.11
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-10 00:00:00.000000000Z
14
+ date: 2011-11-24 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
18
- requirement: &70225631314800 !ruby/object:Gem::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: *70225631314800
26
+ version_requirements: *70204201993120
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: i18n
29
- requirement: &70225631314220 !ruby/object:Gem::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: *70225631314220
37
+ version_requirements: *70204201992620
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rspec
40
- requirement: &70225631313740 !ruby/object:Gem::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: *70225631313740
48
+ version_requirements: *70204201992160
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: guard
51
- requirement: &70225631313260 !ruby/object:Gem::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: *70225631313260
59
+ version_requirements: *70204201991700
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: guard-rspec
62
- requirement: &70225631312800 !ruby/object:Gem::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: *70225631312800
70
+ version_requirements: *70204201991240
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: growl
73
- requirement: &70225631312340 !ruby/object:Gem::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: *70225631312340
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