Sha256: 1d5af2cb0f8f5ccb77833365e98146a2008069e4f14e815cb530402365117401

Contents?: true

Size: 1.23 KB

Versions: 11

Compression:

Stored size: 1.23 KB

Contents

# encoding: utf-8

module Axiom
  module Types

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

      # The range of allowed values
      #
      # @return [Range]
      #
      # @api public
      attr_reader :range

      # 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.accept_options :minimum, :maximum
      end

      # Finalize by setting up a value range constraint
      #
      # @return [Axiom::Types::ValueComparable]
      #
      # @api private
      def finalize
        return self if frozen?
        @range = IceNine.deep_freeze(minimum..maximum)
        use_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 use_value_within_range
        constraint(range.method(:cover?))
      end

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

Version data entries

11 entries across 9 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/axiom-types-0.1.1/lib/axiom/types/value_comparable.rb
axiom-types-0.1.1 lib/axiom/types/value_comparable.rb
axiom-types-0.1.0 lib/axiom/types/value_comparable.rb