veneer 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -24,6 +24,24 @@ module ActiveRecord
24
24
  klass.before_save *methods
25
25
  end
26
26
 
27
+ %w(
28
+ validates_presence_of
29
+ validates_uniqueness_of
30
+ validates_confirmation_of
31
+ ).each do |meth|
32
+ class_eval <<-RUBY
33
+ def #{meth}(*args)
34
+ klass.#{meth}(*args)
35
+ end
36
+ RUBY
37
+ end
38
+
39
+ def validates_with_method(*methods)
40
+ methods.each do |meth|
41
+ @klass.validate meth
42
+ end
43
+ end
44
+
27
45
  private
28
46
  def conditional_to_ar_opts(c)
29
47
  opts = {}
@@ -28,7 +28,28 @@ module DataMapper
28
28
  end
29
29
  end
30
30
 
31
+ [
32
+ %w(validates_presence_of validates_present),
33
+ %w(validates_uniqueness_of validates_is_unique),
34
+ %w(validates_confirmation_of validates_is_confirmed),
35
+ %w(validates_with_method validates_with_method)
36
+ ].each do |(meth,natural)|
37
+ class_eval <<-RUBY
38
+ def #{meth}(*args)
39
+ ensure_validations_loaded!
40
+ klass.#{natural}(*args)
41
+ end
42
+ RUBY
43
+ end
44
+
31
45
  private
46
+ def ensure_validations_loaded!
47
+ unless defined?(::DataMapper::Validate)
48
+ ::Kernel.require 'dm-validations'
49
+ @klass.class_eval{include ::DataMapper::Validate}
50
+ end
51
+ end
52
+
32
53
  def opts_from_conditional_for_dm(c)
33
54
  opts = {}
34
55
 
@@ -24,6 +24,24 @@ module MongoMapper
24
24
  klass.before_save *methods
25
25
  end
26
26
 
27
+ %w(
28
+ validates_presence_of
29
+ validates_uniqueness_of
30
+ validates_confirmation_of
31
+ ).each do |meth|
32
+ class_eval <<-RUBY
33
+ def #{meth}(*args)
34
+ klass.#{meth}(*args)
35
+ end
36
+ RUBY
37
+ end
38
+
39
+ def validates_with_method(*methods)
40
+ methods.each do |meth|
41
+ klass.validates_each(nil, :logic => ::Proc.new{ send(meth)})
42
+ end
43
+ end
44
+
27
45
  private
28
46
  def conditional_to_mongo_opts(c)
29
47
  opts = {}
@@ -11,11 +11,15 @@ module Veneer
11
11
  end
12
12
 
13
13
  def handle_before_save_error(e)
14
- case e.message
15
- when Array
16
- instance.errors.add(e.message[0], e.message[1])
17
- when String
18
- instance.errors.add("", e.message)
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
19
23
  end
20
24
  false
21
25
  end
@@ -0,0 +1,91 @@
1
+ class Test::Unit::TestCase
2
+ def self.veneer_should_implement_validations
3
+ context "implements validations" do
4
+ setup do
5
+ Veneer(@klass).destroy_all
6
+ end
7
+
8
+ should "setup the test correctly" do
9
+ assert_not_nil @klass
10
+ assert_kind_of Class, @klass
11
+ assert_not_nil @valid_attributes
12
+ assert_kind_of Hash, @valid_attributes
13
+ assert_not_nil @valid_attributes[:name]
14
+ end
15
+
16
+ context "validates_presence_of" do
17
+ setup do
18
+ key = [@klass, :validates_presence_of, :name]
19
+ unless $validations.include?(key)
20
+ Veneer(@klass).validates_presence_of(:name)
21
+ $validations << key
22
+ end
23
+ end
24
+
25
+ should "allow addition of a validation for presence_of" do
26
+ attr = @valid_attributes.dup
27
+ attr.delete(:name)
28
+ k = Veneer(@klass).new(attr)
29
+ assert_false k.valid?
30
+ end
31
+ end
32
+
33
+ context "validates_uniqueness_of" do
34
+ setup do
35
+ key = [@klass, :validates_uniqueness_of, :name]
36
+ unless $validations.include?(key)
37
+ Veneer(@klass).validates_uniqueness_of(:name)
38
+ $validations << key
39
+ end
40
+ end
41
+
42
+ should "not allow duplicate data" do
43
+ k = Veneer(@klass).new(@valid_attributes)
44
+ assert_true k.save
45
+ k2 = Veneer(@klass).new(@valid_attributes)
46
+ assert_false k2.valid?
47
+ end
48
+
49
+ # TODO: Get some introspection of validations happening
50
+ should "allow_non duplicate data" do
51
+ k = Veneer(@klass).new(@valid_attributes)
52
+ assert_true k.save
53
+ k2 = Veneer(@klass).new(@valid_attributes.merge(:name => "not a duplicate"))
54
+ assert_true k2.save
55
+ end
56
+ end
57
+
58
+ context "validates_confirmation_of" do
59
+ setup do
60
+ key = [@klass, :validates_confirmation_of, :password]
61
+ unless $validations.include?(key)
62
+ Veneer(@klass).validates_confirmation_of(:password)
63
+ $validations << key
64
+ end
65
+ end
66
+
67
+ should "be invalid without confirmation" do
68
+ k = Veneer(@klass).new(@valid_attributes)
69
+ k.password_confirmation = "Not Confirmed"
70
+ assert_false k.valid?
71
+ end
72
+ end
73
+
74
+ context "validates_with_method" do
75
+ setup do
76
+ key = [@klass, :validates_with_method, :v_with_m_test]
77
+ unless $validations.include?(key)
78
+ Veneer(@klass).validates_with_method(:v_with_m_test)
79
+ $validations << key
80
+ end
81
+ end
82
+
83
+ should "be invalid with an invalid through v_with_m_test" do
84
+ k = Veneer(@klass).new(@valid_attributes)
85
+ k.name = "v_with_m_test"
86
+ assert_false k.valid?
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -1,6 +1,10 @@
1
1
  class Test::Unit::TestCase
2
2
  def self.veneer_should_implement_new_record?
3
3
  context "on an instance" do
4
+ setup do
5
+ Veneer(@klass).destroy_all
6
+ end
7
+
4
8
  should "setup the test correctly" do
5
9
  assert_not_nil @klass
6
10
  assert_kind_of Class, @klass
File without changes
data/test/test_helper.rb CHANGED
@@ -9,6 +9,8 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
9
  $LOAD_PATH.unshift(File.dirname(__FILE__))
10
10
  require 'veneer'
11
11
 
12
+ $validations = []
13
+
12
14
  class Test::Unit::TestCase
13
15
  include Veneer::Test::Helpers
14
16
  end
@@ -3,6 +3,7 @@ require 'veneer/adapters/activerecord'
3
3
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
4
4
 
5
5
  class CreateActiveRecordFoo < ActiveRecord::Migration
6
+
6
7
  def self.up
7
8
  create_table :active_record_foos, :force => true do |t|
8
9
  t.string :name
@@ -18,6 +19,7 @@ end
18
19
  CreateActiveRecordFoo.up
19
20
 
20
21
  class ActiveRecordFoo < ActiveRecord::Base
22
+ attr_accessor :password, :password_confirmation
21
23
  def self.veneer_spec_reset!
22
24
  delete_all
23
25
  end
@@ -25,4 +27,8 @@ class ActiveRecordFoo < ActiveRecord::Base
25
27
  def validate
26
28
  errors.add(:name, "Name cannot be 'invalid'") if name == "invalid"
27
29
  end
30
+
31
+ def v_with_m_test
32
+ errors.add(:name, "Name cannot be v_with_m_test") if name == "v_with_m_test"
33
+ end
28
34
  end
@@ -7,8 +7,8 @@ module Veneer
7
7
  context "Active Record Veneer Adapter" do
8
8
  setup do
9
9
  @klass = ::ActiveRecordFoo
10
- @valid_attributes = {:name => "foo"}
11
- @invalid_attributes = {:name => "invalid"}
10
+ @valid_attributes = {:name => "foo", :password => "pass", :password_confirmation => "pass"}
11
+ @invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
12
12
  end
13
13
  veneer_should_have_the_required_veneer_constants
14
14
  veneer_should_implement_create_with_valid_attributes
@@ -16,7 +16,8 @@ module Veneer
16
16
  veneer_should_implement_create_with_invalid_attributes
17
17
  veneer_should_implement_find
18
18
  veneer_should_implement_before_save_hooks
19
- end
19
+ veneer_should_implement_validations
20
+ end
20
21
  end
21
22
  end
22
23
  end
@@ -7,8 +7,8 @@ module Veneer
7
7
  context "Active Record Veneer Adapter" do
8
8
  setup do
9
9
  @klass = ::ActiveRecordFoo
10
- @valid_attributes = {:name => "foo"}
11
- @invalid_attributes = {:name => "invalid"}
10
+ @valid_attributes = {:name => "foo", :password => "pass", :password_confirmation => "pass"}
11
+ @invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
12
12
  end
13
13
 
14
14
  veneer_should_implement_new_record?
@@ -7,8 +7,8 @@ module Veneer
7
7
  context "DataMapper Veneer Adapter" do
8
8
  setup do
9
9
  @klass = DMFoo
10
- @valid_attributes = {:name => "foo"}
11
- @invalid_attributes = {:name => "invalid"}
10
+ @valid_attributes = {:name => "foo", :password => "pass", :password_confirmation => "pass"}
11
+ @invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
12
12
  end
13
13
 
14
14
  veneer_should_have_the_required_veneer_constants
@@ -17,6 +17,7 @@ module Veneer
17
17
  veneer_should_implement_create_with_invalid_attributes
18
18
  veneer_should_implement_find
19
19
  veneer_should_implement_before_save_hooks
20
+ veneer_should_implement_validations
20
21
  end
21
22
  end
22
23
  end
@@ -6,6 +6,7 @@ DataMapper.setup(:default, 'sqlite3::memory:')
6
6
 
7
7
  class DMFoo
8
8
  include DataMapper::Resource
9
+ attr_accessor :password, :password_confirmation
9
10
 
10
11
  property :id, Serial
11
12
  property :name, String
@@ -20,6 +21,14 @@ class DMFoo
20
21
  true
21
22
  end
22
23
  end
24
+
25
+ def v_with_m_test
26
+ if name == "v_with_m_test"
27
+ [false, "name cannot be v_with_m_test"]
28
+ else
29
+ true
30
+ end
31
+ end
23
32
  end
24
33
 
25
34
  DataMapper.auto_migrate!
@@ -7,8 +7,8 @@ module Veneer
7
7
  context "DataMapper Veneer Adapter" do
8
8
  setup do
9
9
  @klass = DMFoo
10
- @valid_attributes = {:name => "foo"}
11
- @invalid_attributes = {:name => "invalid"}
10
+ @valid_attributes = {:name => "foo", :password => "pass", :password_confirmation => "pass"}
11
+ @invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
12
12
  end
13
13
 
14
14
  veneer_should_implement_new_record?
@@ -7,8 +7,8 @@ module Veneer
7
7
  context "MongoMapper Veneer Adapter" do
8
8
  setup do
9
9
  @klass = MongoFoo
10
- @valid_attributes = {:name => "foo"}
11
- @invalid_attributes = {:name => "invalid"}
10
+ @valid_attributes = {:name => "foo", :password => "pass", :password_confirmation => "pass"}
11
+ @invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
12
12
  end
13
13
 
14
14
  veneer_should_have_the_required_veneer_constants
@@ -17,6 +17,7 @@ module Veneer
17
17
  veneer_should_implement_create_with_invalid_attributes
18
18
  veneer_should_implement_find
19
19
  veneer_should_implement_before_save_hooks
20
+ veneer_should_implement_validations
20
21
  end
21
22
  end
22
23
  end
@@ -7,8 +7,8 @@ module Veneer
7
7
  context "MongoMapper Veneer Adapter" do
8
8
  setup do
9
9
  @klass = MongoFoo
10
- @valid_attributes = {:name => "foo"}
11
- @invalid_attributes = {:name => "invalid"}
10
+ @valid_attributes = {:name => "foo", :password => "pass", :password_confirmation => "pass"}
11
+ @invalid_attributes = @valid_attributes.dup.merge(:name => "invalid")
12
12
  end
13
13
 
14
14
  veneer_should_implement_new_record?
@@ -7,6 +7,7 @@ MongoMapper.database = 'veneer_test'
7
7
 
8
8
  class MongoFoo
9
9
  include MongoMapper::Document
10
+ attr_accessor :password, :password_confirmation
10
11
 
11
12
  key :name, String
12
13
  key :order_field1, Integer
@@ -16,5 +17,9 @@ class MongoFoo
16
17
  def check_name
17
18
  errors.add(:name, "may not be invalid") if name == "invalid"
18
19
  end
20
+
21
+ def v_with_m_test
22
+ errors.add(:name, "may not be v_with_m_test") if name == "v_with_m_test"
23
+ end
19
24
  end
20
25
 
@@ -134,16 +134,6 @@ class VeneerProxyTest < Test::Unit::TestCase
134
134
  @invalid_attributes = {:invalid => true}
135
135
  @instance = ::Foo.new
136
136
  end
137
-
138
- veneer_should_have_the_required_veneer_constants
139
- veneer_should_implement_create_with_valid_attributes
140
- veneer_should_implement_create_with_invalid_attributes
141
- veneer_should_impelement_destroy_all
142
- veneer_should_implement_find
143
- veneer_should_implement_new_record?
144
- veneer_should_implement_save
145
- veneer_should_implement_save!
146
- veneer_should_implement_destroy
147
137
  end
148
138
 
149
139
  context "all" do
data/veneer.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{veneer}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
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"]
@@ -46,12 +46,14 @@ Gem::Specification.new do |s|
46
46
  "test/macros/class_wrapper/delete_macro.rb",
47
47
  "test/macros/class_wrapper/find_macro.rb",
48
48
  "test/macros/class_wrapper/hooks.rb",
49
+ "test/macros/class_wrapper/validations.rb",
49
50
  "test/macros/instance_wrapper/destroy_macro.rb",
50
51
  "test/macros/instance_wrapper/new_record_macro.rb",
51
52
  "test/macros/instance_wrapper/save_macro.rb",
52
53
  "test/macros/veneer_constants_interface.rb",
53
54
  "test/support/before_save_mixin.rb",
54
55
  "test/support/helpers.rb",
56
+ "test/support/valiations_method_method.rb",
55
57
  "test/test_helper.rb",
56
58
  "test/veneer/adapters/active_record/active_record_setup.rb",
57
59
  "test/veneer/adapters/active_record/class_wrapper_test.rb",
@@ -77,12 +79,14 @@ Gem::Specification.new do |s|
77
79
  "test/macros/class_wrapper/delete_macro.rb",
78
80
  "test/macros/class_wrapper/find_macro.rb",
79
81
  "test/macros/class_wrapper/hooks.rb",
82
+ "test/macros/class_wrapper/validations.rb",
80
83
  "test/macros/instance_wrapper/destroy_macro.rb",
81
84
  "test/macros/instance_wrapper/new_record_macro.rb",
82
85
  "test/macros/instance_wrapper/save_macro.rb",
83
86
  "test/macros/veneer_constants_interface.rb",
84
87
  "test/support/before_save_mixin.rb",
85
88
  "test/support/helpers.rb",
89
+ "test/support/valiations_method_method.rb",
86
90
  "test/test_helper.rb",
87
91
  "test/veneer/adapters/active_record/active_record_setup.rb",
88
92
  "test/veneer/adapters/active_record/class_wrapper_test.rb",
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Neighman
@@ -52,12 +52,14 @@ files:
52
52
  - test/macros/class_wrapper/delete_macro.rb
53
53
  - test/macros/class_wrapper/find_macro.rb
54
54
  - test/macros/class_wrapper/hooks.rb
55
+ - test/macros/class_wrapper/validations.rb
55
56
  - test/macros/instance_wrapper/destroy_macro.rb
56
57
  - test/macros/instance_wrapper/new_record_macro.rb
57
58
  - test/macros/instance_wrapper/save_macro.rb
58
59
  - test/macros/veneer_constants_interface.rb
59
60
  - test/support/before_save_mixin.rb
60
61
  - test/support/helpers.rb
62
+ - test/support/valiations_method_method.rb
61
63
  - test/test_helper.rb
62
64
  - test/veneer/adapters/active_record/active_record_setup.rb
63
65
  - test/veneer/adapters/active_record/class_wrapper_test.rb
@@ -104,12 +106,14 @@ test_files:
104
106
  - test/macros/class_wrapper/delete_macro.rb
105
107
  - test/macros/class_wrapper/find_macro.rb
106
108
  - test/macros/class_wrapper/hooks.rb
109
+ - test/macros/class_wrapper/validations.rb
107
110
  - test/macros/instance_wrapper/destroy_macro.rb
108
111
  - test/macros/instance_wrapper/new_record_macro.rb
109
112
  - test/macros/instance_wrapper/save_macro.rb
110
113
  - test/macros/veneer_constants_interface.rb
111
114
  - test/support/before_save_mixin.rb
112
115
  - test/support/helpers.rb
116
+ - test/support/valiations_method_method.rb
113
117
  - test/test_helper.rb
114
118
  - test/veneer/adapters/active_record/active_record_setup.rb
115
119
  - test/veneer/adapters/active_record/class_wrapper_test.rb