Sha256: 386c61da338f87738c2b98bb129146cf94c729ea9f78a8fa5611cca2057223fb

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

class KeyVortex
  class Limitation
    attr_reader :type, :constraints

    def initialize(type, *constraints)
      @type = type
      @constraints = constraints
    end

    def add_constraint(*constraints)
      constraints.each do |constraint|
        unless constraint.is_a?(KeyVortex::Constraint::Base)
          raise KeyVortex::Error,
                "Not a constraint: #{constraint.class}"
        end
      end

      @constraints += constraints
    end

    def encompasses?(limitation)
      @constraints.all? do |constraint|
        limitation.encompasses_constraint?(constraint)
      end
    end

    def encompasses_constraint?(constraint)
      !applicable_constraints(constraint).select do |con|
        con.within?(constraint)
      end.empty?
    end

    def within?(limitation)
      limitation.constraints.all? do |constraint|
        within_constraint?(constraint)
      end
    end

    def within_constraint?(constraint)
      !applicable_constraints(constraint).select do |con|
        con.within?(constraint)
      end.empty?
    end

    def applicable_constraints(constraint)
      @constraints.select do |con|
        con.applies_to?(constraint)
      end
    end

    def accepts?(value)
      value.is_a?(type) && @constraints.all? { |constraint| constraint.accepts?(value) }
    end

    def to_s
      "Limitation: #{@type}\n\t#{@constraints.join('\n\t')}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
key-vortex-0.2.1 lib/key_vortex/limitation.rb
key-vortex-0.2.0 lib/key_vortex/limitation.rb