Created
February 6, 2015 00:26
-
-
Save dchandekstark/eb835d7d4e8750875395 to your computer and use it in GitHub Desktop.
Validation
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
class Model | |
attr_accessor :title | |
# has-a validator | |
def validator | |
@validator ||= ModelValidator.new(self) | |
end | |
end | |
# Represents a validation attempt | |
class Validation | |
attr_reader :errors | |
def initialize(errors) | |
@errors = errors | |
end | |
end | |
require "delegate" | |
class Validator < SimpleDelegator | |
include ActiveModel::Validations | |
# @return [Validation] | |
def validate | |
super | |
Validation.new(errors) | |
end | |
end | |
class ModelValidator < Validator | |
validates_presence_of :title | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment