validated_object 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f32a3569317b4ac6693c86bf9b4f1d30e035c7b
4
- data.tar.gz: 589c0f17eea6ddbb85518a224033485c536735c7
3
+ metadata.gz: 5b76d3f8fe9c6f252f2e6ff709800cd10cd8b6b0
4
+ data.tar.gz: 785c77bba6ede1f3c7e3f59c2b0f63605d2cb5d6
5
5
  SHA512:
6
- metadata.gz: 02104cd38f5f88d621084ce95bd7f14ec29e3d6c1dc40812f78ac584953ce98afc33d09c63a9135f6e27503e98c1cb29e7bad2786f62dd98f148d17c03a8d8ea
7
- data.tar.gz: fbec06b3948959293b669fc25708f107a9b53d69e6b47346edf32c43951915f256b53df53335b78c59dad192acba6dcb6cab4a7fb773f59dbfd95a0d890717fc
6
+ metadata.gz: 7da913249ec669171fab23b35edea1a0ce1a2916ad28db759de46d4122d4f2a1a7e62744d956702f09e7833228fe462bf2e5e7932bee510700aee049242d2e21
7
+ data.tar.gz: b3ba82c241a08f8eee58ff0aaa44f5b072f09136857664a2033ef0a5301c229340109fd52e9dd5bd1e1e09f38405c7a6bbe2f15ca8562b8f8cb74fc0268670ad
@@ -1,3 +1,3 @@
1
1
  module ValidatedObject
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,5 +1,84 @@
1
+ require 'active_model'
1
2
  require "validated_object/version"
2
3
 
3
4
  module ValidatedObject
4
- # Your code goes here...
5
+ # @abstract Subclass and add `attr_accessor` and validations
6
+ # to create custom validating objects.
7
+ #
8
+ # Uses [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates)
9
+ # to create self-validating Plain Old Ruby objects. This is especially
10
+ # useful when importing data from one system into another. This class also
11
+ # creates very readable error messages.
12
+ #
13
+ # @example Writing a self-validating object
14
+ # class Dog < Eaternet::ValidatedObject
15
+ # attr_accessor :name, :birthday
16
+ #
17
+ # validates :name, presence: true
18
+ # validates :birthday, type: Date, allow_nil: true
19
+ # end
20
+ #
21
+ # @example Instantiating and automatically validating
22
+ # # The dog1 instance validates itself at the end of instantiation.
23
+ # # Here, it succeeds and so doesn't raise an exception.
24
+ # dog1 = Dog.new do |d|
25
+ # d.name = 'Spot'
26
+ # end
27
+ #
28
+ # # We can also explicitly test for validity
29
+ # dog1.valid? # => true
30
+ #
31
+ # dog1.birthday = Date.new(2015, 1, 23)
32
+ # dog1.valid? # => true
33
+ #
34
+ # @example Making an instance invalid
35
+ # dog1.birthday = '2015-01-23'
36
+ # dog1.valid? # => false
37
+ # dog1.check_validations! # => ArgumentError: Birthday is class String, not Date
38
+ #
39
+ # @see Eaternet::ValidatedObject::TypeValidator
40
+ # @see http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/ ActiveModel: Make Any Ruby Object Feel Like ActiveRecord, Yehuda Katz
41
+ # @see http://www.rubyinside.com/rails-3-0s-activemodel-how-to-give-ruby-classes-some-activerecord-magic-2937.html Rails 3.0′s ActiveModel: How To Give Ruby Classes Some ActiveRecord Magic, Peter Cooper
42
+ class Base
43
+ include ActiveModel::Validations
44
+
45
+ # Instantiate and validate a new object.
46
+ #
47
+ # @yieldparam [ValidatedObject] new_object the yielded new object
48
+ # for configuration.
49
+ #
50
+ # @raise [ArgumentError] if the object is not valid at the
51
+ # end of initialization.
52
+ def initialize(&block)
53
+ block.call(self)
54
+ check_validations!
55
+ end
56
+
57
+ # Run any validations and raise an error if invalid.
58
+ # @raise [ArgumentError] if any validations fail.
59
+ def check_validations!
60
+ fail ArgumentError, errors.full_messages.join('; ') if invalid?
61
+ end
62
+
63
+ # A custom validator which ensures an object is a certain class.
64
+ # It's here as a nested class in {ValidatedObject} for easy
65
+ # access by subclasses.
66
+ #
67
+ # @example Ensure that weight is a floating point number
68
+ # class Dog < ValidatedObject
69
+ # attr_accessor :weight
70
+ # validates :weight, type: Float
71
+ # end
72
+ class TypeValidator < ActiveModel::EachValidator
73
+ # @return [nil]
74
+ def validate_each(record, attribute, value)
75
+ expected = options[:with]
76
+ actual = value.class
77
+ return if actual == expected
78
+
79
+ msg = options[:message] || "is class #{actual}, not #{expected}"
80
+ record.errors.add attribute, msg
81
+ end
82
+ end
83
+ end
5
84
  end
@@ -30,4 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "bundler", "~> 1.10"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "rspec"
33
+
34
+ spec.add_runtime_dependency 'activemodel', '>= 3.2.21'
33
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validated_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.21
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.21
55
69
  description: A small wrapper around ActiveModel Validations.
56
70
  email:
57
71
  - robb@weblaws.org