Created
November 29, 2014 12:52
-
-
Save jodosha/49f85aba480275b034b4 to your computer and use it in GitHub Desktop.
Lotus::Validations custom coercion problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler/setup' | |
require 'lotus/validations' | |
puts "Lotus::Validations::VERSION: #{ Lotus::Validations::VERSION }" | |
class Bar | |
attr_reader :baz | |
def initialize(value) | |
@baz = value | |
end | |
end | |
class FooValidator | |
include Lotus::Validations | |
attribute :bar, type: Bar | |
end | |
foo = FooValidator.new(bar: 'BAZBAZ') | |
puts "before" | |
puts foo.bar | |
puts foo.bar.class.name | |
puts foo.bar.baz | |
puts foo.bar.baz.class.name | |
puts "\n\n\n" | |
foo.valid? | |
puts "after" | |
puts foo.bar | |
puts foo.bar.class.name | |
puts foo.bar.baz | |
puts foo.bar.baz.class.name | |
__END__ | |
Lotus::Validations::VERSION: 0.2.0 | |
# CORRECT (lazy coercion) | |
before | |
#<Bar:0x007fa6359b8948> | |
Bar | |
BAZBAZ | |
String | |
# WRONG (coercion triggered by #valid?) | |
after | |
#<Bar:0x007fa6359b8240> | |
Bar | |
#<Bar:0x007fa6359b8420> | |
Bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment