logical_model 0.2.10 → 0.2.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -62,7 +62,7 @@ the database. We could change this by deleting all users in the test setup.
62
62
 
63
63
  To run spec:
64
64
 
65
- bundle exec rspec spec/client_spec.rb
65
+ bundle exec rspec spec/client_spec.rb --color
66
66
 
67
67
 
68
68
  == Contributing to logical_model
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.10
1
+ 0.2.11
@@ -62,12 +62,15 @@ class LogicalModel
62
62
  DEFAULT_TIMEOUT = 10000
63
63
 
64
64
  class << self
65
- attr_accessor :host, :hydra, :resource_path, :api_key, :api_key_name, :timeout, :use_ssl, :log_path, :use_api_key, :json_root
65
+ attr_accessor :host, :hydra, :resource_path, :api_key, :api_key_name, :timeout,
66
+ :use_ssl, :use_api_key, :enable_delete_multiple,
67
+ :json_root, :log_path
66
68
 
67
69
  def timeout; @timeout ||= DEFAULT_TIMEOUT; end
68
70
  def use_ssl; @use_ssl ||= false; end
69
71
  def log_path; @log_path ||= "log/logical_model.log"; end
70
72
  def use_api_key; @use_api_key ||= false; end
73
+ def delete_multiple_enabled?; @enable_delete_multiple ||= false; end
71
74
 
72
75
  def validates_associated(*associations)
73
76
  associations.each do |association|
@@ -249,8 +252,8 @@ class LogicalModel
249
252
  # Usage:
250
253
  # Person.async_paginate(:page => params[:page]){|i| result = i}
251
254
  def self.async_paginate(options={})
252
- page = options[:page] || 1
253
- per_page = options[:per_page] || 20
255
+ options[:page] ||= 1
256
+ options[:per_page] ||= 20
254
257
 
255
258
  options = self.merge_key(options)
256
259
 
@@ -266,8 +269,8 @@ class LogicalModel
266
269
  result_set[:collection],
267
270
  {
268
271
  :total_count=>result_set[:total],
269
- :limit => per_page,
270
- :offset => per_page * ([page, 1].max - 1)
272
+ :limit => options[:per_page],
273
+ :offset => options[:per_page] * ([options[:page], 1].max - 1)
271
274
  }
272
275
  )
273
276
 
@@ -292,6 +295,52 @@ class LogicalModel
292
295
  return nil
293
296
  end
294
297
 
298
+ # Asynchronic Count
299
+ # This count won't block excecution waiting for result, count will be enqueued in Objectr#hydra.
300
+ #
301
+ # Parameters:
302
+ # @param options [Hash].
303
+ # Valid options are:
304
+ # @option options [Integer] :page - indicated what page to return. Defaults to 1.
305
+ # @option options [Integer] :per_page - indicates how many records to be returned per page. Defauls to 20
306
+ # @option options [Hash] all other options will be forwarded in :params to WebService
307
+ #
308
+ # @example 'Count bobs'
309
+ # Person.async_count(:when => {:name => 'bob'}}){|i| result = i}
310
+ def self.async_count(options={})
311
+ options[:page] = 1
312
+ options[:per_page] = 1
313
+
314
+ options = self.merge_key(options)
315
+
316
+ request = Typhoeus::Request.new(resource_uri, :params => options)
317
+ request.on_complete do |response|
318
+ if response.code >= 200 && response.code < 400
319
+ log_ok(response)
320
+
321
+ result_set = self.from_json(response.body)
322
+
323
+ yield result_set[:total]
324
+ else
325
+ log_failed(response)
326
+ end
327
+ end
328
+ self.hydra.queue(request)
329
+ end
330
+
331
+ # synchronic count
332
+ def self.count(options={})
333
+ result = nil
334
+ async_count(options){|i| result = i}
335
+ Timeout::timeout(self.timeout/1000) do
336
+ self.hydra.run
337
+ end
338
+ result
339
+ rescue Timeout::Error
340
+ self.logger.warn("timeout")
341
+ return nil
342
+ end
343
+
295
344
  # Asynchronic Find
296
345
  # This find won't block excecution waiting for result, excecution will be enqueued in Objectr#hydra.
297
346
  #
@@ -471,7 +520,7 @@ class LogicalModel
471
520
  :timeout => self.timeout
472
521
  )
473
522
  end
474
- if response == 200
523
+ if response.code == 200
475
524
  log_ok(response)
476
525
  return self
477
526
  else
@@ -491,10 +540,47 @@ class LogicalModel
491
540
  self.class.delete(self.id,params)
492
541
  end
493
542
 
543
+ # Deletes all Objects matching given ids
544
+ #
545
+ # This method will make a DELETE request to resource_uri/destroy_multiple
546
+ #
547
+ # Returns nil if delete failed
548
+ #
549
+ # @param [Array] ids - ids of contacts to be deleted
550
+ # @param [Hash] params - other params to be sent to WS on request
551
+ #
552
+ # Usage:
553
+ # Person.delete_multiple([1,2,4,5,6])
554
+ def self.delete_multiple(ids, params={})
555
+ raise "not-enabled" unless self.delete_multiple_enabled?
556
+
557
+ params = self.merge_key(params)
558
+ params = params.merge({:ids => ids})
559
+
560
+ response = nil
561
+ Timeout::timeout(self.timeout/1000) do
562
+ response = Typhoeus::Request.delete( self.resource_uri+"/destroy_multiple",
563
+ :params => params,
564
+ :timeout => self.timeout
565
+ )
566
+ end
567
+ if response.code == 200
568
+ log_ok(response)
569
+ return self
570
+ else
571
+ log_failed(response)
572
+ return nil
573
+ end
574
+ rescue Timeout::Error
575
+ self.logger.warn "timeout"
576
+ return nil
577
+ end
578
+
494
579
  def persisted?
495
580
  false
496
581
  end
497
582
 
583
+
498
584
  # Returns true if a record has not been persisted yet.
499
585
  #
500
586
  # Usage:
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "logical_model"
8
- s.version = "0.2.10"
8
+ s.version = "0.2.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dwayne Macgowan"]
12
- s.date = "2012-01-09"
12
+ s.date = "2012-01-17"
13
13
  s.description = "LogicalModel allows to use a resource as a model. It is based on web presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3"
14
14
  s.email = "dwaynemac@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -1,5 +1,8 @@
1
1
  require File.dirname(__FILE__) + '/../client'
2
2
 
3
+ require File.dirname(__FILE__) + '/../test/typhoeus_mocks.rb'
4
+ include TyphoeusMocks
5
+
3
6
  # NOTE: to run these specs you must have the service running locally. Do like this:
4
7
  # ruby service.rb -p 3000 -e test
5
8
 
@@ -10,6 +13,7 @@ describe "LogicalModel User client" do
10
13
  describe "#create" do
11
14
  context "with valid attributes" do
12
15
  before(:each) do
16
+ # TODO mock service
13
17
  @user = User.new({:name => "paul",
14
18
  :email => "paul@pauldix.net",
15
19
  :password => "strongpass",
@@ -31,21 +35,12 @@ describe "LogicalModel User client" do
31
35
 
32
36
  describe "#paginate" do
33
37
  before do
34
- # --> Mock service
35
- req = Typhoeus::Request.any_instance
36
- response = mock(
37
- code: 200,
38
- body: {
39
- collection: [{name:'a',email:'a@m'},
40
- {name:'b',email:'b@m'},
41
- {name:'c',email:'c@m'}],
42
- total: 6
43
- }.to_json,
44
- request: mock(url:"mockedurl"),
45
- time: 1234
38
+ mock_index(
39
+ collection: [{name:'a',email:'a@m'},
40
+ {name:'b',email:'b@m'},
41
+ {name:'c',email:'c@m'}],
42
+ total: 6
46
43
  )
47
- req.stub(:on_complete).and_yield(response)
48
- # <-- service mocked
49
44
 
50
45
  @users = User.paginate(page:1, per_page:1)
51
46
  end
@@ -56,7 +51,44 @@ describe "LogicalModel User client" do
56
51
  @users.total_count.should == 6
57
52
  end
58
53
  end
59
-
54
+
55
+ describe "#count" do
56
+ before do
57
+ mock_index(
58
+ total: 6
59
+ )
60
+ end
61
+ let(:count){User.count}
62
+ it "should return a Integer" do
63
+ count.should be_a(Integer)
64
+ end
65
+ it "should return total amount of users" do
66
+ count.should == 6
67
+ end
68
+ end
69
+
70
+ describe "#find" do
71
+ context "if found" do
72
+ before do
73
+ mock_show(
74
+ attributes: {
75
+ id: 1,
76
+ name: 'mocked-username',
77
+ email: 'mocked@mail',
78
+ password: '1234',
79
+ bio: 'asdfasdf'
80
+ }
81
+ )
82
+
83
+ @user = User.find(1)
84
+ end
85
+ it "should set attributes" do
86
+ @user.id.should == 1
87
+ @user.email.should == "mocked@mail"
88
+ end
89
+ end
90
+ end
91
+
60
92
  describe "#https" do
61
93
  context "when use_ssl is tue" do
62
94
  before(:each) do
@@ -76,4 +108,50 @@ describe "LogicalModel User client" do
76
108
  end
77
109
  end
78
110
  end
111
+
112
+ describe "delete_multiple" do
113
+ before do
114
+ Typhoeus::Request.stub!(:delete).and_return(mock_response(body: 'ok'))
115
+ end
116
+ context "when disabled" do
117
+ it "should raise exception" do
118
+ class User < LogicalModel; self.enable_delete_multiple = false; end
119
+ expect{User.delete_multiple([1,2,4,5])}.to raise_error
120
+ end
121
+ end
122
+ context "when enabled" do
123
+ it "should raise exception" do
124
+ class User < LogicalModel; self.enable_delete_multiple = true; end
125
+ expect{User.delete_multiple([1,2,4,5])}.not_to raise_error
126
+ end
127
+ end
128
+ end
129
+
130
+ describe "#delete_multiple_enabled?" do
131
+ context "with config: 'enable_multiple_delete = true'" do
132
+ before do
133
+ class User < LogicalModel
134
+ self.enable_delete_multiple = true
135
+ end
136
+ end
137
+ it "should return true" do
138
+ User.delete_multiple_enabled?.should be_true
139
+ end
140
+ end
141
+ context "with config: 'enable_delete_multiple = false'" do
142
+ it "should return false" do
143
+ class User < LogicalModel
144
+ self.enable_delete_multiple = false
145
+ end
146
+ User.delete_multiple_enabled?.should be_false
147
+ end
148
+ end
149
+ context "without config" do
150
+ it "should default to false" do
151
+ class AnotherClass < LogicalModel
152
+ end
153
+ AnotherClass.delete_multiple_enabled?.should be_false
154
+ end
155
+ end
156
+ end
79
157
  end
@@ -1,5 +1,43 @@
1
1
  module TyphoeusMocks
2
2
 
3
+ # This stubs response of GET /resource
4
+ #
5
+ # @param [Hash] options
6
+ # @option options [Array<Hash>] :collection
7
+ # @option options [Integer] :total
8
+ # @option options [String] :url
9
+ # @option options [Integer] :status - HTTP status
10
+ def mock_index(options={})
11
+ req = Typhoeus::Request.any_instance
12
+ response = mock(
13
+ code: options[:status] || 200,
14
+ body: {
15
+ collection: options[:collection] || [],
16
+ total: options[:total] || 0
17
+ }.to_json,
18
+ request: mock(url: options[:url] || "mockedurl"),
19
+ time: 1234
20
+ )
21
+ req.stub(:on_complete).and_yield(response)
22
+ end
23
+
24
+ # This stubs response of GET /resource/:id
25
+ #
26
+ # @param [Hash] options
27
+ # @option options [Array<Hash>] :attributes
28
+ # @option options [String] :url
29
+ # @option options [Integer] :status - HTTP status
30
+ def mock_show(options={})
31
+ req = Typhoeus::Request.any_instance
32
+ response = mock(
33
+ code: options[:status] || 200,
34
+ body: options[:attributes].to_json,
35
+ request: mock(url: options[:url] || "mockedurl"),
36
+ time: 1234
37
+ )
38
+ req.stub(:on_complete).and_yield(response)
39
+ end
40
+
3
41
  # Mocks a Typhoeus::Response
4
42
  # @param [Hash] options
5
43
  # @option [Integer] code - http response code. default: 200
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logical_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-09 00:00:00.000000000Z
12
+ date: 2012-01-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &78627270 !ruby/object:Gem::Requirement
16
+ requirement: &79777970 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *78627270
24
+ version_requirements: *79777970
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &78626410 !ruby/object:Gem::Requirement
27
+ requirement: &79777350 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *78626410
35
+ version_requirements: *79777350
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: typhoeus
38
- requirement: &78624510 !ruby/object:Gem::Requirement
38
+ requirement: &79776880 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.2.4
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *78624510
46
+ version_requirements: *79776880
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: kaminari
49
- requirement: &78623980 !ruby/object:Gem::Requirement
49
+ requirement: &79776350 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.13.0
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *78623980
57
+ version_requirements: *79776350
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activerecord
60
- requirement: &78623670 !ruby/object:Gem::Requirement
60
+ requirement: &79775710 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *78623670
68
+ version_requirements: *79775710
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: shoulda
71
- requirement: &78623310 !ruby/object:Gem::Requirement
71
+ requirement: &79774980 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *78623310
79
+ version_requirements: *79774980
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: bundler
82
- requirement: &78622890 !ruby/object:Gem::Requirement
82
+ requirement: &79774320 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.0.10
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *78622890
90
+ version_requirements: *79774320
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: jeweler
93
- requirement: &78622410 !ruby/object:Gem::Requirement
93
+ requirement: &79773570 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: 1.6.4
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *78622410
101
+ version_requirements: *79773570
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rcov
104
- requirement: &78621770 !ruby/object:Gem::Requirement
104
+ requirement: &79772870 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *78621770
112
+ version_requirements: *79772870
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: sqlite3-ruby
115
- requirement: &78621440 !ruby/object:Gem::Requirement
115
+ requirement: &79743610 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *78621440
123
+ version_requirements: *79743610
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: sinatra
126
- requirement: &78621020 !ruby/object:Gem::Requirement
126
+ requirement: &79742900 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ~>
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: 1.2.6
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *78621020
134
+ version_requirements: *79742900
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: json
137
- requirement: &78599190 !ruby/object:Gem::Requirement
137
+ requirement: &79741620 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *78599190
145
+ version_requirements: *79741620
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: rspec-rails
148
- requirement: &78598610 !ruby/object:Gem::Requirement
148
+ requirement: &79740370 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,10 +153,10 @@ dependencies:
153
153
  version: '0'
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *78598610
156
+ version_requirements: *79740370
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: guard-rspec
159
- requirement: &78598040 !ruby/object:Gem::Requirement
159
+ requirement: &79739390 !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements:
162
162
  - - ! '>='
@@ -164,10 +164,10 @@ dependencies:
164
164
  version: '0'
165
165
  type: :development
166
166
  prerelease: false
167
- version_requirements: *78598040
167
+ version_requirements: *79739390
168
168
  - !ruby/object:Gem::Dependency
169
169
  name: rb-inotify
170
- requirement: &78597410 !ruby/object:Gem::Requirement
170
+ requirement: &79738470 !ruby/object:Gem::Requirement
171
171
  none: false
172
172
  requirements:
173
173
  - - ! '>='
@@ -175,10 +175,10 @@ dependencies:
175
175
  version: '0'
176
176
  type: :development
177
177
  prerelease: false
178
- version_requirements: *78597410
178
+ version_requirements: *79738470
179
179
  - !ruby/object:Gem::Dependency
180
180
  name: libnotify
181
- requirement: &78596950 !ruby/object:Gem::Requirement
181
+ requirement: &79737780 !ruby/object:Gem::Requirement
182
182
  none: false
183
183
  requirements:
184
184
  - - ! '>='
@@ -186,7 +186,7 @@ dependencies:
186
186
  version: '0'
187
187
  type: :development
188
188
  prerelease: false
189
- version_requirements: *78596950
189
+ version_requirements: *79737780
190
190
  description: LogicalModel allows to use a resource as a model. It is based on web
191
191
  presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3
192
192
  email: dwaynemac@gmail.com
@@ -234,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
234
234
  version: '0'
235
235
  segments:
236
236
  - 0
237
- hash: -162919699
237
+ hash: 788754387
238
238
  required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  none: false
240
240
  requirements: