ardb 0.8.0 → 0.9.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.
@@ -29,8 +29,10 @@ module Ardb
29
29
 
30
30
  end
31
31
 
32
- [ :validates_presence_of, :validates_uniqueness_of,
33
- :validates_inclusion_of
32
+ [ :validates_acceptance_of, :validates_confirmation_of,
33
+ :validates_exclusion_of, :validates_format_of, :validates_inclusion_of,
34
+ :validates_length_of, :validates_numericality_of,
35
+ :validates_presence_of, :validates_size_of, :validates_uniqueness_of
34
36
  ].each do |method_name|
35
37
  type = method_name.to_s.match(/\Avalidates_(.+)_of\Z/)[1]
36
38
 
@@ -41,12 +43,33 @@ module Ardb
41
43
 
42
44
  end
43
45
 
46
+ def validates_associated(*args)
47
+ @validations ||= []
48
+ @validations << Validation.new(:associated, *args)
49
+ end
50
+
51
+ def validates_with(*args)
52
+ @validations ||= []
53
+ @validations << Validation.new(:with, *args)
54
+ end
55
+
56
+ def validates_each(*args, &block)
57
+ @validations ||= []
58
+ @validations << Validation.new(:each, *args, &block)
59
+ end
60
+
44
61
  def validate(method_name = nil, &block)
45
62
  @validations ||= []
46
63
  @validations << Validation.new(:custom, method_name, &block)
47
64
  end
48
65
 
49
- [ :after_initialize, :before_validation, :after_save ].each do |method_name|
66
+ [ :before_validation, :after_validation,
67
+ :before_create, :around_create, :after_create,
68
+ :before_update, :around_update, :after_update,
69
+ :before_save, :around_save, :after_save,
70
+ :before_destroy, :around_destroy, :after_destroy,
71
+ :after_initialize, :after_find
72
+ ].each do |method_name|
50
73
 
51
74
  define_method(method_name) do |*args, &block|
52
75
  @callbacks ||= []
@@ -83,16 +106,18 @@ module Ardb
83
106
  end
84
107
 
85
108
  class Validation
86
- attr_reader :type, :columns, :options, :method_name, :block
109
+ attr_reader :type, :args, :options, :method_name, :block
110
+ alias :columns :args
111
+ alias :associations :args
112
+ alias :classes :args
87
113
 
88
114
  def initialize(type, *args, &block)
89
115
  @type = type.to_sym
116
+ @options = args.last.kind_of?(::Hash) ? args.pop : {}
117
+ @args = args
90
118
  @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
119
+ if @type == :custom
120
+ @method_name = @args.first
96
121
  end
97
122
  end
98
123
  end
data/lib/ardb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ardb
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -17,9 +17,19 @@ module Ardb::RecordSpy
17
17
 
18
18
  should have_readers :associations, :callbacks, :validations
19
19
  should have_imeths :belongs_to, :has_many, :has_one
20
- should have_imeths :validates_presence_of, :validates_uniqueness_of
21
- should have_imeths :validates_inclusion_of, :validate
22
- should have_imeths :after_initialize, :before_validation, :after_save
20
+ should have_imeths :validates_acceptance_of, :validates_confirmation_of
21
+ should have_imeths :validates_exclusion_of, :validates_format_of
22
+ should have_imeths :validates_inclusion_of, :validates_length_of
23
+ should have_imeths :validates_numericality_of, :validates_presence_of
24
+ should have_imeths :validates_size_of, :validates_uniqueness_of
25
+ should have_imeths :validates_associated, :validates_with, :validates_each
26
+ should have_imeths :validate
27
+ should have_imeths :before_validation, :after_validation
28
+ should have_imeths :before_create, :around_create, :after_create
29
+ should have_imeths :before_update, :around_update, :after_update
30
+ should have_imeths :before_save, :around_save, :after_save
31
+ should have_imeths :before_destroy, :around_destroy, :after_destroy
32
+ should have_imeths :after_initialize, :after_find
23
33
 
24
34
  should "included the record spy instance methods" do
25
35
  assert_includes Ardb::RecordSpy::InstanceMethods, subject.included_modules
@@ -53,25 +63,37 @@ module Ardb::RecordSpy
53
63
  subject.validates_presence_of :name, :email, :on => :create
54
64
  validation = subject.validations.last
55
65
  assert_equal :presence, validation.type
56
- assert_includes :name, validation.columns
66
+ assert_equal :create, validation.options[:on]
67
+ assert_includes :name, validation.columns
57
68
  assert_includes :email, validation.columns
58
- assert_equal :create, validation.options[:on]
59
69
  end
60
70
 
61
- should "add a validation config with #validates_uniqueness_of" do
62
- subject.validates_uniqueness_of :name, :scope => :area_id
71
+ should "add a validation config with #validates_associated" do
72
+ subject.validates_associated :area, :linkings
63
73
  validation = subject.validations.last
64
- assert_equal :uniqueness, validation.type
65
- assert_includes :name, validation.columns
66
- assert_equal :area_id, validation.options[:scope]
74
+ assert_equal :associated, validation.type
75
+ assert_includes :area, validation.associations
76
+ assert_includes :linkings, validation.associations
67
77
  end
68
78
 
69
- should "add a validation config with #validates_inclusion_of" do
70
- subject.validates_inclusion_of :active, :in => [ true, false]
79
+ should "add a validation config with #validates_associated" do
80
+ first_validation_class = Class.new
81
+ second_validation_class = Class.new
82
+ subject.validates_with first_validation_class, second_validation_class
71
83
  validation = subject.validations.last
72
- assert_equal :inclusion, validation.type
73
- assert_includes :active, validation.columns
74
- assert_equal [ true, false], validation.options[:in]
84
+ assert_equal :with, validation.type
85
+ assert_includes first_validation_class, validation.classes
86
+ assert_includes second_validation_class, validation.classes
87
+ end
88
+
89
+ should "add a validation config with #validates_each" do
90
+ block = proc{ }
91
+ subject.validates_each(:name, :email, &block)
92
+ validation = subject.validations.last
93
+ assert_equal :each, validation.type
94
+ assert_equal block, validation.block
95
+ assert_includes :name, validation.columns
96
+ assert_includes :email, validation.columns
75
97
  end
76
98
 
77
99
  should "add a validation config with #validate" do
@@ -80,11 +102,11 @@ module Ardb::RecordSpy
80
102
  assert_equal :custom, validation.type
81
103
  assert_equal :some_method, validation.method_name
82
104
 
83
- proc = proc{ }
84
- subject.validate(&proc)
105
+ block = proc{ }
106
+ subject.validate(&block)
85
107
  validation = subject.validations.last
86
108
  assert_equal :custom, validation.type
87
- assert_equal proc, validation.block
109
+ assert_equal block, validation.block
88
110
  end
89
111
 
90
112
  should "add a callback config with #after_initialize" do
@@ -105,13 +127,6 @@ module Ardb::RecordSpy
105
127
  assert_equal 'test', @instance.name
106
128
  end
107
129
 
108
- should "add a callback config with #after_save" do
109
- subject.after_save :a_callback_method
110
- callback = subject.callbacks.last
111
- assert_equal :after_save, callback.type
112
- assert_includes :a_callback_method, callback.args
113
- end
114
-
115
130
  end
116
131
 
117
132
  class GeneratorTests < BaseTests
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: 63
4
+ hash: 59
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 8
8
+ - 9
9
9
  - 0
10
- version: 0.8.0
10
+ version: 0.9.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-19 00:00:00 Z
19
+ date: 2013-06-20 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  prerelease: false