Sha256: 9d676278777ae61dfe4086f4b81fe84fca407b459bb8446749af6846736e71be

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module FlexValidations
  # Not a really validation but handy class for complex validations
  #
  # @example
  #   hash = FlexValidations::Chain.new(
  #     FlexValidations::Call.new(:keys),
  #     FlexValidations::Decorate.new(Set),
  #     FlexValidations::Predicate.new(:==, Set.new(["id", "title"]))
  #   )
  class Decorate
    include Validation

    def initialize(decorator_class, *decorator_args)
      @decorator_class = decorator_class
      @decorator_args = decorator_args
    end

    # @param value [Object] Value to be validated
    #
    # @return [FlexValidations::Result]
    def validate(value)
      new_val = @decorator_class.new value, *@decorator_args

      Result::Success::Simple.new(self, SuccessMessage.new(value, new_val), value, new_val)
    end

    # @return [String]
    def to_s
      "decorate value with #{@decorator_class}"
    end

    class SuccessMessage
      def initialize(value, ret)
        @value = value
        @ret = ret
      end

      def to_s
        "decorated #{@value.inspect} now #{@ret.inspect}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flex_validations-0.1.0 lib/flex_validations/decorate.rb