lotus/validations — pull request #37 by stevehodgkiss (2014-12-29 21:49:11+00:00)
Title: Refactor to allow objects to be initialised independently

A work in progress based on https://github.com/lotus/validations/issues/36

- [x] Remove attribute tracking from the Lotus::Validations provided constructor, and provide a generic accessor setter constructor that can be overwritten easily.
- [x] Build the list of attributes and values to be validated lazily, rather than in the constructor
- [x] Move type coercion functionality to setters (run separately from validations).
- [ ] Figure out what to do about the broken tests/blank values in coersions
- [ ] Extract `.validates` method
- [ ] Document new methods

Failing tests:

format_validation_test,rb:22

```ruby
it "isn't valid if one doesn't respect given formats" do
validator = FormatValidatorTest.new({name: 'Luca', age: 'thirtytwo'})

validator.valid?.must_equal false
error = validator.errors.for(:age)
error.must_include Lotus::Validations::Error.new(:age, :format, /\A[0-9]+\z/, 'thirtytwo')
end

::Lotus::Utils::Kernel.Integer('thirtytwo') # => 0 <-- then format validator runs and passes on "0"
```

Do we really want to coerce invalid integers into 0? This would pass previously because the format validator ran before type coercion happened, but the question still applies to the Integer coercion functionality in general (without a format validator).

type_test.rb:113

```ruby
it 'returns nil when empty string is given to an integer typed attribute' do
validator = TypeValidatorTest.new(integer_attr: '')
validator.integer_attr.must_be_nil
end

::Lotus::Utils::Kernel.Integer('') # => 0 <-- due to `return nil if blank_value?` missing from the defined writer method
```

Perhaps we could move the `return nil if blank_value?` functionality to Lotus::Validations::Coercions.coerce to fix this.