Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from mcorp/validations-0.6.0
Browse files Browse the repository at this point in the history
Update to supports new version of hanami-validations
  • Loading branch information
vyper authored Jul 29, 2016
2 parents a8c6218 + 9986a7c commit 93d9cd8
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 244 deletions.
12 changes: 2 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
language: ruby
rvm:
- 2.0.0
- 2.1.0
- 2.1.1
- 2.1.2
- 2.1.3
- 2.1.4
- 2.1.5
- 2.1.6
- 2.1.7
- 2.1.8
- 2.2.0
- 2.2.1
- 2.2.2
- 2.2.3
- 2.2.4
- 2.2.5
- 2.3.0
- 2.3.1
- rbx-2
- jruby-9000
- jruby-9.0.1.0
Expand Down
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ And then execute:
Or install it yourself as:

$ gem install shoulda-hanami

## Configure

Create file `spec/support/shoulda_hanami.rb` with:
Expand All @@ -38,17 +38,18 @@ end
class Person
include Hanami::Validations

attribute :email, type: String, presence: true, format: /@/
attribute :name, type: String, size: 5..50
attribute :password, type: String, size: 10
attribute :birthday, type: Date
attribute :created_at, type: DateTime
attribute :state, type: String, inclusion: %w(PR SC SP)
attribute :year, type: Integer, inclusion: 1979..1990
validations do
required(:email) { format?(/@/) }
required(:name) { size?(5..50) }
required(:password) { size?(10) }
required(:state) { included_in?(%w(PR SC SP)) }
required(:year) { included_in?(1979..1990) }
end
end
```

### Spec

```ruby
# allow_value
it { is_expected.to allow_value("[email protected]").for(:email) }
Expand All @@ -61,13 +62,6 @@ it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_length_of(:name).is_at_least(5).is_at_most(50) }
it { is_expected.to validate_length_of(:password).is_equal_to(10) }

# coerces
it { is_expected.to coerce_attribute(:email).to(String) }
it { is_expected.to coerce_attribute(:name).to(String) }
it { is_expected.to coerce_attribute(:password).to(String) }
it { is_expected.to coerce_attribute(:birthday).to(Date) }
it { is_expected.to coerce_attribute(:created_at).to(DateTime) }

# inclusion
it { is_expected.to validate_inclusion_of(:state).in_array(%w(PR SC SP)) }
it { is_expected.to validate_inclusion_of(:year).in_array(1979..1990) }
Expand Down
17 changes: 12 additions & 5 deletions lib/shoulda/hanami/matchers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'shoulda/hanami/matchers/coerce_attribute_matcher'
require 'shoulda/hanami/matchers/allow_value_matcher'
require 'shoulda/hanami/matchers/validate_inclusion_of_matcher'
require 'shoulda/hanami/matchers/validate_length_of_matcher'
Expand All @@ -8,15 +7,23 @@ module Shoulda
module Hanami
module Matchers
class Matcher
ERRORS = {
format: 'is in invalid format',
inclusion: 'must be one of:',
size: 'length must be',
presence: 'must be filled'
}

def initialize(target, attribute, validation)
@target = target
@attribute = attribute
@target = target
@attribute = attribute
@validation = validation
end

def matches?
@target.valid?
@target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == @validation }.size > 0
result = @target.validate
result.failure? &&
result.messages.fetch(@attribute).select { |msg| msg.include?(ERRORS[@validation]) }.size > 0
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/shoulda/hanami/matchers/allow_value_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def initialize(value)
end

def matches?(target)
target.send("#{@attribute}=", @value)
!Matcher.new(target, @attribute, :format).matches?
!Matcher.new(target.class.new(@attribute => @value), @attribute, :format).matches?
end

def description
Expand Down
53 changes: 0 additions & 53 deletions lib/shoulda/hanami/matchers/coerce_attribute_matcher.rb

This file was deleted.

3 changes: 1 addition & 2 deletions lib/shoulda/hanami/matchers/validate_inclusion_of_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def matches?(target)
break unless @values.include? value
end

target.send("#{@attribute}=", value)
Matcher.new(target, @attribute, :inclusion).matches?
Matcher.new(target.class.new(@attribute => value), @attribute, :inclusion).matches?
end

def description
Expand Down
6 changes: 2 additions & 4 deletions lib/shoulda/hanami/matchers/validate_length_of_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ def initialize(attribute)
def matches?(target)
errors = []

target.send("#{@attribute}=", '*' * (minimum - 1))
errors << Matcher.new(target, @attribute, :size).matches?
errors << Matcher.new(target.class.new(@attribute => '*' * (minimum - 1)), @attribute, :size).matches?

target.send("#{@attribute}=", '*' * (maximum + 1))
errors << Matcher.new(target, @attribute, :size).matches?
errors << Matcher.new(target.class.new(@attribute => '*' * (maximum + 1)), @attribute, :size).matches?

errors.all? { |error| error }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(attribute)
end

def matches?(target)
Matcher.new(target, @attribute, :presence).matches?
Matcher.new(target.class.new(@attribute => nil), @attribute, :presence).matches?
end

def description
Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/hanami/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Shoulda
module Hanami
VERSION = '0.1.0'.freeze
VERSION = '0.2.0'.freeze
end
end
2 changes: 1 addition & 1 deletion shoulda-hanami.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.4.0'

spec.add_dependency 'hanami-validations', '~> 0.5.0'
spec.add_dependency 'hanami-validations', '~> 0.6.0'
end
4 changes: 3 additions & 1 deletion spec/fixtures/with_validation_format_model.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class WithValidationFormatModel
include Hanami::Validations

attribute :email, format: /@/
validations do
optional(:email) { format?(/@/) }
end
end
4 changes: 3 additions & 1 deletion spec/fixtures/with_validation_inclusion_model.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class WithValidationInclusionModel
include Hanami::Validations

attribute :state, inclusion: %w(PR SC SP)
validations do
optional(:state).filled(inclusion?: %w(PR SC SP))
end
end
6 changes: 4 additions & 2 deletions spec/fixtures/with_validation_length_model.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class WithValidationLengthModel
include Hanami::Validations

attribute :name_equal_10, size: 10
attribute :name_between_2_10, size: 2..10
validations do
optional(:name_equal_10).filled(size?: 10)
optional(:name_between_2_10).filled(size?: 2..10)
end
end
4 changes: 3 additions & 1 deletion spec/fixtures/with_validation_presence_model.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class WithValidationPresenceModel
include Hanami::Validations

attribute :email, presence: true
validations do
required(:email).filled
end
end
28 changes: 15 additions & 13 deletions spec/fixtures/with_validation_type_model.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
class WithValidationTypeModel
include Hanami::Validations

attribute :attr_array, type: Array
attribute :attr_bigdecimal, type: BigDecimal
attribute :attr_boolean, type: Boolean
attribute :attr_date, type: Date
attribute :attr_datetime, type: DateTime
attribute :attr_float, type: Float
attribute :attr_hash, type: Hash
attribute :attr_integer, type: Integer
attribute :attr_pathname, type: Pathname
attribute :attr_set, type: Set
attribute :attr_string, type: String
attribute :attr_symbol, type: Symbol
attribute :attr_time, type: Time
validations do
optional(:attr_array) { array? }
optional(:attr_bigdecimal) { decimal? }
optional(:attr_boolean) { bool? }
optional(:attr_date) { date? }
optional(:attr_datetime).filled(type?: DateTime)
optional(:attr_float) { float? }
optional(:attr_hash) { hash? }
optional(:attr_integer) { int? }
optional(:attr_pathname).filled(type?: Pathname)
optional(:attr_set).filled(type?: Set)
optional(:attr_string) { str? }
optional(:attr_symbol).filled(type?: Symbol)
optional(:attr_time) { time? }
end
end
4 changes: 3 additions & 1 deletion spec/fixtures/without_validation_model.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class WithoutValidationModel
include Hanami::Validations

attribute :email
validations do
optional(:email)
end
end
File renamed without changes.
Loading

0 comments on commit 93d9cd8

Please sign in to comment.