Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created November 29, 2014 12:52
Show Gist options
  • Save jodosha/49f85aba480275b034b4 to your computer and use it in GitHub Desktop.
Save jodosha/49f85aba480275b034b4 to your computer and use it in GitHub Desktop.
Lotus::Validations custom coercion problem
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