ardb 0.7.0 → 0.8.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.
@@ -18,7 +18,16 @@ module Ardb
18
18
 
19
19
  module ClassMethods
20
20
 
21
- attr_reader :validations, :callbacks
21
+ attr_reader :associations, :callbacks, :validations
22
+
23
+ [ :belongs_to, :has_many, :has_one ].each do |method_name|
24
+
25
+ define_method(method_name) do |*args|
26
+ @associations ||= []
27
+ @associations << Association.new(method_name, *args)
28
+ end
29
+
30
+ end
22
31
 
23
32
  [ :validates_presence_of, :validates_uniqueness_of,
24
33
  :validates_inclusion_of
@@ -32,7 +41,12 @@ module Ardb
32
41
 
33
42
  end
34
43
 
35
- [ :before_validation, :after_save ].each do |method_name|
44
+ def validate(method_name = nil, &block)
45
+ @validations ||= []
46
+ @validations << Validation.new(:custom, method_name, &block)
47
+ end
48
+
49
+ [ :after_initialize, :before_validation, :after_save ].each do |method_name|
36
50
 
37
51
  define_method(method_name) do |*args, &block|
38
52
  @callbacks ||= []
@@ -47,13 +61,13 @@ module Ardb
47
61
 
48
62
  end
49
63
 
50
- class Validation
51
- attr_reader :type, :columns, :options
64
+ class Association
65
+ attr_reader :type, :name, :options
52
66
 
53
- def initialize(type, *args)
54
- @type = type.to_sym
55
- @options = args.last.kind_of?(::Hash) ? args.pop : {}
56
- @columns = args
67
+ def initialize(type, name, options)
68
+ @type = type.to_sym
69
+ @name = name
70
+ @options = options
57
71
  end
58
72
  end
59
73
 
@@ -68,6 +82,21 @@ module Ardb
68
82
  end
69
83
  end
70
84
 
85
+ class Validation
86
+ attr_reader :type, :columns, :options, :method_name, :block
87
+
88
+ def initialize(type, *args, &block)
89
+ @type = type.to_sym
90
+ @block = block
91
+ if type != :custom
92
+ @options = args.last.kind_of?(::Hash) ? args.pop : {}
93
+ @columns = args
94
+ else
95
+ @method_name = args.first
96
+ end
97
+ end
98
+ end
99
+
71
100
  end
72
101
 
73
102
  end
@@ -1,3 +1,3 @@
1
1
  module Ardb
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -28,6 +28,7 @@ module Ardb
28
28
  end
29
29
 
30
30
  should "init the adapter on init" do
31
+ Adapter.reset
31
32
  assert_nil Adapter.current
32
33
  begin
33
34
  subject.init
@@ -15,14 +15,40 @@ module Ardb::RecordSpy
15
15
  end
16
16
  subject{ MyRecord }
17
17
 
18
- should have_readers :validations, :callbacks
18
+ should have_readers :associations, :callbacks, :validations
19
+ should have_imeths :belongs_to, :has_many, :has_one
19
20
  should have_imeths :validates_presence_of, :validates_uniqueness_of
20
- should have_imeths :validates_inclusion_of, :before_validation, :after_save
21
+ should have_imeths :validates_inclusion_of, :validate
22
+ should have_imeths :after_initialize, :before_validation, :after_save
21
23
 
22
24
  should "included the record spy instance methods" do
23
25
  assert_includes Ardb::RecordSpy::InstanceMethods, subject.included_modules
24
26
  end
25
27
 
28
+ should "add an association config with #belongs_to" do
29
+ subject.belongs_to :area, :foreign_key => :area_id
30
+ association = subject.associations.last
31
+ assert_equal :belongs_to, association.type
32
+ assert_equal :area, association.name
33
+ assert_equal :area_id, association.options[:foreign_key]
34
+ end
35
+
36
+ should "add an association config with #has_many" do
37
+ subject.has_many :comments, :as => :parent
38
+ association = subject.associations.last
39
+ assert_equal :has_many, association.type
40
+ assert_equal :comments, association.name
41
+ assert_equal :parent, association.options[:as]
42
+ end
43
+
44
+ should "add an association config with #has_one" do
45
+ subject.has_one :linking, :class_name => 'Linking'
46
+ association = subject.associations.last
47
+ assert_equal :has_one, association.type
48
+ assert_equal :linking, association.name
49
+ assert_equal 'Linking', association.options[:class_name]
50
+ end
51
+
26
52
  should "add a validation config with #validates_presence_of" do
27
53
  subject.validates_presence_of :name, :email, :on => :create
28
54
  validation = subject.validations.last
@@ -48,6 +74,26 @@ module Ardb::RecordSpy
48
74
  assert_equal [ true, false], validation.options[:in]
49
75
  end
50
76
 
77
+ should "add a validation config with #validate" do
78
+ subject.validate :some_method
79
+ validation = subject.validations.last
80
+ assert_equal :custom, validation.type
81
+ assert_equal :some_method, validation.method_name
82
+
83
+ proc = proc{ }
84
+ subject.validate(&proc)
85
+ validation = subject.validations.last
86
+ assert_equal :custom, validation.type
87
+ assert_equal proc, validation.block
88
+ end
89
+
90
+ should "add a callback config with #after_initialize" do
91
+ subject.after_initialize :a_callback_method
92
+ callback = subject.callbacks.last
93
+ assert_equal :after_initialize, callback.type
94
+ assert_includes :a_callback_method, callback.args
95
+ end
96
+
51
97
  should "add a callback config with #before_validation" do
52
98
  subject.before_validation(:on => :create) do
53
99
  self.name = 'test'
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: 3
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 7
8
+ - 8
9
9
  - 0
10
- version: 0.7.0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-06-17 00:00:00 Z
19
+ date: 2013-06-19 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  prerelease: false