veneer 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.textile +9 -5
- data/Rakefile +0 -16
- data/VERSION +1 -1
- data/lib/veneer.rb +3 -1
- data/lib/veneer/adapters/active_record.rb +1 -0
- data/lib/veneer/adapters/activerecord/class_wrapper.rb +0 -11
- data/lib/veneer/adapters/activerecord/instance_wrapper.rb +10 -0
- data/lib/veneer/adapters/data_mapper.rb +1 -0
- data/lib/veneer/adapters/mongomapper.rb +3 -0
- data/lib/veneer/adapters/mongomapper/class_wrapper.rb +55 -0
- data/lib/veneer/adapters/mongomapper/instance_wrapper.rb +13 -0
- data/lib/veneer/base/conditional.rb +2 -2
- data/test/macros/class_wrapper/find_macro.rb +12 -5
- data/test/veneer/adapters/mongomapper/class_wrapper_test.rb +22 -0
- data/test/veneer/adapters/mongomapper/instance_wrapper_test.rb +21 -0
- data/test/veneer/adapters/mongomapper/mongomapper_setup.rb +20 -0
- data/veneer.gemspec +13 -2
- metadata +13 -2
data/.gitignore
CHANGED
data/README.textile
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
h1. veneer
|
2
2
|
|
3
|
-
Veneer is an interface that sits above Data Sources.
|
4
|
-
|
3
|
+
Veneer is an interface that sits above Data Sources. A data source may be an ORM like ActiveRecord or DataMapper, or it could be a document store like MongoMapper or even just an storage like Moneta.
|
4
|
+
|
5
|
+
Veneer aims to provide plugin authors with a consistant interface to any of these libraries so that plugins, engines, stacks and gems may be used across data store types. Reusable code FTW!
|
6
|
+
|
7
|
+
It differs from ActiveModel in that it doesn't provide any validation support, serialization, callbacks, state machine or anything like that. Veneer is intended to work with ActiveModel to provide a data store independant interface to create reusable code.
|
8
|
+
|
5
9
|
Veneer instead aims to provide a simple interface for
|
6
10
|
* querying
|
7
11
|
* creating
|
@@ -20,7 +24,7 @@ obj.save
|
|
20
24
|
obj = Veneer(MyModel).create(:some => "attribute")
|
21
25
|
</code></pre>
|
22
26
|
|
23
|
-
There are also version that will raise
|
27
|
+
There are also version that will raise an exception if it could not save
|
24
28
|
|
25
29
|
<pre><code>obj = Veneer(MyModel).new(:some => "attribute").save!
|
26
30
|
|
@@ -44,7 +48,7 @@ Veneer lets you query models with a simple interface. Only simple queries are s
|
|
44
48
|
|
45
49
|
You can query a single object, or multiple objects.
|
46
50
|
|
47
|
-
<pre><code>Veneer(MyModel).
|
51
|
+
<pre><code>Veneer(MyModel).first(:conditions => {:name => "foo"})
|
48
52
|
|
49
53
|
Veneer(MyModel).all(:conditions => {"age gte" => 18}, :limit => 5)
|
50
54
|
</code></pre>
|
@@ -112,7 +116,7 @@ h2. Current Support
|
|
112
116
|
|
113
117
|
Veneer currently has built in support for ActiveRecord and DataMapper.
|
114
118
|
|
115
|
-
Veneer works on a VeneerInterface inner module though so you can easily impelment
|
119
|
+
Veneer works on a VeneerInterface inner module though so you can easily impelment your adapter without requiring it to be in the veneer repo (although pull requests are welcome)
|
116
120
|
|
117
121
|
To use DataMapper or ActiveRecord
|
118
122
|
|
data/Rakefile
CHANGED
@@ -20,22 +20,6 @@ rescue LoadError
|
|
20
20
|
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
21
|
end
|
22
22
|
|
23
|
-
#require 'spec/rake/spectask'
|
24
|
-
#Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
-
# spec.libs << 'lib' << 'spec'
|
26
|
-
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
-
#end
|
28
|
-
#
|
29
|
-
#Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
-
# spec.libs << 'lib' << 'spec'
|
31
|
-
# spec.pattern = 'spec/**/*_spec.rb'
|
32
|
-
# spec.rcov = true
|
33
|
-
#end
|
34
|
-
#
|
35
|
-
#task :spec => :check_dependencies
|
36
|
-
#
|
37
|
-
#task :default => :spec
|
38
|
-
|
39
23
|
task :default => [:test_units]
|
40
24
|
require 'rake/testtask'
|
41
25
|
desc "Run test suite."
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/veneer.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
require ~'./activerecord'
|
@@ -74,17 +74,6 @@ module ActiveRecord
|
|
74
74
|
end
|
75
75
|
end # ClassWrapper
|
76
76
|
|
77
|
-
class InstanceWrapper < Veneer::Base::InstanceWrapper
|
78
|
-
def save!
|
79
|
-
instance.save!
|
80
|
-
rescue ActiveRecord::RecordInvalid => e
|
81
|
-
raise ::Veneer::Errors::NotSaved
|
82
|
-
end
|
83
|
-
|
84
|
-
def save
|
85
|
-
instance.save
|
86
|
-
end
|
87
|
-
end # InstanceWrapper
|
88
77
|
end
|
89
78
|
end
|
90
79
|
end
|
@@ -5,6 +5,16 @@ module ActiveRecord
|
|
5
5
|
def update(attributes = {})
|
6
6
|
instance.update_attributes(attributes)
|
7
7
|
end
|
8
|
+
|
9
|
+
def save!
|
10
|
+
instance.save!
|
11
|
+
rescue ActiveRecord::RecordInvalid => e
|
12
|
+
raise ::Veneer::Errors::NotSaved
|
13
|
+
end
|
14
|
+
|
15
|
+
def save
|
16
|
+
instance.save
|
17
|
+
end
|
8
18
|
end # InstanceWrapper
|
9
19
|
end # VeneerInterface
|
10
20
|
end # Base
|
@@ -0,0 +1 @@
|
|
1
|
+
require ~'./datamapper'
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Document
|
3
|
+
module VeneerInterface
|
4
|
+
class ClassWrapper < Veneer::Base::ClassWrapper
|
5
|
+
def new(opts = {})
|
6
|
+
::Kernel::Veneer(klass.new(opts))
|
7
|
+
end
|
8
|
+
|
9
|
+
def destroy_all
|
10
|
+
klass.destroy_all
|
11
|
+
end
|
12
|
+
|
13
|
+
def find_first(conditional)
|
14
|
+
opts = conditional_to_mongo_opts(conditional)
|
15
|
+
klass.first(opts)
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_many(conditional)
|
19
|
+
opts = conditional_to_mongo_opts(conditional)
|
20
|
+
klass.all(opts)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def conditional_to_mongo_opts(c)
|
25
|
+
opts = {}
|
26
|
+
opts[:limit] = c.limit if c.limit
|
27
|
+
opts[:offset] = c.offset if c.offset
|
28
|
+
|
29
|
+
unless c.order.blank?
|
30
|
+
opts[:order] = c.order.inject([]) do |ary, cnd|
|
31
|
+
ary << "#{cnd.field} #{cnd.direction}"
|
32
|
+
end.join(",")
|
33
|
+
end
|
34
|
+
|
35
|
+
unless c.conditions.blank?
|
36
|
+
cnds = c.conditions.inject({}) do |hsh, c|
|
37
|
+
case c.operator
|
38
|
+
when :eql
|
39
|
+
hsh[c.field] = c.value
|
40
|
+
when :not
|
41
|
+
hsh[c.field] = {"$ne" => c.value}
|
42
|
+
when :gt, :gte, :lt, :lte, :in
|
43
|
+
hsh[c.field] = {"$#{c.operator}" => c.value}
|
44
|
+
end
|
45
|
+
::STDOUT.puts hsh.inspect
|
46
|
+
hsh
|
47
|
+
end
|
48
|
+
opts[:conditions] = cnds
|
49
|
+
end
|
50
|
+
opts
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -7,8 +7,8 @@ module Veneer
|
|
7
7
|
|
8
8
|
attr_reader :options
|
9
9
|
def initialize(opts)
|
10
|
-
@options =
|
11
|
-
odr =
|
10
|
+
@options = Hashie::Mash.new(opts || {})
|
11
|
+
odr = [@options.delete("order")].flatten.compact
|
12
12
|
@ordering = odr.nil? || odr.empty? ? [] : begin
|
13
13
|
odr.map do |order|
|
14
14
|
field, direction = order.split(" ")
|
@@ -27,12 +27,19 @@ class Test::Unit::TestCase
|
|
27
27
|
end
|
28
28
|
|
29
29
|
should "implement order" do
|
30
|
-
result = Veneer(@klass).find_many(Veneer::Conditional.from_hash(:order => "order_field1"))
|
30
|
+
result = Veneer(@klass).find_many(Veneer::Conditional.from_hash(:order => "order_field1 asc"))
|
31
31
|
raw = result.map{|i| i.order_field1 }.compact
|
32
|
-
sorted = raw.sort
|
32
|
+
sorted = raw.sort
|
33
33
|
assert_equal raw, sorted
|
34
34
|
end
|
35
35
|
|
36
|
+
should "implement asc and desc order as opposites" do
|
37
|
+
result_asc = Veneer(@klass).find_many(Veneer::Conditional.from_hash(:order => "order_field1 asc")).map{|o| o.order_field1}
|
38
|
+
result_desc = Veneer(@klass).find_many(Veneer::Conditional.from_hash(:order => "order_field1 desc")).map{|o| o.order_field1}
|
39
|
+
|
40
|
+
assert_equal result_asc, result_desc.reverse
|
41
|
+
end
|
42
|
+
|
36
43
|
should "implement order decending" do
|
37
44
|
result = Veneer(@klass).find_many(Veneer::Conditional.from_hash(:order => "order_field1 desc"))
|
38
45
|
raw = result.map{|i| i.order_field1 }.compact
|
@@ -83,9 +90,9 @@ class Test::Unit::TestCase
|
|
83
90
|
|
84
91
|
should "implement gte conditions" do
|
85
92
|
total = Veneer(@klass).find_many(Veneer::Conditional.from_hash({}))
|
86
|
-
expected = total.select{|i| !i.order_field1.nil? && i.order_field1 >= 3}
|
87
|
-
result = Veneer(@klass).find_many(Veneer::Conditional.from_hash(:conditions => {"order_field1 gte" => 3}))
|
88
|
-
assert_equal
|
93
|
+
expected = total.select{|i| !i.order_field1.nil? && i.order_field1 >= 3}.map{|i| i.order_field1}
|
94
|
+
result = Veneer(@klass).find_many(Veneer::Conditional.from_hash(:conditions => {"order_field1 gte" => 3})).map{|o| o.order_field1}
|
95
|
+
assert_equal expected, result
|
89
96
|
end
|
90
97
|
|
91
98
|
should "implement :lt condition" do
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "..", "test_helper")
|
2
|
+
require ~"./mongomapper_setup"
|
3
|
+
|
4
|
+
module Veneer
|
5
|
+
module Test
|
6
|
+
class MongoMapperClassWrapper < ::Test::Unit::TestCase
|
7
|
+
context "MongoMapper Veneer Adapter" do
|
8
|
+
setup do
|
9
|
+
@klass = MongoFoo
|
10
|
+
@valid_attributes = {:name => "foo"}
|
11
|
+
@invalid_attributes = {:name => "invalid"}
|
12
|
+
end
|
13
|
+
|
14
|
+
veneer_should_have_the_required_veneer_constants
|
15
|
+
veneer_should_implement_create_with_valid_attributes
|
16
|
+
veneer_should_impelement_destroy_all
|
17
|
+
veneer_should_implement_create_with_invalid_attributes
|
18
|
+
veneer_should_implement_find
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "..", "test_helper")
|
2
|
+
require File.join(File.dirname(__FILE__), "mongomapper_setup")
|
3
|
+
|
4
|
+
module Veneer
|
5
|
+
module Test
|
6
|
+
class MongoMapperInstanceWrapper < ::Test::Unit::TestCase
|
7
|
+
context "MongoMapper Veneer Adapter" do
|
8
|
+
setup do
|
9
|
+
@klass = MongoFoo
|
10
|
+
@valid_attributes = {:name => "foo"}
|
11
|
+
@invalid_attributes = {:name => "invalid"}
|
12
|
+
end
|
13
|
+
|
14
|
+
veneer_should_implement_new_record?
|
15
|
+
veneer_should_implement_save
|
16
|
+
veneer_should_implement_save!
|
17
|
+
veneer_should_implement_destroy
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'mongomapper'
|
2
|
+
|
3
|
+
require 'veneer/adapters/mongomapper'
|
4
|
+
|
5
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
|
6
|
+
MongoMapper.database = 'veneer_test'
|
7
|
+
|
8
|
+
class MongoFoo
|
9
|
+
include MongoMapper::Document
|
10
|
+
|
11
|
+
key :name, String
|
12
|
+
key :order_field1, Integer
|
13
|
+
|
14
|
+
validate_on_create :check_name
|
15
|
+
|
16
|
+
def check_name
|
17
|
+
errors.add(:name, "may not be invalid") if name == "invalid"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
data/veneer.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{veneer}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Daniel Neighman"]
|
12
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-12-05}
|
13
13
|
s.description = %q{Veneer provides basic querying, saving, deleteing and creating of data stores.}
|
14
14
|
s.email = %q{has.sox@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,12 +25,17 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"datamapper_setup.rb",
|
27
27
|
"lib/veneer.rb",
|
28
|
+
"lib/veneer/adapters/active_record.rb",
|
28
29
|
"lib/veneer/adapters/activerecord.rb",
|
29
30
|
"lib/veneer/adapters/activerecord/class_wrapper.rb",
|
30
31
|
"lib/veneer/adapters/activerecord/instance_wrapper.rb",
|
32
|
+
"lib/veneer/adapters/data_mapper.rb",
|
31
33
|
"lib/veneer/adapters/datamapper.rb",
|
32
34
|
"lib/veneer/adapters/datamapper/class_wrapper.rb",
|
33
35
|
"lib/veneer/adapters/datamapper/instance_wrapper.rb",
|
36
|
+
"lib/veneer/adapters/mongomapper.rb",
|
37
|
+
"lib/veneer/adapters/mongomapper/class_wrapper.rb",
|
38
|
+
"lib/veneer/adapters/mongomapper/instance_wrapper.rb",
|
34
39
|
"lib/veneer/base/class_wrapper.rb",
|
35
40
|
"lib/veneer/base/conditional.rb",
|
36
41
|
"lib/veneer/base/instance_wrapper.rb",
|
@@ -52,6 +57,9 @@ Gem::Specification.new do |s|
|
|
52
57
|
"test/veneer/adapters/datamapper/class_wrapper_test.rb",
|
53
58
|
"test/veneer/adapters/datamapper/datamapper_setup.rb",
|
54
59
|
"test/veneer/adapters/datamapper/instance_wrapper_test.rb",
|
60
|
+
"test/veneer/adapters/mongomapper/class_wrapper_test.rb",
|
61
|
+
"test/veneer/adapters/mongomapper/instance_wrapper_test.rb",
|
62
|
+
"test/veneer/adapters/mongomapper/mongomapper_setup.rb",
|
55
63
|
"test/veneer/base/conditonal_test.rb",
|
56
64
|
"test/veneer/proxy_test.rb",
|
57
65
|
"veneer.gemspec"
|
@@ -78,6 +86,9 @@ Gem::Specification.new do |s|
|
|
78
86
|
"test/veneer/adapters/datamapper/class_wrapper_test.rb",
|
79
87
|
"test/veneer/adapters/datamapper/datamapper_setup.rb",
|
80
88
|
"test/veneer/adapters/datamapper/instance_wrapper_test.rb",
|
89
|
+
"test/veneer/adapters/mongomapper/class_wrapper_test.rb",
|
90
|
+
"test/veneer/adapters/mongomapper/instance_wrapper_test.rb",
|
91
|
+
"test/veneer/adapters/mongomapper/mongomapper_setup.rb",
|
81
92
|
"test/veneer/base/conditonal_test.rb",
|
82
93
|
"test/veneer/proxy_test.rb"
|
83
94
|
]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: veneer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Neighman
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-12-05 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -31,12 +31,17 @@ files:
|
|
31
31
|
- VERSION
|
32
32
|
- datamapper_setup.rb
|
33
33
|
- lib/veneer.rb
|
34
|
+
- lib/veneer/adapters/active_record.rb
|
34
35
|
- lib/veneer/adapters/activerecord.rb
|
35
36
|
- lib/veneer/adapters/activerecord/class_wrapper.rb
|
36
37
|
- lib/veneer/adapters/activerecord/instance_wrapper.rb
|
38
|
+
- lib/veneer/adapters/data_mapper.rb
|
37
39
|
- lib/veneer/adapters/datamapper.rb
|
38
40
|
- lib/veneer/adapters/datamapper/class_wrapper.rb
|
39
41
|
- lib/veneer/adapters/datamapper/instance_wrapper.rb
|
42
|
+
- lib/veneer/adapters/mongomapper.rb
|
43
|
+
- lib/veneer/adapters/mongomapper/class_wrapper.rb
|
44
|
+
- lib/veneer/adapters/mongomapper/instance_wrapper.rb
|
40
45
|
- lib/veneer/base/class_wrapper.rb
|
41
46
|
- lib/veneer/base/conditional.rb
|
42
47
|
- lib/veneer/base/instance_wrapper.rb
|
@@ -58,6 +63,9 @@ files:
|
|
58
63
|
- test/veneer/adapters/datamapper/class_wrapper_test.rb
|
59
64
|
- test/veneer/adapters/datamapper/datamapper_setup.rb
|
60
65
|
- test/veneer/adapters/datamapper/instance_wrapper_test.rb
|
66
|
+
- test/veneer/adapters/mongomapper/class_wrapper_test.rb
|
67
|
+
- test/veneer/adapters/mongomapper/instance_wrapper_test.rb
|
68
|
+
- test/veneer/adapters/mongomapper/mongomapper_setup.rb
|
61
69
|
- test/veneer/base/conditonal_test.rb
|
62
70
|
- test/veneer/proxy_test.rb
|
63
71
|
- veneer.gemspec
|
@@ -105,5 +113,8 @@ test_files:
|
|
105
113
|
- test/veneer/adapters/datamapper/class_wrapper_test.rb
|
106
114
|
- test/veneer/adapters/datamapper/datamapper_setup.rb
|
107
115
|
- test/veneer/adapters/datamapper/instance_wrapper_test.rb
|
116
|
+
- test/veneer/adapters/mongomapper/class_wrapper_test.rb
|
117
|
+
- test/veneer/adapters/mongomapper/instance_wrapper_test.rb
|
118
|
+
- test/veneer/adapters/mongomapper/mongomapper_setup.rb
|
108
119
|
- test/veneer/base/conditonal_test.rb
|
109
120
|
- test/veneer/proxy_test.rb
|