ardb 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ardb/record_spy.rb +73 -0
- data/lib/ardb/version.rb +1 -1
- data/test/unit/record_spy_tests.rb +90 -0
- metadata +20 -17
@@ -0,0 +1,73 @@
|
|
1
|
+
module Ardb
|
2
|
+
|
3
|
+
module RecordSpy
|
4
|
+
|
5
|
+
def self.new(&block)
|
6
|
+
block ||= proc{ }
|
7
|
+
record_spy = Class.new{ include Ardb::RecordSpy }
|
8
|
+
record_spy.class_eval(&block)
|
9
|
+
record_spy
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.included(klass)
|
13
|
+
klass.class_eval do
|
14
|
+
extend ClassMethods
|
15
|
+
include InstanceMethods
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
|
21
|
+
attr_reader :validations, :callbacks
|
22
|
+
|
23
|
+
[ :validates_presence_of, :validates_uniqueness_of,
|
24
|
+
:validates_inclusion_of
|
25
|
+
].each do |method_name|
|
26
|
+
type = method_name.to_s.match(/\Avalidates_(.+)_of\Z/)[1]
|
27
|
+
|
28
|
+
define_method(method_name) do |*args|
|
29
|
+
@validations ||= []
|
30
|
+
@validations << Validation.new(type, *args)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
[ :before_validation, :after_save ].each do |method_name|
|
36
|
+
|
37
|
+
define_method(method_name) do |*args, &block|
|
38
|
+
@callbacks ||= []
|
39
|
+
@callbacks << Callback.new(method_name, *args, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
module InstanceMethods
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
class Validation
|
51
|
+
attr_reader :type, :columns, :options
|
52
|
+
|
53
|
+
def initialize(type, *args)
|
54
|
+
@type = type.to_sym
|
55
|
+
@options = args.last.kind_of?(::Hash) ? args.pop : {}
|
56
|
+
@columns = args
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Callback
|
61
|
+
attr_reader :type, :args, :options, :block
|
62
|
+
|
63
|
+
def initialize(type, *args, &block)
|
64
|
+
@type = type.to_sym
|
65
|
+
@options = args.last.kind_of?(::Hash) ? args.pop : {}
|
66
|
+
@args = args
|
67
|
+
@block = block
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
data/lib/ardb/version.rb
CHANGED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ardb/record_spy'
|
3
|
+
|
4
|
+
module Ardb::RecordSpy
|
5
|
+
|
6
|
+
class MyRecord
|
7
|
+
include Ardb::RecordSpy
|
8
|
+
attr_accessor :name
|
9
|
+
end
|
10
|
+
|
11
|
+
class BaseTests < Assert::Context
|
12
|
+
desc "Ardb::RecordSpy"
|
13
|
+
setup do
|
14
|
+
@instance = MyRecord.new
|
15
|
+
end
|
16
|
+
subject{ MyRecord }
|
17
|
+
|
18
|
+
should have_readers :validations, :callbacks
|
19
|
+
should have_imeths :validates_presence_of, :validates_uniqueness_of
|
20
|
+
should have_imeths :validates_inclusion_of, :before_validation, :after_save
|
21
|
+
|
22
|
+
should "included the record spy instance methods" do
|
23
|
+
assert_includes Ardb::RecordSpy::InstanceMethods, subject.included_modules
|
24
|
+
end
|
25
|
+
|
26
|
+
should "add a validation config with #validates_presence_of" do
|
27
|
+
subject.validates_presence_of :name, :email, :on => :create
|
28
|
+
validation = subject.validations.last
|
29
|
+
assert_equal :presence, validation.type
|
30
|
+
assert_includes :name, validation.columns
|
31
|
+
assert_includes :email, validation.columns
|
32
|
+
assert_equal :create, validation.options[:on]
|
33
|
+
end
|
34
|
+
|
35
|
+
should "add a validation config with #validates_uniqueness_of" do
|
36
|
+
subject.validates_uniqueness_of :name, :scope => :area_id
|
37
|
+
validation = subject.validations.last
|
38
|
+
assert_equal :uniqueness, validation.type
|
39
|
+
assert_includes :name, validation.columns
|
40
|
+
assert_equal :area_id, validation.options[:scope]
|
41
|
+
end
|
42
|
+
|
43
|
+
should "add a validation config with #validates_inclusion_of" do
|
44
|
+
subject.validates_inclusion_of :active, :in => [ true, false]
|
45
|
+
validation = subject.validations.last
|
46
|
+
assert_equal :inclusion, validation.type
|
47
|
+
assert_includes :active, validation.columns
|
48
|
+
assert_equal [ true, false], validation.options[:in]
|
49
|
+
end
|
50
|
+
|
51
|
+
should "add a callback config with #before_validation" do
|
52
|
+
subject.before_validation(:on => :create) do
|
53
|
+
self.name = 'test'
|
54
|
+
end
|
55
|
+
callback = subject.callbacks.last
|
56
|
+
assert_equal :before_validation, callback.type
|
57
|
+
assert_equal :create, callback.options[:on]
|
58
|
+
@instance.instance_eval(&callback.block)
|
59
|
+
assert_equal 'test', @instance.name
|
60
|
+
end
|
61
|
+
|
62
|
+
should "add a callback config with #after_save" do
|
63
|
+
subject.after_save :a_callback_method
|
64
|
+
callback = subject.callbacks.last
|
65
|
+
assert_equal :after_save, callback.type
|
66
|
+
assert_includes :a_callback_method, callback.args
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
class GeneratorTests < BaseTests
|
72
|
+
desc "to generate record spy classes"
|
73
|
+
setup do
|
74
|
+
@record_spy_class = Ardb::RecordSpy.new do
|
75
|
+
attr_accessor :name
|
76
|
+
end
|
77
|
+
@instance = @record_spy_class.new
|
78
|
+
end
|
79
|
+
subject{ @record_spy_class }
|
80
|
+
|
81
|
+
should "build a new record spy class and " \
|
82
|
+
"used the passed proc to further defined it" do
|
83
|
+
assert_includes Ardb::RecordSpy, subject.included_modules
|
84
|
+
assert @instance.respond_to? :name
|
85
|
+
assert @instance.respond_to? :name=
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ardb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 7
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.7.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -16,12 +16,11 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2013-
|
19
|
+
date: 2013-06-17 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name: assert
|
23
22
|
prerelease: false
|
24
|
-
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
24
|
none: false
|
26
25
|
requirements:
|
27
26
|
- - ~>
|
@@ -31,12 +30,12 @@ dependencies:
|
|
31
30
|
- 2
|
32
31
|
- 0
|
33
32
|
version: "2.0"
|
33
|
+
requirement: *id001
|
34
|
+
name: assert
|
34
35
|
type: :development
|
35
|
-
version_requirements: *id001
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name: activerecord
|
38
37
|
prerelease: false
|
39
|
-
|
38
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
39
|
none: false
|
41
40
|
requirements:
|
42
41
|
- - ~>
|
@@ -46,12 +45,12 @@ dependencies:
|
|
46
45
|
- 3
|
47
46
|
- 2
|
48
47
|
version: "3.2"
|
48
|
+
requirement: *id002
|
49
|
+
name: activerecord
|
49
50
|
type: :runtime
|
50
|
-
version_requirements: *id002
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
|
-
name: activesupport
|
53
52
|
prerelease: false
|
54
|
-
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
55
54
|
none: false
|
56
55
|
requirements:
|
57
56
|
- - ~>
|
@@ -61,12 +60,12 @@ dependencies:
|
|
61
60
|
- 3
|
62
61
|
- 2
|
63
62
|
version: "3.2"
|
63
|
+
requirement: *id003
|
64
|
+
name: activesupport
|
64
65
|
type: :runtime
|
65
|
-
version_requirements: *id003
|
66
66
|
- !ruby/object:Gem::Dependency
|
67
|
-
name: ns-options
|
68
67
|
prerelease: false
|
69
|
-
|
68
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
70
69
|
none: false
|
71
70
|
requirements:
|
72
71
|
- - ~>
|
@@ -76,8 +75,9 @@ dependencies:
|
|
76
75
|
- 1
|
77
76
|
- 1
|
78
77
|
version: "1.1"
|
78
|
+
requirement: *id004
|
79
|
+
name: ns-options
|
79
80
|
type: :runtime
|
80
|
-
version_requirements: *id004
|
81
81
|
description: Activerecord database tools.
|
82
82
|
email:
|
83
83
|
- kelly@kellyredding.com
|
@@ -103,6 +103,7 @@ files:
|
|
103
103
|
- lib/ardb/adapter/sqlite.rb
|
104
104
|
- lib/ardb/cli.rb
|
105
105
|
- lib/ardb/migration_helpers.rb
|
106
|
+
- lib/ardb/record_spy.rb
|
106
107
|
- lib/ardb/root_path.rb
|
107
108
|
- lib/ardb/runner.rb
|
108
109
|
- lib/ardb/runner/connect_command.rb
|
@@ -121,6 +122,7 @@ files:
|
|
121
122
|
- test/unit/ardb_tests.rb
|
122
123
|
- test/unit/config_tests.rb
|
123
124
|
- test/unit/migration_helpers_tests.rb
|
125
|
+
- test/unit/record_spy_tests.rb
|
124
126
|
- test/unit/runner/connect_command_tests.rb
|
125
127
|
- test/unit/runner/create_command_tests.rb
|
126
128
|
- test/unit/runner/drop_command_tests.rb
|
@@ -169,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
171
|
requirements: []
|
170
172
|
|
171
173
|
rubyforge_project:
|
172
|
-
rubygems_version: 1.8.
|
174
|
+
rubygems_version: 1.8.15
|
173
175
|
signing_key:
|
174
176
|
specification_version: 3
|
175
177
|
summary: Activerecord database tools.
|
@@ -182,6 +184,7 @@ test_files:
|
|
182
184
|
- test/unit/ardb_tests.rb
|
183
185
|
- test/unit/config_tests.rb
|
184
186
|
- test/unit/migration_helpers_tests.rb
|
187
|
+
- test/unit/record_spy_tests.rb
|
185
188
|
- test/unit/runner/connect_command_tests.rb
|
186
189
|
- test/unit/runner/create_command_tests.rb
|
187
190
|
- test/unit/runner/drop_command_tests.rb
|