Sha256: f4e45c6260208e2f8873eaf5bb15d5f188d890a460908fbd4b66f7a26ef3bcf9

Contents?: true

Size: 511 Bytes

Versions: 3

Compression:

Stored size: 511 Bytes

Contents

module RiceBubble
  class Attributes
    class Optional < Base
      attr_reader :child

      def initialize(child, &)
        super(&)
        @child = child
      end

      def valid?(value)
        value.nil? || child.valid?(child.coerce(value))
      end

      def call(value, path: '')
        value && child.call(value, path:)
      end

      def coerce(value)
        value && child.coerce(value)
      end

      def description
        "#{child.description} (optional)"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rice_bubble-0.1.2 lib/rice_bubble/attributes/optional.rb
rice_bubble-0.1.1 lib/rice_bubble/attributes/optional.rb
rice_bubble-0.1.0 lib/rice_bubble/attributes/optional.rb