domain_model 0.2.1 → 0.3.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.
@@ -1,3 +1,3 @@
1
1
  module DomainModel
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/domain_model.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  module DomainModel
2
+ InvalidModel = Class.new(StandardError)
3
+
2
4
  def self.included(base)
3
5
  base.extend(ClassMethods)
4
6
  end
@@ -51,6 +53,14 @@ module DomainModel
51
53
  errors.empty?
52
54
  end
53
55
 
56
+ def valid!
57
+ cached_errors = errors # Just in case #errors is non-deterministic
58
+
59
+ if !cached_errors.empty?
60
+ raise InvalidModel.new("This #{self.class} object contains the following errors: #{cached_errors.to_hash}")
61
+ end
62
+ end
63
+
54
64
  def ==(other)
55
65
  other.is_a?(self.class) && attributes == other.attributes
56
66
  end
@@ -219,6 +229,10 @@ module DomainModel
219
229
  def as_json(*)
220
230
  @hash.clone
221
231
  end
232
+
233
+ def to_hash
234
+ @hash.clone
235
+ end
222
236
  end
223
237
 
224
238
  class FieldErrors
@@ -490,6 +490,23 @@ describe DomainModel do
490
490
  end
491
491
  end
492
492
 
493
+ describe "#valid!" do
494
+ it "raises an DomainModel::InvalidModel exception if there are errors" do
495
+ define { field :field, :required => true }
496
+ expect { Client.new.valid! }.to raise_error(DomainModel::InvalidModel)
497
+ end
498
+
499
+ it "includes a description of the errors as the error" do
500
+ define { field :field, :required => true }
501
+ expect { Client.new.valid! }.to raise_error('This Client object contains the following errors: {:field=>["cannot be nil"]}')
502
+ end
503
+
504
+ it "does not raise an excpetion if there are no errors" do
505
+ define { field :field }
506
+ expect { Client.new.valid! }.not_to raise_error
507
+ end
508
+ end
509
+
493
510
  describe "#attributes" do
494
511
  it "returns the models as a hash" do
495
512
  define { field :field }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domain_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-22 00:00:00.000000000 Z
12
+ date: 2015-03-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake