ohm 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,33 +1,9 @@
1
1
  require "rake/testtask"
2
- require 'rake/gempackagetask'
3
2
 
4
3
  task :default => :test
5
4
 
6
- gem_spec_file = 'ohm.gemspec'
7
- gem_spec = eval(File.read(gem_spec_file)) rescue nil
8
-
9
5
  desc 'Run all tests'
10
6
  Rake::TestTask.new(:test) do |t|
11
7
  t.pattern = 'test/**/*_test.rb'
12
8
  t.verbose = false
13
9
  end
14
-
15
- Rake::GemPackageTask.new(gem_spec) do |pkg|
16
- pkg.need_zip = false
17
- pkg.need_tar = false
18
- rm_f FileList['pkg/**/*.*']
19
- end if gem_spec
20
-
21
- desc "Generate the gemspec file."
22
- task :gemspec do
23
- require 'erb'
24
-
25
- File.open(gem_spec_file, 'w') do |f|
26
- f.write ERB.new(File.read("#{gem_spec_file}.erb")).result(binding)
27
- end
28
- end
29
-
30
- desc "Builds and installs the gem."
31
- task :install => :repackage do
32
- `sudo gem install pkg/#{gem_spec.name}-#{gem_spec.version}.gem`
33
- end
@@ -141,6 +141,7 @@ module Ohm
141
141
 
142
142
  def disconnect
143
143
  @sock.close
144
+ @sock = nil
144
145
  end
145
146
 
146
147
  def reconnect
@@ -1,5 +1,62 @@
1
1
  module Ohm
2
2
  module Validations
3
+ class Presenter
4
+ class UnhandledErrors < StandardError
5
+ attr :errors
6
+
7
+ def initialize(errors)
8
+ @errors = errors
9
+ end
10
+
11
+ def message
12
+ "Unhandled errors: #{errors.inspect}"
13
+ end
14
+ end
15
+
16
+ def initialize(errors)
17
+ @errors = errors
18
+ @unhandled = errors.dup
19
+ @output = []
20
+ end
21
+
22
+ def on(error, message = (block_given? ? yield : raise(ArgumentError)))
23
+ handle(error) do
24
+ @output << message
25
+ end
26
+ end
27
+
28
+ def ignore(error)
29
+ handle(error)
30
+ end
31
+
32
+ def present
33
+ yield(self)
34
+ raise UnhandledErrors.new(@unhandled) unless @unhandled.empty?
35
+ @output
36
+ end
37
+
38
+ protected
39
+
40
+ def handle(error)
41
+ if (errors = @errors.select {|e| error === e }).any?
42
+ @unhandled -= errors
43
+ yield(errors) if block_given?
44
+ end
45
+ end
46
+ end
47
+
48
+ class Errors < Array
49
+ attr_accessor :model
50
+
51
+ def initialize(model)
52
+ @model = model
53
+ end
54
+
55
+ def present(presenter = Presenter, &block)
56
+ presenter.new(model.errors).present(&block)
57
+ end
58
+ end
59
+
3
60
  def valid?
4
61
  errors.clear
5
62
  validate
@@ -10,7 +67,7 @@ module Ohm
10
67
  end
11
68
 
12
69
  def errors
13
- @errors ||= []
70
+ @errors ||= Errors.new(self)
14
71
  end
15
72
 
16
73
  protected
@@ -1 +1 @@
1
- 6304
1
+ 36980
@@ -0,0 +1,120 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class ErrorsTest < Test::Unit::TestCase
4
+ class User < Ohm::Model
5
+ attribute :name
6
+ attribute :account
7
+
8
+ def validate
9
+ assert_present :name
10
+ assert_present :account
11
+ assert false, :terrible_error
12
+ end
13
+ end
14
+
15
+ setup do
16
+ @model = User.new(:account => "")
17
+ @model.valid?
18
+ end
19
+
20
+ context "errors handler" do
21
+ should "raise an error if the errors are not handled" do
22
+ assert_raise Ohm::Validations::Presenter::UnhandledErrors do
23
+ @model.errors.present do |e|
24
+ e.on :terrible_error do
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ should "evaluate blocks when errors match" do
31
+ values = []
32
+
33
+ @model.errors.present do |e|
34
+ e.on [:name, :nil] do
35
+ values << 1
36
+ end
37
+
38
+ e.on [:account, :empty] do
39
+ values << 2
40
+ end
41
+
42
+ e.on :terrible_error do
43
+ values << 3
44
+ end
45
+ end
46
+
47
+ assert_equal [1, 2, 3], values
48
+ end
49
+
50
+ should "accept case-like matches for an error" do
51
+ values = []
52
+
53
+ @model.errors.present do |e|
54
+ e.on Array do
55
+ values << 1
56
+ end
57
+
58
+ e.on :terrible_error do
59
+ values << 3
60
+ end
61
+ end
62
+
63
+ assert_equal [1, 3], values
64
+ end
65
+
66
+ should "accept multiple matches for an error" do
67
+ values = @model.errors.present do |e|
68
+ e.on [:name, :nil], "A"
69
+ e.on [:account, :empty] do
70
+ "B"
71
+ end
72
+ e.on :terrible_error, "C"
73
+ end
74
+
75
+ assert_equal %w{A B C}, values
76
+ end
77
+
78
+ class MyPresenter < Ohm::Validations::Presenter
79
+ def on(*args)
80
+ super(*args) do
81
+ yield.downcase
82
+ end
83
+ end
84
+ end
85
+
86
+ should "take a custom presenter" do
87
+ values = @model.errors.present(MyPresenter) do |e|
88
+ e.on([:name, :nil]) { "A" }
89
+ e.on([:account, :empty]) { "B" }
90
+ e.on(:terrible_error) { "C" }
91
+ end
92
+
93
+ assert_equal %w{a b c}, values
94
+ end
95
+
96
+ should "raise an error if neither a message nor a block are supplied" do
97
+ assert_raise ArgumentError do
98
+ Ohm::Validations::Presenter.new([:custom]).present do |e|
99
+ e.on(:custom)
100
+ end
101
+ end
102
+ end
103
+
104
+ should "not raise an error if the message passed is nil" do
105
+ values = Ohm::Validations::Presenter.new([:custom]).present do |e|
106
+ e.on(:custom, nil)
107
+ end
108
+
109
+ assert_equal [nil], values
110
+
111
+ assert_nothing_raised do
112
+ Ohm::Validations::Presenter.new([:custom]).present do |e|
113
+ e.on(:custom, nil) do
114
+ raise "Should not call block"
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-06-19 00:00:00 -03:00
13
+ date: 2009-06-22 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -35,6 +35,7 @@ files:
35
35
  - test/benchmarks.rb
36
36
  - test/db/dump.rdb
37
37
  - test/db/redis.pid
38
+ - test/errors_test.rb
38
39
  - test/indices_test.rb
39
40
  - test/model_test.rb
40
41
  - test/redis_test.rb