class FlexValidations::Predicate

Call predicate on object

@example

odd = FlexValidations::Predicate.new(:odd?)
odd.validate(1).success? #=> true

@example Description

> puts odd
value.odd? should succeed

@example Description of success result

> puts odd.validate(1)
1.odd? succeed

@example Description of fail result

> puts odd.validate(2)
2.odd? failed

@example Description when value doesn't respond to method

> puts odd.validate("foo")
"foo" of String isn't respond to method odd?

Public Class Methods

new(method, *args) click to toggle source
# File lib/flex_validations/predicate.rb, line 28
def initialize(method, *args)
  @method = method
  @args = args
end

Public Instance Methods

to_s() click to toggle source

@return [String]

# File lib/flex_validations/predicate.rb, line 47
def to_s
  args = "(#{@args.map(&:inspect).join(', ')})" if @args.length > 0

  "value.#{@method}#{args} should succeed"
end
validate(value) click to toggle source

@param value [Object] Value to be validated

@return [FlexValidations::Result]

# File lib/flex_validations/predicate.rb, line 36
def validate(value)
  return not_respond(value) unless value.respond_to?(@method, false)

  ret = value.public_send(@method, *@args)

  return failed(value, ret) unless ret

  success(value, ret)
end

Private Instance Methods

failed(value, ret) click to toggle source
# File lib/flex_validations/predicate.rb, line 113
def failed(value, ret)
  msg = FailedMessage.new(value, @method, @args)

  Result::Fail::Simple.new(self, msg, value, ret)
end
not_respond(value) click to toggle source
# File lib/flex_validations/predicate.rb, line 107
def not_respond(value)
  msg = NotRespondMessage.new(value, @method)

  Result::Fail::Simple.new(self, msg, value, false)
end
success(value, ret) click to toggle source
# File lib/flex_validations/predicate.rb, line 101
def success(value, ret)
  msg = SuccessMessage.new(value, @method, @args, ret)

  Result::Success::Simple.new(self, msg, value, ret)
end