acfs 0.22.2.b194 → 0.22.2.b196

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDBlODUyYTBjYWJmOTJmYTczYjVjNTc5YTc3OTkzYjc3YTQ5NWE1NA==
4
+ YjNkYThhNTU4ZGM5MWFiOWQyNDRlY2MxYmU2ZGRmMzIxYzhjZDQ1Zg==
5
5
  data.tar.gz: !binary |-
6
- NzZiYTA2NGNhZWViZGM2NzY1YTcxYWYzZGNiNmZlYmU3YWM0ZTE1Yg==
6
+ MDFmNGVlYzg1YThiMzljMDMwZGE5MmQzZDc3ZDhlOTgxMDhmZDVlZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- OWUwYjdjMjVkMzMwY2Q4MGY1YWU2YmJiNWMxNDY3MDRkYWFkNWNhYzkwMGMx
10
- NjZiZDZkYzg0NWM0ZTk1NWI2ZDg1ZmIwNmZkM2Q0NDUxZDYwMWFhMDM1NjMz
11
- ZDdlNzU1ODEzMWI0ZDAxZmM5ZDQ5ZmNlN2M4ODFmYjY2MzlhMzU=
9
+ MjViYjlhZmIwYjUxOTZjZTc2NTgxNmQwMDNhOTczZWVkMGFlMzgyZTEyM2Ni
10
+ OTNiODg3NDA1NmE3MjNiNDViNGYzOTcxZTQ5NDc5Y2I5NTk5YWVkMTZhNGJh
11
+ ODM1NGNlNGNhNDEyMTBmMTlhZjJkNzY1N2Y3YTYyMmY2YWMxMjg=
12
12
  data.tar.gz: !binary |-
13
- YmNlOWU1YzkyNGNjZDRmMmI2ZWIzNDMwN2RhMWU0M2FkNTZmNjFjMjFkNzUy
14
- ZWI5MmQ0MWZhM2YwYzY5MmZjYmRhZmViYzViYmI5OWRhN2ExZDc3Njc5NWE5
15
- MGRiYmNjYmRkZGQ0YzcxNjljZmY2MzM2ODU1YjdkNDkxYjE5NmY=
13
+ OTllM2RlYThiNjUzYzNlYTIwYTJmZDcwNDQ1MzI4MTQyMWJmNTY1ZWQ0ZjM1
14
+ MDdiMWQ0YzZhMzlmMmUyZDQ4MDk2ZWRiMTBiOTQ1YTVmNTE1OTA4YzcwNmM3
15
+ MTRlZjE1MDMyZmMzNGRmODdjOGU0YmVlNzY4MDIzN2Y5ZjY2Zjk=
@@ -86,5 +86,20 @@ module Acfs
86
86
  end
87
87
  end
88
88
 
89
+ # Gets raised if ressource type is no valid subclass of
90
+ # parent resource. Check if the type is set to the correct
91
+ # Acfs::Resource Name
92
+ class RessourceTypeError < Error
93
+ attr_reader :base_class
94
+ attr_reader :type_name
95
+
96
+ def initialize(opts = {})
97
+ @base_class = opts.delete :base_class
98
+ @type_name = opts.delete :type_name
99
+ opts[:message] = "Recieved ressource type #{type_name} is no subclass of #{base_class}"
100
+ super
101
+ end
102
+ end
103
+
89
104
  class RealRequestsNotAllowedError < StandardError; end
90
105
  end
@@ -76,10 +76,7 @@ module Acfs::Model
76
76
 
77
77
  operation :list, params: params do |data|
78
78
  data.each do |obj|
79
- collection << self.new.tap do |m|
80
- m.attributes = obj
81
- m.loaded!
82
- end
79
+ collection << create_resource(obj)
83
80
  end
84
81
  collection.loaded!
85
82
  block.call collection unless block.nil?
@@ -91,14 +88,13 @@ module Acfs::Model
91
88
 
92
89
  private
93
90
  def find_single(id, opts, &block)
94
- model = self.new
91
+ model = SimpleDelegator.new self.new
95
92
 
96
93
  opts[:params] ||= {}
97
94
  opts[:params].merge!({ id: id })
98
95
 
99
96
  operation :read, opts do |data|
100
- model.attributes = data
101
- model.loaded!
97
+ model.__setobj__ create_resource data, origin: model.__getobj__
102
98
  block.call model unless block.nil?
103
99
  end
104
100
 
@@ -119,6 +115,23 @@ module Acfs::Model
119
115
  end
120
116
  end
121
117
  end
118
+
119
+ def create_resource(data, opts = {})
120
+ type = data.delete 'type'
121
+ klass = resource_class_lookup(type)
122
+ (opts[:origin].is_a?(klass) ? opts[:origin] : klass.new).tap do |m|
123
+ m.attributes = data
124
+ m.loaded!
125
+ end
126
+ end
127
+
128
+ def resource_class_lookup(type)
129
+ return self if type.nil?
130
+ klass = type.camelize.constantize
131
+ raise Acfs::RessourceTypeError, type_name: type, base_class: self unless klass <= self
132
+ klass
133
+ end
134
+
122
135
  end
123
136
  end
124
137
  end
@@ -113,7 +113,7 @@ describe Acfs::Model::Persistence do
113
113
 
114
114
  it 'should be frozen after DELETE' do
115
115
  model.delete!
116
- expect(model).to be_frozen
116
+ expect(model.__getobj__).to be_frozen
117
117
  end
118
118
  end
119
119
 
@@ -127,12 +127,12 @@ describe Acfs::Model::Persistence do
127
127
  end
128
128
 
129
129
  it 'should save resource' do
130
- expect(model).to receive(:save).with({})
130
+ expect(model.__getobj__).to receive(:save).with({})
131
131
  model.update_attributes name: 'Idefix'
132
132
  end
133
133
 
134
134
  it 'should pass second hash to save' do
135
- expect(model).to receive(:save).with({ bla: 'blub' })
135
+ expect(model.__getobj__).to receive(:save).with({ bla: 'blub' })
136
136
  model.update_attributes({ name: 'Idefix' }, { bla: 'blub' })
137
137
  end
138
138
  end
@@ -147,12 +147,12 @@ describe Acfs::Model::Persistence do
147
147
  end
148
148
 
149
149
  it 'should save resource' do
150
- expect(model).to receive(:save!).with({})
150
+ expect(model.__getobj__).to receive(:save!).with({})
151
151
  model.update_attributes! name: 'Idefix'
152
152
  end
153
153
 
154
154
  it 'should pass second hash to save' do
155
- expect(model).to receive(:save!).with({ bla: 'blub' })
155
+ expect(model.__getobj__).to receive(:save!).with({ bla: 'blub' })
156
156
  model.update_attributes!({ name: 'Idefix' }, { bla: 'blub' })
157
157
  end
158
158
  end
@@ -20,7 +20,7 @@ describe Acfs::Model::QueryMethods do
20
20
  it 'should invoke callback after model is loaded' do
21
21
  proc = Proc.new { }
22
22
  proc.should_receive(:call) do |user|
23
- expect(user).to be === @user
23
+ expect(user.__getobj__).to equal @user.__getobj__
24
24
  expect(user).to be_loaded
25
25
  end
26
26
 
@@ -100,4 +100,40 @@ describe Acfs::Model::QueryMethods do
100
100
  end
101
101
  end
102
102
  end
103
+
104
+ describe '.all' do
105
+ let(:computer) { Computer }
106
+ let(:pc) { PC }
107
+ let(:mac) { Mac }
108
+ before do
109
+ stub_request(:get, 'http://computers.example.org/computers').to_return response([{ id: 1, type: 'PC' }, { id: 2, type: 'Computer' }, { id: 3, type: 'Mac' }])
110
+ end
111
+
112
+ context 'with resource type inheritance' do
113
+ it 'should create appropriate subclass resources' do
114
+ @computers = Computer.all
115
+
116
+ expect(@computers).to_not be_loaded
117
+
118
+ Acfs.run
119
+
120
+ expect(@computers).to be_loaded
121
+ expect(@computers).to have(3).items
122
+ expect(@computers[0]).to be_a PC
123
+ expect(@computers[1]).to be_a Computer
124
+ expect(@computers[2]).to be_a Mac
125
+ end
126
+
127
+ context 'with invalid type set' do
128
+ before do
129
+ stub_request(:get, 'http://computers.example.org/computers').to_return response([{ id: 1, type: 'MyUser' }, { id: 2, type: 'Computer' }, { id: 3, type: 'Mac' }])
130
+ end
131
+
132
+ it 'should raise error if type is no subclass' do
133
+ Computer.all
134
+ expect { Acfs.run }.to raise_error(Acfs::RessourceTypeError)
135
+ end
136
+ end
137
+ end
138
+ end
103
139
  end
@@ -1,6 +1,7 @@
1
1
 
2
2
  Acfs.configure do
3
3
  locate :user_service, 'http://users.example.org'
4
+ locate :computer_service, 'http://computers.example.org'
4
5
  locate :comments, 'http://comments.example.org'
5
6
  end
6
7
 
@@ -47,6 +48,26 @@ class Comment < Acfs::Resource
47
48
  attribute :text, :string
48
49
  end
49
50
 
51
+ class ComputerService < Acfs::Service
52
+ use Acfs::Middleware::MessagePackDecoder
53
+ use Acfs::Middleware::JsonDecoder
54
+ use Acfs::Middleware::JsonEncoder
55
+ end
56
+
57
+ class Computer < Acfs::Resource
58
+ service ComputerService, path: 'computers'
59
+
60
+ attribute :id, :integer
61
+ end
62
+
63
+ class PC < Computer
64
+
65
+ end
66
+
67
+ class Mac < Computer
68
+
69
+ end
70
+
50
71
  # DRAFT: Singular resource
51
72
  #class Singular < Acfs::Resource
52
73
  # service UserService, singular: true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.22.2.b194
4
+ version: 0.22.2.b196
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-25 00:00:00.000000000 Z
11
+ date: 2013-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport