ares-ext 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile.lock +10 -2
- data/Rakefile +1 -0
- data/active_resource_extensions.gemspec +5 -3
- data/lib/active_resource_extensions/resource_with_schema.rb +23 -23
- data/lib/active_resource_extensions/searchable_resource/search.rb +30 -8
- data/spec/active_resource_extensions/resource_with_schema_spec.rb +25 -11
- data/spec/active_resource_extensions/searchable_resource_spec/collection_spec.rb +11 -0
- data/spec/active_resource_extensions/searchable_resource_spec/search_spec.rb +86 -3
- metadata +37 -7
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
|
4
|
+
ares-ext (0.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
@@ -9,12 +9,20 @@ GEM
|
|
9
9
|
activeresource (2.3.5)
|
10
10
|
activesupport (= 2.3.5)
|
11
11
|
activesupport (2.3.5)
|
12
|
+
addressable (2.2.8)
|
13
|
+
crack (0.3.1)
|
14
|
+
rcov (1.0.0)
|
12
15
|
rspec (1.3.2)
|
16
|
+
webmock (1.7.6)
|
17
|
+
addressable (~> 2.2, > 2.2.5)
|
18
|
+
crack (>= 0.1.7)
|
13
19
|
|
14
20
|
PLATFORMS
|
15
21
|
ruby
|
16
22
|
|
17
23
|
DEPENDENCIES
|
18
|
-
active_resource_extensions!
|
19
24
|
activeresource (= 2.3.5)
|
25
|
+
ares-ext!
|
26
|
+
rcov
|
20
27
|
rspec (~> 1.3.1)
|
28
|
+
webmock (= 1.7.6)
|
data/Rakefile
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
|
-
gem.authors = ["
|
4
|
+
gem.authors = ["Benoit Dinocourt"]
|
5
5
|
gem.email = ["ghrind@gmail.com"]
|
6
|
-
gem.description = "Make activeresource
|
6
|
+
gem.description = "Make activeresource models compatible with will_paginate and searchlogic helpers, and add a schema feature"
|
7
7
|
gem.summary = gem.description
|
8
8
|
gem.homepage = "http://github.com/Ghrind/active_resource_extensions"
|
9
9
|
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
12
|
gem.name = "ares-ext"
|
13
13
|
gem.require_paths = ["lib"]
|
14
|
-
gem.version = '0.0.
|
14
|
+
gem.version = '0.0.3'
|
15
15
|
|
16
16
|
#gem.extra_rdoc_files = [
|
17
17
|
# "LICENSE",
|
@@ -21,4 +21,6 @@ Gem::Specification.new do |gem|
|
|
21
21
|
|
22
22
|
gem.add_development_dependency 'rspec', '~> 1.3.1'
|
23
23
|
gem.add_development_dependency 'activeresource', '2.3.5'
|
24
|
+
gem.add_development_dependency 'rcov'
|
25
|
+
gem.add_development_dependency 'webmock', '1.7.6'
|
24
26
|
end
|
@@ -20,15 +20,15 @@ module ActiveResourceExtensions
|
|
20
20
|
protected
|
21
21
|
|
22
22
|
def schema
|
23
|
-
@schema ||=
|
23
|
+
@schema ||= remote_schema
|
24
24
|
end
|
25
25
|
|
26
26
|
def schema= new_schema
|
27
27
|
@schema = new_schema
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
|
30
|
+
def remote_schema
|
31
|
+
get( :schema )
|
32
32
|
end
|
33
33
|
|
34
34
|
end
|
@@ -38,27 +38,27 @@ module ActiveResourceExtensions
|
|
38
38
|
if self.class.is_attribute? name
|
39
39
|
value = attributes[name.to_s]
|
40
40
|
return if value.nil?
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
rescue ArgumentError, NameError
|
57
|
-
raise ArgumentError, "Can't cast #{value.inspect} into #{self.class.attribute_type( name )}"
|
58
|
-
end
|
59
|
-
end
|
41
|
+
case self.class.attribute_type name
|
42
|
+
when 'Time'
|
43
|
+
Time.parse value
|
44
|
+
when 'Date'
|
45
|
+
Date.parse value
|
46
|
+
when 'Integer'
|
47
|
+
value.to_i
|
48
|
+
when 'Float'
|
49
|
+
value.to_f
|
50
|
+
when 'Boolean'
|
51
|
+
[true, 1, '1'].include? value
|
52
|
+
when 'ObjectId'
|
53
|
+
value.to_s
|
54
|
+
when ''
|
55
|
+
value
|
60
56
|
else
|
61
|
-
|
57
|
+
begin
|
58
|
+
self.class.attribute_type( name ).constantize.new value
|
59
|
+
rescue ArgumentError, NameError
|
60
|
+
raise ArgumentError, "Can't cast #{value.inspect} into #{self.class.attribute_type( name )}"
|
61
|
+
end
|
62
62
|
end
|
63
63
|
else
|
64
64
|
super name, *args, &block
|
@@ -9,9 +9,24 @@ module ActiveResourceExtensions
|
|
9
9
|
|
10
10
|
attr_reader :conditions
|
11
11
|
|
12
|
-
def initialize model, conditions =
|
12
|
+
def initialize model, conditions = nil
|
13
13
|
@model = model
|
14
|
-
@conditions = conditions
|
14
|
+
@conditions = conditions || {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def order
|
18
|
+
@conditions[:order]
|
19
|
+
end
|
20
|
+
|
21
|
+
def order= new_order
|
22
|
+
@conditions[:order] = new_order
|
23
|
+
end
|
24
|
+
|
25
|
+
# Return all records matching conditions.
|
26
|
+
#
|
27
|
+
# @return [Array<ActiveResource::Base>] All records matching conditions.
|
28
|
+
def all
|
29
|
+
@model.find( :all, :params => parse_conditions( @conditions ) )
|
15
30
|
end
|
16
31
|
|
17
32
|
def paginate pagination_options = {}
|
@@ -38,18 +53,25 @@ module ActiveResourceExtensions
|
|
38
53
|
params
|
39
54
|
end
|
40
55
|
|
41
|
-
|
56
|
+
CONDITIONS_SUFFIXES = %w(
|
57
|
+
_greater_than
|
58
|
+
_less_than
|
59
|
+
_like
|
60
|
+
)
|
61
|
+
|
42
62
|
def method_missing(name, *args, &block)
|
43
63
|
name.to_s =~ /^([\w]*)(=?)$/
|
44
|
-
|
64
|
+
condition_name = $1.to_sym
|
65
|
+
operator = $2
|
66
|
+
attribute_name = condition_name.to_s.sub(/(#{CONDITIONS_SUFFIXES.join('|')})$/, '').to_sym
|
45
67
|
|
46
68
|
ignore_attribute = ! @model.respond_to?( :is_attribute? )
|
47
69
|
|
48
|
-
if ignore_attribute or @model.is_attribute?(
|
49
|
-
if
|
50
|
-
@conditions[
|
70
|
+
if ignore_attribute or @model.is_attribute?( attribute_name )
|
71
|
+
if operator == '='
|
72
|
+
@conditions[condition_name]= args.first
|
51
73
|
else
|
52
|
-
@conditions[
|
74
|
+
@conditions[condition_name]
|
53
75
|
end
|
54
76
|
else
|
55
77
|
super name, *args, &block
|
@@ -19,8 +19,22 @@ describe ActiveResourceExtensions::ResourceWithSchema do
|
|
19
19
|
Person.respond_to?( :schema ).should be_true
|
20
20
|
end
|
21
21
|
|
22
|
-
it "should have
|
23
|
-
Person.
|
22
|
+
it "should not have a schema" do
|
23
|
+
Person.instance_variable_get( :@schema ).should be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '::schema' do
|
27
|
+
it "should load remote schema" do
|
28
|
+
schema = { 'name' => 'String', 'age' => 'Integer' }
|
29
|
+
Person.should_receive( :get ).with( :schema ).and_return( schema )
|
30
|
+
Person.send( :schema ).should == schema
|
31
|
+
end
|
32
|
+
it "should memoize schema" do
|
33
|
+
schema = { 'name' => 'String', 'age' => 'Integer' }
|
34
|
+
Person.should_receive( :get ).with( :schema ).once.and_return( schema )
|
35
|
+
Person.send( :schema ).should == schema
|
36
|
+
Person.send( :schema ).should == schema
|
37
|
+
end
|
24
38
|
end
|
25
39
|
|
26
40
|
it "should replace schema" do
|
@@ -29,17 +43,11 @@ describe ActiveResourceExtensions::ResourceWithSchema do
|
|
29
43
|
Person.send( :schema ).should == schema
|
30
44
|
end
|
31
45
|
|
32
|
-
describe '::
|
46
|
+
describe '::remote_schema' do
|
33
47
|
it "should use active_resource get method" do
|
34
|
-
Person.should_receive( :get ).with( :schema )
|
35
|
-
Person.send :load_remote_schema
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should load schema from response" do
|
39
48
|
schema = { 'name' => 'String', 'age' => 'Integer' }
|
40
49
|
Person.should_receive( :get ).with( :schema ).and_return( schema )
|
41
|
-
Person.send :
|
42
|
-
Person.send( :schema ).should == schema
|
50
|
+
Person.send( :remote_schema ).should == schema
|
43
51
|
end
|
44
52
|
end
|
45
53
|
|
@@ -88,10 +96,12 @@ describe ActiveResourceExtensions::ResourceWithSchema do
|
|
88
96
|
'name' => 'String',
|
89
97
|
'age' => 'Integer',
|
90
98
|
'created_at' => 'Time',
|
99
|
+
'birth_date' => 'Date',
|
91
100
|
'is_musician' => 'Boolean',
|
92
101
|
'height' => 'Float',
|
93
102
|
'unknown_1' => 'Company',
|
94
|
-
'unknown_2' => ''
|
103
|
+
'unknown_2' => '',
|
104
|
+
'company_id' => 'ObjectId'
|
95
105
|
} )
|
96
106
|
end
|
97
107
|
|
@@ -127,10 +137,12 @@ describe ActiveResourceExtensions::ResourceWithSchema do
|
|
127
137
|
@person = Person.new(
|
128
138
|
:name => 'John',
|
129
139
|
:age => '26',
|
140
|
+
:birth_date => '1983-07-24',
|
130
141
|
:height => '5.3',
|
131
142
|
:is_musician => '1',
|
132
143
|
:created_at => '2012-05-01 23:15:12',
|
133
144
|
:unknown_1 => 'a',
|
145
|
+
:company_id => '4fr654r321',
|
134
146
|
:unknown_2 => 'b' )
|
135
147
|
end
|
136
148
|
|
@@ -140,6 +152,8 @@ describe ActiveResourceExtensions::ResourceWithSchema do
|
|
140
152
|
@person.height.should == 5.3
|
141
153
|
@person.is_musician.should == true
|
142
154
|
@person.created_at.should == Time.parse('2012-05-01 23:15:12')
|
155
|
+
@person.birth_date.should == Date.parse('1983-07-24')
|
156
|
+
@person.company_id.should == '4fr654r321'
|
143
157
|
|
144
158
|
for value in [ '0', 0, false, 'true' ]
|
145
159
|
person = Person.new( :is_musician => value )
|
@@ -6,6 +6,17 @@ describe ActiveResourceExtensions::SearchableResource::Collection do
|
|
6
6
|
@collection = ActiveResourceExtensions::SearchableResource::Collection.new [], 2, 10, 51
|
7
7
|
end
|
8
8
|
|
9
|
+
describe '.method_missing' do
|
10
|
+
it "should proxy every missing method to target" do
|
11
|
+
target = []
|
12
|
+
@collection = ActiveResourceExtensions::SearchableResource::Collection.new target, 2, 10, 51
|
13
|
+
for method_name in %w( class << [] ) do
|
14
|
+
target.should_receive( method_name.intern ).and_return nil
|
15
|
+
@collection.send method_name.intern
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
9
20
|
describe '.total_pages' do
|
10
21
|
it "should return correct page number" do
|
11
22
|
@collection.total_pages.should == 6
|
@@ -16,7 +16,39 @@ describe ActiveResourceExtensions::SearchableResource::Search do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
describe '.
|
19
|
+
describe '.new' do
|
20
|
+
it "should never set conditions to nil" do
|
21
|
+
search = ActiveResourceExtensions::SearchableResource::Search.new Person
|
22
|
+
search.conditions.should_not be_nil
|
23
|
+
|
24
|
+
search = ActiveResourceExtensions::SearchableResource::Search.new Person, nil
|
25
|
+
search.conditions.should_not be_nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#order' do
|
30
|
+
|
31
|
+
context "when class has a schema" do
|
32
|
+
|
33
|
+
before do
|
34
|
+
Person.send :include, ActiveResourceExtensions::ResourceWithSchema
|
35
|
+
Person.send :schema=, 'name' => 'String', 'age' => 'Integer'
|
36
|
+
@search = Person.search_for
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should set :order condition" do
|
40
|
+
@search.order = 'name'
|
41
|
+
@search.conditions[:order].should == 'name'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return :order condition" do
|
45
|
+
@search.instance_variable_set :@conditions, { :order => 'name' }
|
46
|
+
@search.order.should == 'name'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#method_missing' do
|
20
52
|
context "when class has a schema" do
|
21
53
|
|
22
54
|
before do
|
@@ -39,6 +71,36 @@ describe ActiveResourceExtensions::SearchableResource::Search do
|
|
39
71
|
|
40
72
|
end
|
41
73
|
|
74
|
+
context "when method name is a known attribute with a valid suffix" do
|
75
|
+
|
76
|
+
it "should set condition" do
|
77
|
+
for suffix in %w( _like _greater_than _less_than )
|
78
|
+
@search.send :"age#{suffix}=", 26
|
79
|
+
@search.conditions[:"age#{suffix}"].should == 26
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should get condition" do
|
84
|
+
for suffix in %w( _like _greater_than _less_than )
|
85
|
+
@search.instance_variable_set :@conditions, :"age#{suffix}" => 26
|
86
|
+
@search.send( :"age#{suffix}" ).should == 26
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
context "when method name is a known attribute with a valid suffix" do
|
93
|
+
it "should have default behavior" do
|
94
|
+
lambda do
|
95
|
+
@search.name_looks_like
|
96
|
+
end.should raise_error NoMethodError
|
97
|
+
|
98
|
+
lambda do
|
99
|
+
@search.name_looks_like= 'bob'
|
100
|
+
end.should raise_error NoMethodError
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
42
104
|
context "when method name is not an attribute" do
|
43
105
|
it "should have default behavior" do
|
44
106
|
lambda do
|
@@ -63,13 +125,34 @@ describe ActiveResourceExtensions::SearchableResource::Search do
|
|
63
125
|
end
|
64
126
|
end
|
65
127
|
|
66
|
-
describe '
|
128
|
+
describe '#parse_conditions' do
|
67
129
|
it "should remove empty conditions" do
|
68
130
|
@search.send( :parse_conditions, :name_like => 'john', :age => '' ).should == { :name_like => 'john' }
|
69
131
|
end
|
70
132
|
end
|
71
133
|
|
72
|
-
describe '
|
134
|
+
describe '#all' do
|
135
|
+
it "should parse conditions" do
|
136
|
+
@search.name_like = 'john'
|
137
|
+
@search.age = ''
|
138
|
+
|
139
|
+
@search.should_receive( :parse_conditions ).with( :name_like => 'john', :age => '' ).and_return :name_like => 'john'
|
140
|
+
|
141
|
+
Person.should_receive( :find ).with( :all, :params => { :name_like => 'john' } ).and_return []
|
142
|
+
|
143
|
+
@search.all
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should return an Array" do
|
147
|
+
|
148
|
+
@people = (1..2).map{ |i| Person.new :id => i }
|
149
|
+
|
150
|
+
Person.should_receive( :find ).with( :all, :params => { } ).and_return @people
|
151
|
+
@search.all.should == @people
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe '#paginate' do
|
73
156
|
|
74
157
|
it "should parse conditions" do
|
75
158
|
@search.name_like = 'john'
|
metadata
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ares-ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
|
-
-
|
13
|
+
- Benoit Dinocourt
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-06-11 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -50,7 +50,37 @@ dependencies:
|
|
50
50
|
version: 2.3.5
|
51
51
|
type: :development
|
52
52
|
version_requirements: *id002
|
53
|
-
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rcov
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: webmock
|
69
|
+
prerelease: false
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - "="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 7
|
76
|
+
segments:
|
77
|
+
- 1
|
78
|
+
- 7
|
79
|
+
- 6
|
80
|
+
version: 1.7.6
|
81
|
+
type: :development
|
82
|
+
version_requirements: *id004
|
83
|
+
description: Make activeresource models compatible with will_paginate and searchlogic helpers, and add a schema feature
|
54
84
|
email:
|
55
85
|
- ghrind@gmail.com
|
56
86
|
executables: []
|
@@ -109,7 +139,7 @@ rubyforge_project:
|
|
109
139
|
rubygems_version: 1.6.1
|
110
140
|
signing_key:
|
111
141
|
specification_version: 3
|
112
|
-
summary: Make activeresource
|
142
|
+
summary: Make activeresource models compatible with will_paginate and searchlogic helpers, and add a schema feature
|
113
143
|
test_files:
|
114
144
|
- spec/active_resource_extensions/resource_with_schema_spec.rb
|
115
145
|
- spec/active_resource_extensions/searchable_resource_spec.rb
|