Skip to content

Commit

Permalink
Prepare for v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Dec 19, 2014
1 parent 99e1aea commit 3686ef0
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 39 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Lotus::Validations
Validations mixin for Ruby objects

## v0.2.1 - 2014-12-23
### Added
- [Luca Guidi] Introduced `Validations::Errors#to_h` and `to_a`
- [Luca Guidi] Introduced `Validations::Errors#any?`

### Fixed
- [Satoshi Amemiya] Made `Validations#valid?` idempotent

## v0.2.0 - 2014-11-23
### Added
- [Luca Guidi] Skip attribute whitelisting when a validator does not define any attribute
Expand Down
8 changes: 4 additions & 4 deletions lib/lotus/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module ClassMethods
#
# @param base [Class] the target action
#
# @since x.x.x
# @since 0.1.0
# @api private
#
# @see http://www.ruby-doc.org/core/Module.html#method-i-included
Expand Down Expand Up @@ -384,7 +384,7 @@ def valid?
# @yieldparam attribute [Symbol] the name of the attribute
# @yieldparam value [Object,nil] the value of the attribute
#
# @since x.x.x
# @since 0.2.0
def each(&blk)
to_h.each(&blk)
end
Expand All @@ -394,15 +394,15 @@ def each(&blk)
#
# @return [Hash]
#
# @since x.x.x
# @since 0.1.0
def to_h
@attributes.dup
end

private
# The set of user defined attributes.
#
# @since x.x.x
# @since 0.1.0
# @api private
#
# @see Lotus::Validations::ClassMethods#attributes
Expand Down
48 changes: 22 additions & 26 deletions lib/lotus/validations/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class Attribute
#
# @see Lotus::Validations::Attribute#confirmation
#
# @since x.x.x
# @since 0.2.0
# @api private
CONFIRMATION_TEMPLATE = '%{name}_confirmation'.freeze

# @since x.x.x
# @since 0.2.0
# @api private
BLANK_STRING_MATCHER = /\A[[:space:]]*\z/.freeze

Expand All @@ -35,12 +35,8 @@ class Attribute
# @param name [Symbol] the name of the attribute
# @param options [Hash] the set of validations for the attribute
#
# @since x.x.x
# @since 0.2.0
# @api private
# def initialize(validator, name, options)
# @validator, @name, @options = validator, name, options
# @value = _attribute(@name)
# end
def initialize(attributes, name, value, validations)
@attributes = attributes
@name = name
Expand All @@ -50,7 +46,7 @@ def initialize(attributes, name, value, validations)
end

# @api private
# @since x.x.x
# @since 0.2.0
def validate
_with_cleared_errors do
presence
Expand All @@ -61,10 +57,10 @@ def validate
end

# @api private
# @since x.x.x
# @since 0.2.0
def value
if (coercer = @validations[:type])
return nil if blank_value?
return nil if blank_value?
Lotus::Validations::Coercions.coerce(coercer, @value)
else
@value
Expand All @@ -81,7 +77,7 @@ def value
# @see Lotus::Validations::ClassMethods#attribute
# @see Lotus::Validations::Attribute#nil_value?
#
# @since x.x.x
# @since 0.2.0
# @api private
def presence
_validate(__method__) { !blank_value? }
Expand All @@ -97,7 +93,7 @@ def presence
# @see Lotus::Validations::ClassMethods#attribute
# @see http://www.rubydoc.info/gems/lotus-utils/Lotus/Utils/Kernel#Boolean-class_method
#
# @since x.x.x
# @since 0.2.0
# @api private
def acceptance
_validate(__method__) { Lotus::Utils::Kernel.Boolean(@value) }
Expand All @@ -110,7 +106,7 @@ def acceptance
#
# @see Lotus::Validations::ClassMethods#attribute
#
# @since x.x.x
# @since 0.2.0
# @api private
def format
_validate(__method__) {|matcher| @value.to_s.match(matcher) }
Expand All @@ -122,7 +118,7 @@ def format
#
# @see Lotus::Validations::ClassMethods#attribute
#
# @since x.x.x
# @since 0.2.0
# @api private
def inclusion
_validate(__method__) {|collection| collection.include?(value) }
Expand All @@ -134,7 +130,7 @@ def inclusion
#
# @see Lotus::Validations::ClassMethods#attribute
#
# @since x.x.x
# @since 0.2.0
# @api private
def exclusion
_validate(__method__) {|collection| !collection.include?(value) }
Expand All @@ -148,7 +144,7 @@ def exclusion
# @see Lotus::Validations::ClassMethods#attribute
# @see Lotus::Validations::Attribute::CONFIRMATION_TEMPLATE
#
# @since x.x.x
# @since 0.2.0
# @api private
def confirmation
_validate(__method__) do
Expand Down Expand Up @@ -185,7 +181,7 @@ def confirmation
#
# @see Lotus::Validations::ClassMethods#attribute
#
# @since x.x.x
# @since 0.2.0
# @api private
def size
_validate(__method__) do |validator|
Expand Down Expand Up @@ -226,7 +222,7 @@ def size
# @see Lotus::Validations::ClassMethods#attribute
# @see Lotus::Validations::Coercions
#
# @since x.x.x
# @since 0.2.0
# @api private
def coerce
_validate(:type) do |coercer|
Expand All @@ -237,7 +233,7 @@ def coerce

# Checks if the value is `nil`.
#
# @since x.x.x
# @since 0.2.0
# @api private
def nil_value?
@value.nil?
Expand All @@ -249,27 +245,27 @@ def nil_value?
#
# @see Lotus::Validations::Attribute#presence
#
# @since x.x.x
# @since 0.2.0
# @api private
def blank_value?
nil_value? || _blank_string? || _empty_value?
end

# @since x.x.x
# @since 0.2.0
# @api private
def _blank_string?
(@value.respond_to?(:match) and @value.match(BLANK_STRING_MATCHER))
end

# @since x.x.x
# @since 0.2.0
# @api private
def _empty_value?
(@value.respond_to?(:empty?) and @value.empty?)
end

# Run the defined validations
#
# @since x.x.x
# @since 0.2.0
# @api private
def _run_validations
return if skip?
Expand All @@ -283,7 +279,7 @@ def _run_validations
end

# @api private
# @since x.x.x
# @since 0.2.0
def _with_cleared_errors
@errors.clear
yield
Expand All @@ -292,15 +288,15 @@ def _with_cleared_errors

# Reads an attribute from the validator.
#
# @since x.x.x
# @since 0.2.0
# @api private
def _attribute(name = @name)
@attributes[name.to_sym]
end

# Run a single validation and collects the results.
#
# @since x.x.x
# @since 0.2.0
# @api private
def _validate(validation)
if (validator = @validations[validation]) && !(yield validator)
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/validations/attribute_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def iterate(attributes, &blk)
# @raise [ArgumentError] if at least one of the validations are not
# recognized
#
# @since x.x.x
# @since 0.2.0
# @api private
def validate_options!(name, options)
if (unknown = (options.keys - VALIDATIONS)) && unknown.any?
Expand Down
6 changes: 3 additions & 3 deletions lib/lotus/validations/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def empty?
#
# @return [TrueClass,FalseClass] the result of the check
#
# @since x.x.x
# @since 0.2.0
#
# @see Lotus::Validations::Errors#empty?
def any?
Expand Down Expand Up @@ -125,7 +125,7 @@ def ==(other)
#
# @return [Lotus::Utils::Hash] the Hash
#
# @since x.x.x
# @since 0.2.1
def to_h
Utils::Hash.new(@errors).deep_dup
end
Expand All @@ -134,7 +134,7 @@ def to_h
#
# @return [Array]
#
# @since x.x.x
# @since 0.2.1
def to_a
errors.dup
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/validations/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Lotus
module Validations
# @since 0.1.0
VERSION = '0.2.1.dev'.freeze
VERSION = '0.2.1'.freeze
end
end
6 changes: 3 additions & 3 deletions lotus-validations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
spec.version = Lotus::Validations::VERSION
spec.authors = ['Luca Guidi']
spec.email = ['me@lucaguidi.com']
spec.summary = %q{Validations for Lotus}
spec.description = %q{Validate attributes on a class and manage error messages accordingly}
spec.summary = %q{Validations mixin for Ruby objects}
spec.description = %q{Validations mixin for Ruby objects and support for Lotus}
spec.homepage = 'http://lotusrb.org'
spec.license = 'MIT'

Expand All @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.0.0'

spec.add_dependency 'lotus-utils', '~> 0.3', '>= 0.3.1'
spec.add_dependency 'lotus-utils', '~> 0.3', '>= 0.3.2'

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'minitest', '~> 5'
Expand Down
2 changes: 1 addition & 1 deletion test/version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

describe Lotus::Validations::VERSION do
it 'exposes version' do
Lotus::Validations::VERSION.must_equal '0.2.1.dev'
Lotus::Validations::VERSION.must_equal '0.2.1'
end
end

0 comments on commit 3686ef0

Please sign in to comment.