Sha256: f2545de1a33a6cf7cfceeb1821a53b0b91416614b6bcb12ab566da3a03425ec8

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module MiniKraken
  module Core
    # A record that a given variable is associated with a value.
    class Association
      # @return [String] internal name of variable being associated the value.
      attr_accessor :i_name

      # @return [Term] the MiniKraken value associated with the variable
      attr_reader :value

      # @param aVariable [Variable, String] A variable or its internal name.
      # @param aValue [Term] value being associated to the variable.
      def initialize(aVariable, aValue)
        a_name = aVariable.respond_to?(:i_name) ? aVariable.i_name : aVariable
        @i_name = validated_name(a_name)
        @value = aValue
        @dependencies = nil
      end

      # Is the associated value floating, that is, it does contain
      # a variable that is either unbound or floating?
      # @param ctx [Core::Context]
      # @return [Boolean]
      def floating?(ctx)
        value.floating?(ctx)
      end

      # Is the associated value pinned, that is, doesn't contain
      # an unbound or floating variable?
      # @param ctx [Core::Context]
      # @return [Boolean]
      def pinned?(ctx)
        @pinned ||= value.pinned?(ctx)
        @pinned
      end

      # @return [Array<String>] The i_names of direct dependent variables
      def dependencies(ctx)
        @dependencies ||= value.dependencies(ctx)
        raise StandardError unless @dependencies.kind_of?(Set) || @dependencies.kind_of?(NilClass)

        @dependencies
      end

      private

      def validated_name(aName)
        raise StandardError, 'Name cannot be nil' if aName.nil?

        cleaned = aName.strip
        raise StandardError, 'Name cannot be empty or consists of spaces' if cleaned.empty?

        cleaned
      end
    end # class
  end # module
end # module

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mini_kraken-0.3.03 lib/mini_kraken/core/association.rb
mini_kraken-0.3.02 lib/mini_kraken/core/association.rb
mini_kraken-0.3.01 lib/mini_kraken/core/association.rb
mini_kraken-0.3.00 lib/mini_kraken/core/association.rb