Sha256: cd72c12b77ea2eca3078b3dc882245b30040adc6c83915f5f9e1558fb6801169

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# -*- encoding: utf-8 -*-

require 'aequitas/rule/within'

module Aequitas
  class Rule
    module Within
      module Range

        Infinity = 1.0 / 0

        include Within

        attr_reader :range

        def self.rules_for(attribute_name, options)
          Array(new(attribute_name, options))
        end

        def self.new(attribute_name, options)
          super if Within::Range != self

          range = options.fetch(:range) { options.fetch(:set) }

          if range.first != -Infinity && range.last != Infinity
            Bounded.new(attribute_name, options)
          elsif range.first == -Infinity
            UnboundedBegin.new(attribute_name, options)
          elsif range.last == Infinity
            UnboundedEnd.new(attribute_name, options)
          end
        end

        def initialize(attribute_name, options={})
          super

          @range = options.fetch(:range) { options.fetch(:set) }
        end

        def valid?(resource)
          value = attribute_value(resource)

          skip?(value) || range.include?(value)
        end

      end # module Range
    end # module Within
  end # class Rule
end # module Aequitas

require 'aequitas/rule/within/range/bounded'
require 'aequitas/rule/within/range/unbounded_begin'
require 'aequitas/rule/within/range/unbounded_end'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aequitas-0.0.1 lib/aequitas/rule/within/range.rb