-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from davidrunger/active-model-validations
Add ability to specify ActiveModel-style validations for Class shapes
- Loading branch information
Showing
11 changed files
with
283 additions
and
41 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
class Shaped::Shapes::Class < Shaped::Shape | ||
def initialize(shape_description) | ||
if !shape_description.is_a?(Class) | ||
def initialize(expected_klass, validations = {}) | ||
if !expected_klass.is_a?(Class) | ||
raise(Shaped::InvalidShapeDescription, "A #{self.class} description must be a Class.") | ||
end | ||
|
||
@expected_klass = shape_description | ||
@expected_klass = expected_klass | ||
@validations = validations | ||
@validator_klass = validator_klass(validations) | ||
end | ||
|
||
def matched_by?(object) | ||
object.is_a?(@expected_klass) | ||
object.is_a?(@expected_klass) && validations_satisfied?(object) | ||
end | ||
|
||
def to_s | ||
@expected_klass.name | ||
if @validations.empty? | ||
@expected_klass.name | ||
else | ||
"#{@expected_klass} validating #{@validations}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def validator_klass(validations) | ||
return nil if validations.empty? | ||
|
||
Class.new do | ||
include ActiveModel::Validations | ||
|
||
attr_accessor :value | ||
|
||
validates :value, validations | ||
|
||
class << self | ||
# ActiveModel requires the class to have a `name` | ||
def name | ||
'Shaped::Shapes::Class::AnonymousValidator' | ||
end | ||
end | ||
end | ||
end | ||
|
||
def validations_satisfied?(object) | ||
return true if @validator_klass.nil? | ||
|
||
validator_instance = @validator_klass.new | ||
validator_instance.value = object | ||
validator_instance.valid? | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Shaped | ||
VERSION = '0.3.2' | ||
VERSION = '0.4.0.alpha' | ||
end |
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
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
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
Oops, something went wrong.