Class: Porolog::Value
Overview
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.
Defined Under Namespace
Instance Attribute Summary collapse
-
#goal ⇒ Porolog::Goal
The goal in which the value was instantiated.
-
#instantiations ⇒ Array<Porolog::Instantiation>
Instantiations of this value.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares Values for equality.
-
#initialize(value, goal) ⇒ Porolog::Value
constructor
The Value.
-
#inspect ⇒ String
Pretty presentation.
-
#inspect_with_instantiations(visited = [], depth = 0, index = nil, self_index = nil) ⇒ String
Pretty presentation with instantiations and indexes.
-
#method_missing(method, *args, &block) ⇒ Object
Passes on methods to the Value’s value.
-
#remove ⇒ Boolean
Uninstantiate the Value.
-
#respond_to?(method, include_all = false) ⇒ Boolean
Responds to all the Value’s value methods as well as its own.
-
#type ⇒ Symbol
The type of the value.
-
#value ⇒ Object
The value of the Value.
-
#variables ⇒ Array<Porolog::Variable,Symbol>
Variables embedded in the value.
Constructor Details
#initialize(value, goal) ⇒ Porolog::Value
Returns the Value.
32 33 34 35 36 37 38 39 40 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Passes on methods to the Value’s value.
72 73 74 |
# File 'lib/porolog/value.rb', line 72 def method_missing(method, *args, &block) @value.send(method, *args, &block) end |
Instance Attribute Details
#goal ⇒ Porolog::Goal
Returns The goal in which the value was instantiated.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/porolog/value.rb', line 20 class Value # Error class for rescuing or detecting any Value error. class Error < PorologError ; end # Error class indicating that the supplied goal is not actually a goal. class GoalError < Error ; end attr_accessor :goal, :instantiations # @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. 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 # Pretty presentation. # @return [String] the inspect of the value prefixed by the goal id. def inspect "#{@goal.myid}.#{@value.inspect}" end # 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. 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 # Uninstantiate the Value. # @return [Boolean] true def remove @instantiations.dup.each(&:remove) @instantiations[0..-1] = [] true end # Passes on methods to the Value's value. def method_missing(method, *args, &block) @value.send(method, *args, &block) end # Responds to all the Value's value methods as well as its own. # @return [Boolean] whether the value responds to the method. def respond_to?(method, include_all = false) @value.respond_to?(method, include_all) || super end # @return [Object] the value of the Value. def value(*) @value end # @return [Symbol] the type of the value. def type @value.type end # Compares Values for equality. # @return [Boolean] whether the Values' values are equal. def ==(other) @value == other.value end # @return [Array<Porolog::Variable,Symbol>] variables embedded in the value. def variables @value.variables end end |
#instantiations ⇒ Array<Porolog::Instantiation>
Returns Instantiations of this value.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/porolog/value.rb', line 20 class Value # Error class for rescuing or detecting any Value error. class Error < PorologError ; end # Error class indicating that the supplied goal is not actually a goal. class GoalError < Error ; end attr_accessor :goal, :instantiations # @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. 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 # Pretty presentation. # @return [String] the inspect of the value prefixed by the goal id. def inspect "#{@goal.myid}.#{@value.inspect}" end # 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. 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 # Uninstantiate the Value. # @return [Boolean] true def remove @instantiations.dup.each(&:remove) @instantiations[0..-1] = [] true end # Passes on methods to the Value's value. def method_missing(method, *args, &block) @value.send(method, *args, &block) end # Responds to all the Value's value methods as well as its own. # @return [Boolean] whether the value responds to the method. def respond_to?(method, include_all = false) @value.respond_to?(method, include_all) || super end # @return [Object] the value of the Value. def value(*) @value end # @return [Symbol] the type of the value. def type @value.type end # Compares Values for equality. # @return [Boolean] whether the Values' values are equal. def ==(other) @value == other.value end # @return [Array<Porolog::Variable,Symbol>] variables embedded in the value. def variables @value.variables end end |
Instance Method Details
#==(other) ⇒ Boolean
Compares Values for equality.
94 95 96 |
# File 'lib/porolog/value.rb', line 94 def ==(other) @value == other.value end |
#inspect ⇒ String
Pretty presentation.
44 45 46 |
# 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) ⇒ String
Pretty presentation with instantiations and indexes. This method is for polymorphic compatibility with Porolog::Variable. It used by Porolog::Goal#inspect_variables.
56 57 58 59 60 61 |
# 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 |
#remove ⇒ Boolean
Uninstantiate the Value.
65 66 67 68 69 |
# File 'lib/porolog/value.rb', line 65 def remove @instantiations.dup.each(&:remove) @instantiations[0..-1] = [] true end |
#respond_to?(method, include_all = false) ⇒ Boolean
Responds to all the Value’s value methods as well as its own.
78 79 80 |
# File 'lib/porolog/value.rb', line 78 def respond_to?(method, include_all = false) @value.respond_to?(method, include_all) || super end |
#type ⇒ Symbol
Returns the type of the value.
88 89 90 |
# File 'lib/porolog/value.rb', line 88 def type @value.type end |
#value ⇒ Object
Returns the value of the Value.
83 84 85 |
# File 'lib/porolog/value.rb', line 83 def value(*) @value end |
#variables ⇒ Array<Porolog::Variable,Symbol>
Returns variables embedded in the value.
99 100 101 |
# File 'lib/porolog/value.rb', line 99 def variables @value.variables end |