class Porolog::Value

A Porolog::Value combines a value with a goal so that when the goal is closed, the value can be uninstantiated at the same time.

@author Luis Esteban

@!attribute goal

@return [Porolog::Goal] The goal in which the value was instantiated.

@!attribute instantiations

@return [Array<Porolog::Instantiation>] Instantiations of this value.

Attributes

goal[RW]
instantiations[RW]

Public Class Methods

new(value, goal) click to toggle source

@param value [Object] the value to be associated with a Goal. @param goal [Porolog::Goal] the Goal to be associated. @return [Porolog::Value] the Value.

# File lib/porolog/value.rb, line 32
def initialize(value, goal)
  raise GoalError, "Not a Goal: #{goal.inspect}" unless goal.is_a?(Goal)
  
  @value = value
  @value = value.value if value.is_a?(Value)
  @goal  = goal
  
  @instantiations = []
end

Public Instance Methods

==(other) click to toggle source

Compares Values for equality. @return [Boolean] whether the Values' values are equal.

# File lib/porolog/value.rb, line 94
def ==(other)
  @value == other.value
end
inspect() click to toggle source

Pretty presentation. @return [String] the inspect of the value prefixed by the goal id.

# File lib/porolog/value.rb, line 44
def inspect
  "#{@goal.myid}.#{@value.inspect}"
end
inspect_with_instantiations(visited = [], depth = 0, index = nil, self_index = nil) click to toggle source

Pretty presentation with instantiations and indexes. This method is for polymorphic compatibility with Porolog::Variable. It used by Porolog::Goal#inspect_variables. @param visited [Array] the values already visited (to prevent infinite recursion). @param depth [Integer] the level of indentation that shows containment. @param index [Integer,Symbol,Array] the index into this value. @param self_index [Integer,Symbol,Array] the index of which this value belongs. @return [String] the inspect of the value in the context of the variables of a goal.

# File lib/porolog/value.rb, line 56
def inspect_with_instantiations(visited = [], depth = 0, index = nil, self_index = nil)
  index_str = index && "[#{index.inspect}]" || ''
  prefix    = self_index&.inspect || ''
  
  "#{'  ' * depth}#{prefix}#{inspect}#{index_str}"
end
method_missing(method, *args, &block) click to toggle source

Passes on methods to the Value's value.

# File lib/porolog/value.rb, line 72
def method_missing(method, *args, &block)
  @value.send(method, *args, &block)
end
remove() click to toggle source

Uninstantiate the Value. @return [Boolean] true

# File lib/porolog/value.rb, line 65
def remove
  @instantiations.dup.each(&:remove)
  @instantiations[0..-1] = []
  true
end
respond_to?(method, include_all = false) click to toggle source

Responds to all the Value's value methods as well as its own. @return [Boolean] whether the value responds to the method.

Calls superclass method
# File lib/porolog/value.rb, line 78
def respond_to?(method, include_all = false)
  @value.respond_to?(method, include_all) || super
end
type() click to toggle source

@return [Symbol] the type of the value.

# File lib/porolog/value.rb, line 88
def type
  @value.type
end
value(*) click to toggle source

@return [Object] the value of the Value.

# File lib/porolog/value.rb, line 83
def value(*)
  @value
end
variables() click to toggle source

@return [Array<Porolog::Variable,Symbol>] variables embedded in the value.

# File lib/porolog/value.rb, line 99
def variables
  @value.variables
end