validated_object 0.1.1 → 1.0.0

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: 5b76d3f8fe9c6f252f2e6ff709800cd10cd8b6b0
4
- data.tar.gz: 785c77bba6ede1f3c7e3f59c2b0f63605d2cb5d6
3
+ metadata.gz: 917e06893558682ef44f4ae1570d73bb265dc980
4
+ data.tar.gz: b622a865760b6b3626ed1f270313ef26686371f6
5
5
  SHA512:
6
- metadata.gz: 7da913249ec669171fab23b35edea1a0ce1a2916ad28db759de46d4122d4f2a1a7e62744d956702f09e7833228fe462bf2e5e7932bee510700aee049242d2e21
7
- data.tar.gz: b3ba82c241a08f8eee58ff0aaa44f5b072f09136857664a2033ef0a5301c229340109fd52e9dd5bd1e1e09f38405c7a6bbe2f15ca8562b8f8cb74fc0268670ad
6
+ metadata.gz: 95affa10a6d3ee9efc02ced04778a8df4adab2e03a914086afbe79717c679454766be1aae77d2aaa7f7da670b82ed5baf9dc20bf0a0b6c6b0c0527123ab2c732
7
+ data.tar.gz: 079acd77f94c7c4664534a489fe5525ad47ab38883e666ea8575065138ce158c6d4b73a42d14b9ed175400e8e8251db2a44ff3b11811fb45229444fe6712d64d
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
+ [![Gem Version](https://badge.fury.io/rb/validated_object.svg)](https://badge.fury.io/rb/validated_object) [![Build Status](https://travis-ci.org/dogweather/validated_object.svg?branch=master)](https://travis-ci.org/dogweather/validated_object) [![Code Climate](https://codeclimate.com/github/dogweather/validated_object/badges/gpa.svg)](https://codeclimate.com/github/dogweather/validated_object)
2
+
1
3
  # ValidatedObject
2
4
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/validated_object`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ Uses
6
+ [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates)
7
+ to create self-validating Plain Old Ruby objects. I wrote it for helping with CSV data imports into my Rails apps.
8
+ Very readable error messages are also important in that context, to track down parsing errors. This gem provides those too.
4
9
 
5
- TODO: Delete this and the text above, and describe your gem
6
10
 
7
11
  ## Installation
8
12
 
@@ -22,20 +26,56 @@ Or install it yourself as:
22
26
 
23
27
  ## Usage
24
28
 
25
- TODO: Write usage instructions here
29
+
30
+ ### Writing a self-validating object
31
+
32
+ All of the [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) are available, plus a new one, `TypeValidator`.
33
+
34
+ ```ruby
35
+ class Dog < ValidatedObject::Base
36
+ attr_accessor :name, :birthday
37
+
38
+ validates :name, presence: true
39
+ validates :birthday, type: Date, allow_nil: true
40
+ end
41
+ ```
42
+
43
+ ### Instantiating and automatically validating
44
+
45
+ ```ruby
46
+ # The dog1 instance validates itself at the end of instantiation.
47
+ # Here, it succeeds and so doesn't raise an exception.
48
+ dog1 = Dog.new do |d|
49
+ d.name = 'Spot'
50
+ end
51
+
52
+ # We can also explicitly test for validity
53
+ dog1.valid? # => true
54
+
55
+ dog1.birthday = Date.new(2015, 1, 23)
56
+ dog1.valid? # => true
57
+ ```
58
+
59
+ ### Making an instance _invalid_
60
+
61
+ ```ruby
62
+ dog1.birthday = '2015-01-23'
63
+ dog1.valid? # => false
64
+ dog1.check_validations! # => ArgumentError: Birthday is class String, not Date
65
+ ```
66
+
26
67
 
27
68
  ## Development
28
69
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+ (TODO: Verify these instructions.) After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
71
 
31
72
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
73
 
33
74
  ## Contributing
34
75
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validated_object.
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dogweather/validated_object.
36
77
 
37
78
 
38
79
  ## License
39
80
 
40
81
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "validated_object"
3
+ require 'bundler/setup'
4
+ require 'validated_object'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "validated_object"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
@@ -1,3 +1,3 @@
1
1
  module ValidatedObject
2
- VERSION = "0.1.1"
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require 'active_model'
2
- require "validated_object/version"
2
+ require 'validated_object/version'
3
3
 
4
4
  module ValidatedObject
5
5
  # @abstract Subclass and add `attr_accessor` and validations
@@ -11,7 +11,7 @@ module ValidatedObject
11
11
  # creates very readable error messages.
12
12
  #
13
13
  # @example Writing a self-validating object
14
- # class Dog < Eaternet::ValidatedObject
14
+ # class Dog < ValidatedObject::Base
15
15
  # attr_accessor :name, :birthday
16
16
  #
17
17
  # validates :name, presence: true
@@ -36,7 +36,7 @@ module ValidatedObject
36
36
  # dog1.valid? # => false
37
37
  # dog1.check_validations! # => ArgumentError: Birthday is class String, not Date
38
38
  #
39
- # @see Eaternet::ValidatedObject::TypeValidator
39
+ # @see ValidatedObject::Base::TypeValidator
40
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
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
42
  class Base
@@ -49,34 +49,36 @@ module ValidatedObject
49
49
  #
50
50
  # @raise [ArgumentError] if the object is not valid at the
51
51
  # end of initialization.
52
- def initialize(&block)
53
- block.call(self)
52
+ def initialize
53
+ yield(self)
54
54
  check_validations!
55
+ self
55
56
  end
56
57
 
57
58
  # Run any validations and raise an error if invalid.
58
59
  # @raise [ArgumentError] if any validations fail.
59
60
  def check_validations!
60
- fail ArgumentError, errors.full_messages.join('; ') if invalid?
61
+ raise ArgumentError, errors.full_messages.join('; ') if invalid?
62
+ self
61
63
  end
62
64
 
63
- # A custom validator which ensures an object is a certain class.
65
+ # A custom validator which ensures an object is an instance of a class
66
+ # or a subclass.
64
67
  # It's here as a nested class in {ValidatedObject} for easy
65
- # access by subclasses.
68
+ # access by subclasses of {ValidatedObject::Base}.
66
69
  #
67
- # @example Ensure that weight is a floating point number
68
- # class Dog < ValidatedObject
70
+ # @example Ensure that weight is a number
71
+ # class Dog < ValidatedObject::Base
69
72
  # attr_accessor :weight
70
- # validates :weight, type: Float
73
+ # validates :weight, type: Numeric
71
74
  # end
72
75
  class TypeValidator < ActiveModel::EachValidator
73
76
  # @return [nil]
74
77
  def validate_each(record, attribute, value)
75
78
  expected = options[:with]
76
- actual = value.class
77
- return if actual == expected
79
+ return if value.kind_of?(expected)
78
80
 
79
- msg = options[:message] || "is class #{actual}, not #{expected}"
81
+ msg = options[:message] || "is class #{value.class}, not #{expected}"
80
82
  record.errors.add attribute, msg
81
83
  end
82
84
  end
@@ -4,32 +4,32 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'validated_object/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "validated_object"
7
+ spec.name = 'validated_object'
8
8
  spec.version = ValidatedObject::VERSION
9
- spec.authors = ["Robb Shecter"]
10
- spec.email = ["robb@weblaws.org"]
9
+ spec.authors = ['Robb Shecter']
10
+ spec.email = ['robb@weblaws.org']
11
11
 
12
- spec.summary = %q{Self-validating plain Ruby objects.}
13
- spec.description = %q{A small wrapper around ActiveModel Validations.}
14
- spec.homepage = "https://github.com/dogweather/validated_object"
15
- spec.license = "MIT"
12
+ spec.summary = 'Self-validating plain Ruby objects.'
13
+ spec.description = 'A small wrapper around ActiveModel Validations.'
14
+ spec.homepage = 'https://github.com/dogweather/validated_object'
15
+ spec.license = 'MIT'
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
18
  # delete this section to allow pushing this gem to any host.
19
19
  if spec.respond_to?(:metadata)
20
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
21
  else
22
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
23
  end
24
24
 
25
25
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = "exe"
26
+ spec.bindir = 'exe'
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
28
+ spec.require_paths = ['lib']
29
29
 
30
- spec.add_development_dependency "bundler", "~> 1.10"
31
- spec.add_development_dependency "rake", "~> 10.0"
32
- spec.add_development_dependency "rspec"
30
+ spec.add_development_dependency 'bundler', '~> 1.10'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec'
33
33
 
34
34
  spec.add_runtime_dependency 'activemodel', '>= 3.2.21'
35
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validated_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-18 00:00:00.000000000 Z
11
+ date: 2016-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,8 +106,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  requirements: []
108
108
  rubyforge_project:
109
- rubygems_version: 2.4.8
109
+ rubygems_version: 2.5.0
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Self-validating plain Ruby objects.
113
113
  test_files: []
114
+ has_rdoc: