Sha256: 676b83e2a8963e4476c0b1a38a64635690474d6b877c0e5ddf792691fe03e650

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 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 allows?(limitation)
      @constraints.all? do |constraint|
        limitation.accomodates?(constraint)
      end
    end

    def prohibits?(limitation)
      !allows?(limitation)
    end

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
key-vortex-0.1.4 lib/key_vortex/limitation.rb