Sha256: e80c43c7940f56f2297cd7270cec80c51b5f169ff3bcb0daa6ae458d33a0ac2a

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8

module Axiom
  module Types

    # Add a minimum and maximum value constraint to a type
    module ValueComparable

      # Hook called when module is extended
      #
      # Add #minimum and #maximum DSL methods to descendant.
      #
      # @param [Class<Axiom::Types::Type>] descendant
      #
      # @return [undefined]
      #
      # @api private
      def self.extended(descendant)
        super
        descendant.class_eval do
          accept_options :minimum, :maximum
        end
      end

      # Finalize by setting up a value range constraint
      #
      # @return [Axiom::Types::ValueComparable]
      #
      # @api private
      def finalize
        return self if frozen?
        has_value_within_range
        super
      end

    private

      # Add a constraint for a value within a range
      #
      # @return [undefined]
      #
      # @todo freeze the minimum and maximum
      #
      # @api private
      def has_value_within_range
        range = minimum..maximum
        constraint(range.method(:cover?))
      end

    end # module ValueComparable
  end # module Types
end # module Axiom

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
axiom-types-0.0.5 lib/axiom/types/value_comparable.rb
axiom-types-0.0.4 lib/axiom/types/value_comparable.rb
axiom-types-0.0.3 lib/axiom/types/value_comparable.rb