veneer 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/..gemspec +21 -0
- data/.gitignore +2 -5
- data/Gemfile +4 -0
- data/README.md +171 -0
- data/Rakefile +3 -36
- data/lib/..rb +3 -0
- data/lib/veneer/adapters/active_record.rb +1 -1
- data/lib/veneer/adapters/activerecord/class_wrapper.rb +11 -79
- data/lib/veneer/adapters/activerecord/instance_wrapper.rb +3 -11
- data/lib/veneer/adapters/activerecord/pre_3.0_class_wrapper.rb +24 -0
- data/lib/veneer/adapters/activerecord/pre_3.0_instance_wrapper.rb +15 -0
- data/lib/veneer/adapters/activerecord.rb +9 -2
- data/lib/veneer/adapters/data_mapper.rb +1 -1
- data/lib/veneer/adapters/datamapper/class_wrapper.rb +29 -50
- data/lib/veneer/adapters/datamapper/instance_wrapper.rb +3 -14
- data/lib/veneer/adapters/mongomapper/class_wrapper.rb +2 -56
- data/lib/veneer/adapters/mongomapper.rb +2 -2
- data/lib/veneer/base/class_wrapper.rb +46 -11
- data/lib/veneer/base/instance_wrapper.rb +12 -14
- data/lib/veneer/core_ext/kernel.rb +0 -1
- data/lib/veneer/errors.rb +0 -1
- data/lib/veneer/lint.rb +183 -0
- data/lib/veneer/version.rb +3 -0
- data/lib/veneer.rb +1 -4
- data/test/test_helper.rb +4 -3
- data/test/veneer/adapters/{active_record/active_record_setup.rb → test_active_record.rb} +23 -1
- data/test/veneer/adapters/test_datamapper.rb +56 -0
- data/test/veneer/adapters/test_mongomapper.rb +49 -0
- data/test/veneer/proxy_test.rb +20 -45
- data/veneer.gemspec +15 -108
- metadata +40 -60
- data/README.textile +0 -142
- data/VERSION +0 -1
- data/datamapper_setup.rb +0 -2
- data/lib/veneer/base/conditional.rb +0 -77
- data/test/macros/class_wrapper/create_macro.rb +0 -54
- data/test/macros/class_wrapper/delete_macro.rb +0 -31
- data/test/macros/class_wrapper/find_macro.rb +0 -119
- data/test/macros/class_wrapper/hooks.rb +0 -41
- data/test/macros/class_wrapper/validations.rb +0 -91
- data/test/macros/instance_wrapper/destroy_macro.rb +0 -36
- data/test/macros/instance_wrapper/new_record_macro.rb +0 -39
- data/test/macros/instance_wrapper/save_macro.rb +0 -55
- data/test/macros/veneer_constants_interface.rb +0 -26
- data/test/support/before_save_mixin.rb +0 -14
- data/test/support/valiations_method_method.rb +0 -0
- data/test/veneer/adapters/active_record/class_wrapper_test.rb +0 -23
- data/test/veneer/adapters/active_record/instance_wrapper_test.rb +0 -21
- data/test/veneer/adapters/datamapper/class_wrapper_test.rb +0 -24
- data/test/veneer/adapters/datamapper/datamapper_setup.rb +0 -34
- data/test/veneer/adapters/datamapper/instance_wrapper_test.rb +0 -21
- data/test/veneer/adapters/mongomapper/class_wrapper_test.rb +0 -24
- data/test/veneer/adapters/mongomapper/instance_wrapper_test.rb +0 -21
- data/test/veneer/adapters/mongomapper/mongomapper_setup.rb +0 -25
- data/test/veneer/base/conditonal_test.rb +0 -118
@@ -1,52 +1,87 @@
|
|
1
1
|
module Veneer
|
2
2
|
module Base
|
3
|
+
# Required methods
|
4
|
+
# new
|
5
|
+
# find_many
|
6
|
+
# destroy_all
|
7
|
+
#
|
8
|
+
# Optional Methods
|
9
|
+
# find_first
|
10
|
+
# create!
|
11
|
+
# create
|
12
|
+
#
|
3
13
|
class ClassWrapper < BasicObject
|
4
14
|
attr_reader :klass, :opts
|
5
15
|
def initialize(klass, opts)
|
6
16
|
@klass, @opts = klass, opts
|
7
17
|
end
|
8
18
|
|
19
|
+
# Create an instance of the object.
|
20
|
+
# That is, instantiate and persist it in one step.
|
21
|
+
# Raise an error if the object is not persisted
|
9
22
|
# @api public
|
23
|
+
# @see create
|
10
24
|
def create!(opts = {})
|
11
25
|
instance = new(opts)
|
12
26
|
instance.save!
|
13
27
|
instance
|
14
28
|
end
|
15
29
|
|
30
|
+
# Create an instance of the object.
|
31
|
+
# That is, instantiate and persist it in one step.
|
16
32
|
# @api public
|
33
|
+
# @see create!
|
17
34
|
def create(opts = {})
|
18
35
|
instance = new(opts)
|
19
36
|
instance.save
|
20
37
|
instance
|
21
38
|
end
|
22
39
|
|
40
|
+
# Instantiate an item
|
41
|
+
# The interface required is that a hash of attributes is passed
|
42
|
+
# The object should take each key, and set the value provided
|
23
43
|
# @api implementor
|
24
44
|
def new(opts = {})
|
25
|
-
|
45
|
+
klass.new(opts)
|
26
46
|
end
|
27
47
|
|
48
|
+
# Provides query access to the first item who meets the conditions in the passed in options hash
|
49
|
+
#
|
28
50
|
# @api public
|
51
|
+
# @see Veneer::Base::ClassWrapper.all
|
29
52
|
def first(opts={})
|
30
|
-
|
31
|
-
::Kernel.Veneer(find_first(conditional))
|
53
|
+
::Kernel.Veneer(find_first(Hashie::Mash.new(opts)))
|
32
54
|
end
|
33
55
|
|
56
|
+
# Provides an interface to query the objects
|
57
|
+
#
|
58
|
+
# Options that must be supported are
|
59
|
+
#
|
60
|
+
# * :limit
|
61
|
+
# * :offset
|
62
|
+
# * :conditions
|
63
|
+
# * :order
|
64
|
+
#
|
34
65
|
# @api public
|
35
66
|
def all(opts={})
|
36
|
-
|
37
|
-
find_many(conditional).map{|element| ::Kernel.Veneer(element)}
|
67
|
+
find_many(Hashie::Mash.new(opts)).map{|element| ::Kernel.Veneer(element)}
|
38
68
|
end
|
39
69
|
|
40
|
-
# Should return an array or array like structure with elements matching the provided
|
70
|
+
# Should return an array or array like structure with elements matching the provided conditions hash
|
41
71
|
# @api implementor
|
42
|
-
|
72
|
+
# @see Veneer::Base::ClassWrapper.all
|
73
|
+
def find_many(opts)
|
43
74
|
::Kernel.raise Errors::NotImplemented
|
44
75
|
end
|
45
76
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
77
|
+
def find_first(opts)
|
78
|
+
opts[:limit] = 1
|
79
|
+
find_many(opts).first
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
def method_missing(meth, *args, &blk)
|
84
|
+
@klass.__send__(meth, *args, &blk)
|
50
85
|
end
|
51
86
|
end
|
52
87
|
end
|
@@ -1,5 +1,13 @@
|
|
1
1
|
module Veneer
|
2
2
|
module Base
|
3
|
+
# required methods
|
4
|
+
#
|
5
|
+
# save
|
6
|
+
# save! # failure should raise ::Veneer::Errors::NotSaved
|
7
|
+
# update
|
8
|
+
# destroy
|
9
|
+
# new_record?
|
10
|
+
|
3
11
|
class InstanceWrapper
|
4
12
|
attr_accessor :instance, :options
|
5
13
|
def initialize(instance, opts = {})
|
@@ -10,20 +18,6 @@ module Veneer
|
|
10
18
|
@instance.class
|
11
19
|
end
|
12
20
|
|
13
|
-
def handle_before_save_error(e)
|
14
|
-
if instance.respond_to?(:errors) && instance.errors.respond_to?(:add)
|
15
|
-
case e.message
|
16
|
-
when Array
|
17
|
-
instance.errors.add(e.message[0], e.message[1])
|
18
|
-
when String
|
19
|
-
instance.errors.add("", e.message)
|
20
|
-
end
|
21
|
-
else
|
22
|
-
::STDOUT.puts e.message
|
23
|
-
end
|
24
|
-
false
|
25
|
-
end
|
26
|
-
|
27
21
|
# Checks equality of the instances
|
28
22
|
# @api public
|
29
23
|
def ==(other)
|
@@ -47,6 +41,10 @@ module Veneer
|
|
47
41
|
save
|
48
42
|
end
|
49
43
|
|
44
|
+
def update!(attributes = {})
|
45
|
+
raise Veneer::Errors::NotSaved unless update(attributes)
|
46
|
+
end
|
47
|
+
|
50
48
|
# send all methods to the instance
|
51
49
|
def method_missing(*args, &blk)
|
52
50
|
instance.send(*args, &blk)
|
data/lib/veneer/errors.rb
CHANGED
@@ -3,7 +3,6 @@ module Veneer
|
|
3
3
|
class VeneerError < StandardError; end
|
4
4
|
class NotCompatible < VeneerError; end
|
5
5
|
class NotSaved < VeneerError; end
|
6
|
-
class ConditionalOperatorNotSupported < VeneerError; end
|
7
6
|
class NotImplemented < VeneerError; end
|
8
7
|
class BeforeSaveError < VeneerError; end
|
9
8
|
end
|
data/lib/veneer/lint.rb
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
module Veneer
|
2
|
+
module Lint
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
include ::Veneer::Lint::ClassWrapperLint
|
6
|
+
include ::Veneer::Lint::InstanceWrapperLint
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def _veneer_teardown
|
11
|
+
Veneer(@klass).destroy_all
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassWrapperLint
|
15
|
+
def test_should_have_the_correct_inner_constants_for_veneer
|
16
|
+
assert_nothing_raised do
|
17
|
+
@klass::VeneerInterface
|
18
|
+
@klass::VeneerInterface::ClassWrapper
|
19
|
+
@klass::VeneerInterface::InstanceWrapper
|
20
|
+
end
|
21
|
+
assert @klass::VeneerInterface::ClassWrapper.ancestors.include?(::Veneer::Base::ClassWrapper)
|
22
|
+
assert @klass::VeneerInterface::InstanceWrapper.ancestors.include?(::Veneer::Base::InstanceWrapper)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_setup_the_veneer_lint_class_wrapper_with_a_klass
|
26
|
+
assert_not_nil @klass, "@klass should provide a class to test Veneer with"
|
27
|
+
assert_kind_of Class, @klass
|
28
|
+
ensure
|
29
|
+
_veneer_teardown
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_provide_valid_attributes
|
33
|
+
assert_not_nil @valid_attributes
|
34
|
+
assert_kind_of Hash, @valid_attributes
|
35
|
+
ensure
|
36
|
+
_veneer_teardown
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_provide_invalid_attributes
|
40
|
+
assert_not_nil @invalid_attributes
|
41
|
+
assert_kind_of Hash, @invalid_attributes
|
42
|
+
ensure
|
43
|
+
_veneer_teardown
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_create_an_object_from_a_hash
|
47
|
+
r = Veneer(@klass).create(@valid_attributes)
|
48
|
+
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
49
|
+
ensure
|
50
|
+
_veneer_teardown
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_create_the_object_from_the_hash_with_create!
|
54
|
+
r = Veneer(@klass).create!(@valid_attributes)
|
55
|
+
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
56
|
+
ensure
|
57
|
+
_veneer_teardown
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_should_instantiate_a_new_instance_of_the_object
|
61
|
+
r = Veneer(@klass).new(@valid_attributes)
|
62
|
+
assert_instance_of @klass::VeneerInterface::InstanceWrapper, r
|
63
|
+
ensure
|
64
|
+
_veneer_teardown
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_implementst_create_with_invalid_attributes
|
68
|
+
assert_raise Veneer::Errors::NotSaved do
|
69
|
+
Veneer(@klass).create!(@invalid_attributes)
|
70
|
+
end
|
71
|
+
ensure
|
72
|
+
_veneer_teardown
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_should_not_raise_when_create_can_save
|
76
|
+
assert_nothing_raised do
|
77
|
+
Veneer(@klass).create(@invalid_attributes)
|
78
|
+
end
|
79
|
+
ensure
|
80
|
+
_veneer_teardown
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_should_destory_all
|
84
|
+
Veneer(@klass).create(@valid_attributes)
|
85
|
+
assert Veneer(@klass).all.size > 0
|
86
|
+
Veneer(@klass).destroy_all
|
87
|
+
assert_equal 0, Veneer(@klass).all.size
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_should_find_all
|
91
|
+
create_valid_items(4)
|
92
|
+
result = Veneer(@klass).all
|
93
|
+
assert_kind_of Array, result
|
94
|
+
assert_equal 4, result.size
|
95
|
+
ensure
|
96
|
+
_veneer_teardown
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_should_find_first
|
100
|
+
create_valid_items(3)
|
101
|
+
result = Veneer(@klass).all
|
102
|
+
assert_not_nil result
|
103
|
+
ensure
|
104
|
+
_veneer_teardown
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_should_implement_limit
|
108
|
+
create_valid_items(4)
|
109
|
+
result = Veneer(@klass).all(:limit => 2)
|
110
|
+
assert_equal 2, result.size
|
111
|
+
ensure
|
112
|
+
_veneer_teardown
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_should_implement_order
|
116
|
+
create_valid_items(4)
|
117
|
+
key = @valid_attributes.keys.first
|
118
|
+
ordered_result = Veneer(@klass).all(:order => key)
|
119
|
+
result = Veneer(@klass).all
|
120
|
+
result = result.sort_by{ |i| i.send(key) }
|
121
|
+
assert_equal ordered_result, result
|
122
|
+
ensure
|
123
|
+
_veneer_teardown
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_should_implement_order_with_desc
|
127
|
+
create_valid_items(4)
|
128
|
+
key = @valid_attributes.keys.first
|
129
|
+
ordered_result = Veneer(@klass).all(:order => "#{key} desc")
|
130
|
+
result = Veneer(@klass).all
|
131
|
+
result = result.sort_by{ |i| i.send(key) }.reverse
|
132
|
+
assert_equal ordered_result, result
|
133
|
+
ensure
|
134
|
+
_veneer_teardown
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_should_implement_offset
|
138
|
+
create_valid_items(4)
|
139
|
+
result = Veneer(@klass).all
|
140
|
+
offset_result = Veneer(@klass).all(:offset => 2, :limit => 2)
|
141
|
+
assert_equal result[2..4], offset_result
|
142
|
+
ensure
|
143
|
+
_veneer_teardown
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_conditions
|
147
|
+
create_valid_items(3)
|
148
|
+
Veneer(@klass).create(@valid_attributes)
|
149
|
+
result = Veneer(@klass).all(:conditions => @valid_attributes)
|
150
|
+
assert_equal 1, result.size
|
151
|
+
ensure
|
152
|
+
_veneer_teardown
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
module InstanceWrapperLint
|
157
|
+
def test_should_implement_destroy
|
158
|
+
item = Veneer(@klass).create(@valid_attributes)
|
159
|
+
assert Veneer(@klass).all.size > 0
|
160
|
+
assert item.kind_of?(Veneer::Base::InstanceWrapper)
|
161
|
+
item.destroy
|
162
|
+
assert Veneer(@klass).all.size == 0
|
163
|
+
ensure
|
164
|
+
_veneer_teardown
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_should_implement_new_record?
|
168
|
+
item = Veneer(@klass).new(@valid_attributes)
|
169
|
+
assert item.new_record?
|
170
|
+
assert item.save
|
171
|
+
assert !item.new_record?
|
172
|
+
ensure
|
173
|
+
_veneer_teardown
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_should_implement_save!
|
177
|
+
assert_raises Veneer::Errors::NotSaved do
|
178
|
+
Veneer(@klass).create!(@invalid_attributes)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
data/lib/veneer.rb
CHANGED
@@ -1,8 +1,4 @@
|
|
1
1
|
require 'hashie'
|
2
|
-
require 'dirge'
|
3
|
-
|
4
|
-
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), "lib"))
|
5
|
-
|
6
2
|
require 'veneer/core_ext/kernel'
|
7
3
|
|
8
4
|
unless defined?(BasicObject)
|
@@ -28,6 +24,7 @@ module Veneer
|
|
28
24
|
autoload :Errors, 'veneer/errors'
|
29
25
|
autoload :Proxy, 'veneer/proxy'
|
30
26
|
autoload :Conditional, 'veneer/base/conditional'
|
27
|
+
autoload :Lint, 'veneer/lint'
|
31
28
|
|
32
29
|
module Base
|
33
30
|
autoload :ClassWrapper, 'veneer/base/class_wrapper'
|
data/test/test_helper.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
3
|
require 'shoulda'
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
|
7
|
+
require 'veneer/lint'
|
4
8
|
|
5
9
|
Dir[File.join(File.dirname(__FILE__), "support", "**/*.rb")].each{|f| require f}
|
6
|
-
Dir[File.join(File.dirname(__FILE__), "macros", "**/*.rb")].each{|f| require f}
|
7
10
|
|
8
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
11
|
require 'veneer'
|
11
12
|
|
12
13
|
$validations = []
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
2
|
+
|
1
3
|
require 'activerecord'
|
2
4
|
require 'veneer/adapters/activerecord'
|
3
5
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
@@ -7,6 +9,8 @@ class CreateActiveRecordFoo < ActiveRecord::Migration
|
|
7
9
|
def self.up
|
8
10
|
create_table :active_record_foos, :force => true do |t|
|
9
11
|
t.string :name
|
12
|
+
t.string :title
|
13
|
+
t.string :description
|
10
14
|
t.integer :order_field1
|
11
15
|
end
|
12
16
|
end
|
@@ -19,7 +23,6 @@ end
|
|
19
23
|
CreateActiveRecordFoo.up
|
20
24
|
|
21
25
|
class ActiveRecordFoo < ActiveRecord::Base
|
22
|
-
attr_accessor :password, :password_confirmation
|
23
26
|
def self.veneer_spec_reset!
|
24
27
|
delete_all
|
25
28
|
end
|
@@ -32,3 +35,22 @@ class ActiveRecordFoo < ActiveRecord::Base
|
|
32
35
|
errors.add(:name, "Name cannot be v_with_m_test") if name == "v_with_m_test"
|
33
36
|
end
|
34
37
|
end
|
38
|
+
|
39
|
+
class ActiveRecordVeneerTest < ::Test::Unit::TestCase
|
40
|
+
include Veneer::Lint
|
41
|
+
|
42
|
+
def setup
|
43
|
+
@klass = ::ActiveRecordFoo
|
44
|
+
@valid_attributes = {:name => "foo", :title => "title", :description => "description"}
|
45
|
+
@invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
|
46
|
+
end
|
47
|
+
|
48
|
+
def create_valid_items(num)
|
49
|
+
attr = @valid_attributes
|
50
|
+
|
51
|
+
(1..num).each do |i|
|
52
|
+
ActiveRecordFoo.create(:name => "#{attr[:name]}#{i}", :title => "#{attr[:title]}#{i}", :description => "#{attr[:description]}#{i}")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
2
|
+
require 'dm-core'
|
3
|
+
require 'dm-validations'
|
4
|
+
require 'dm-migrations'
|
5
|
+
require 'veneer/adapters/datamapper'
|
6
|
+
|
7
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
8
|
+
|
9
|
+
class DMFoo
|
10
|
+
include DataMapper::Resource
|
11
|
+
|
12
|
+
property :id, Serial
|
13
|
+
property :name, String
|
14
|
+
property :title, String
|
15
|
+
property :description, String
|
16
|
+
property :order_field1, Integer
|
17
|
+
|
18
|
+
validates_with_method :name, :method => :check_name
|
19
|
+
|
20
|
+
def check_name
|
21
|
+
if name == "invalid"
|
22
|
+
[false, "Invalid name"]
|
23
|
+
else
|
24
|
+
true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def v_with_m_test
|
29
|
+
if name == "v_with_m_test"
|
30
|
+
[false, "name cannot be v_with_m_test"]
|
31
|
+
else
|
32
|
+
true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
DataMapper.auto_migrate!
|
38
|
+
|
39
|
+
class DataMapperVeneerTest < ::Test::Unit::TestCase
|
40
|
+
include Veneer::Lint
|
41
|
+
|
42
|
+
def setup
|
43
|
+
@klass = ::DMFoo
|
44
|
+
@valid_attributes = {:name => "foo", :title => "title", :description => "description"}
|
45
|
+
@invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
|
46
|
+
end
|
47
|
+
|
48
|
+
def create_valid_items(num)
|
49
|
+
attr = @valid_attributes
|
50
|
+
|
51
|
+
(1..num).each do |i|
|
52
|
+
DMFoo.create(:name => "#{attr[:name]}#{i}", :title => "#{attr[:title]}#{i}", :description => "#{attr[:description]}#{i}")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
2
|
+
require 'mongo_mapper'
|
3
|
+
|
4
|
+
require 'veneer/adapters/mongomapper'
|
5
|
+
|
6
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
|
7
|
+
MongoMapper.database = 'veneer_test'
|
8
|
+
|
9
|
+
class MongoFoo
|
10
|
+
include MongoMapper::Document
|
11
|
+
attr_accessor :password, :password_confirmation
|
12
|
+
|
13
|
+
key :name, String
|
14
|
+
key :title, String
|
15
|
+
key :description, String
|
16
|
+
key :order_field1, Integer
|
17
|
+
|
18
|
+
validate_on_create :check_name
|
19
|
+
|
20
|
+
def check_name
|
21
|
+
errors.add(:name, "may not be invalid") if name == "invalid"
|
22
|
+
end
|
23
|
+
|
24
|
+
def v_with_m_test
|
25
|
+
errors.add(:name, "may not be v_with_m_test") if name == "v_with_m_test"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class MongoMapperVeneerTest < ::Test::Unit::TestCase
|
30
|
+
include Veneer::Lint
|
31
|
+
|
32
|
+
def setup
|
33
|
+
@klass = ::MongoFoo
|
34
|
+
@valid_attributes = {:name => "foo", :title => "title", :description => "description"}
|
35
|
+
@invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_valid_items(num)
|
39
|
+
attr = @valid_attributes
|
40
|
+
|
41
|
+
(1..num).each do |i|
|
42
|
+
MongoFoo.create(:name => "#{attr[:name]}#{i}", :title => "#{attr[:title]}#{i}", :description => "#{attr[:description]}#{i}")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
data/test/veneer/proxy_test.rb
CHANGED
@@ -51,63 +51,38 @@ class VeneerProxyTest < Test::Unit::TestCase
|
|
51
51
|
klass.collection.clear
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
result.
|
54
|
+
def find_many(opts)
|
55
|
+
result = ::Foo.collection.dup
|
56
|
+
|
57
|
+
case opts.order
|
58
|
+
when Array # first order only supported
|
59
|
+
item = opts.order.first
|
60
|
+
field, direction = item.split(" ")
|
61
|
+
result = result.sort_by{|i| i.send(field)}
|
62
|
+
result.reverse! if direction.to_s /desc/i
|
63
|
+
when String, Symbol
|
64
|
+
result = result.sort_by{|i| i.send(opts.order) }
|
65
65
|
end
|
66
66
|
|
67
|
-
if
|
68
|
-
offset =
|
67
|
+
if opts.offset?
|
68
|
+
offset = opts.offset.to_i
|
69
69
|
result = result[offset..-1]
|
70
70
|
end
|
71
71
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
result.select{|i| i.send(condition.field) == condition.value}
|
76
|
-
when :not
|
77
|
-
result.select{|i| i.send(condition.field) != condition.value}
|
78
|
-
when :gt
|
79
|
-
result.select{|i| !i.send(condition.field).nil? && (i.send(condition.field) > condition.value)}
|
80
|
-
when :gte
|
81
|
-
result.select{|i| !i.send(condition.field).nil? && (i.send(condition.field) >= condition.value)}
|
82
|
-
when :lt
|
83
|
-
result.select{|i| !i.send(condition.field).nil? && (i.send(condition.field) < condition.value)}
|
84
|
-
when :lte
|
85
|
-
result.select{|i| !i.send(condition.field).nil? && (i.send(condition.field) <= condition.value)}
|
86
|
-
when :in
|
87
|
-
result.select{|i| condition.value.include?(i.send(condition.field))}
|
88
|
-
else
|
89
|
-
[]
|
72
|
+
if opts.conditions?
|
73
|
+
opts.conditions.each do |field, value|
|
74
|
+
result = result.select{|i| i.send(field) == value}.flatten.compact
|
90
75
|
end
|
91
|
-
result = filtered.flatten.compact
|
92
76
|
end
|
93
|
-
|
94
|
-
|
77
|
+
|
78
|
+
if opts.limit?
|
79
|
+
result = result[0..(opts.limit.to_i - 1)]
|
95
80
|
end
|
96
81
|
result
|
97
82
|
end
|
98
|
-
|
99
83
|
end
|
100
84
|
|
101
85
|
class InstanceWrapper < Veneer::Base::InstanceWrapper
|
102
|
-
|
103
|
-
def new_record?
|
104
|
-
instance.new_record?
|
105
|
-
end
|
106
|
-
|
107
|
-
def save!
|
108
|
-
instance.save!
|
109
|
-
end
|
110
|
-
|
111
86
|
def save
|
112
87
|
instance.save
|
113
88
|
rescue
|
@@ -170,7 +145,7 @@ class VeneerProxyTest < Test::Unit::TestCase
|
|
170
145
|
|
171
146
|
teardown{ ::Foo.collection.clear }
|
172
147
|
|
173
|
-
should "get the correct resource
|
148
|
+
should "get the correct resource" do
|
174
149
|
expected = ::Foo.collection.select{|i| i.name == "foo1"}.first
|
175
150
|
result = Veneer(::Foo).first(:conditions => {:name => "foo1"})
|
176
151
|
assert_equal expected, result.instance
|