Class: Porolog::Scope
Overview
A Porolog::Scope is a container for Porolog::Predicates. Its purpose is to allow a Ruby program to contain multiple Prolog programs without the Prolog programs interfering with each other.
Defined Under Namespace
Classes: Error, NotPredicateError
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Name of the Scope.
Class Method Summary collapse
-
.[](name) ⇒ Porolog::Scope
Looks up a Scope by its name.
-
.new(name) ⇒ Porolog::Scope
Creates a new Scope unless it already exists.
-
.reset ⇒ Porolog::Scope
Clears all scopes and re-creates the default Scope.
-
.scopes ⇒ Array<Symbol>, Array<Object>
Returns the names of all registered Scopes.
Instance Method Summary collapse
-
#[](name) ⇒ Porolog::Predicate
Looks up a Predicate in the Scope by its name.
-
#[]=(name, predicate) ⇒ Porolog::Predicate
Assigns a Predicate to the Scope by its name.
-
#initialize(name) ⇒ Scope
constructor
Initializes and registers the Scope.
-
#predicates ⇒ Array<Porolog::Predicate>
Returns the Predicates contained in the Scope.
Constructor Details
#initialize(name) ⇒ Scope
Initializes and registers the Scope.
34 35 36 37 38 39 |
# File 'lib/porolog/scope.rb', line 34 def initialize(name) @name = name @predicates = {} @@scopes[@name] = self end |
Instance Attribute Details
#name ⇒ Object (readonly)
Name of the Scope.
16 17 18 19 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 |
# File 'lib/porolog/scope.rb', line 16 class Scope # Error class for rescuing or detecting any Scope error. class Error < PorologError ; end # Error class indicating a non-Predicate was assigned to a Scope. class NotPredicateError < Error ; end attr_reader :name # Creates a new Scope unless it already exists. # @param name [Symbol, Object] the name (or otherwise object) used to register a scope. # @return [Porolog::Scope] new or existing Scope. def self.new(name) @@scopes[name] || super end # Initializes and registers the Scope. # @param name [Object] the name (or otherwise object) used to register a scope. def initialize(name) @name = name @predicates = {} @@scopes[@name] = self end # Clears all scopes and re-creates the default Scope. # @return [Porolog::Scope] the default Scope. def self.reset @@scopes = {} new(:default) end reset # Looks up a Scope by its name. # @param name [Object] the name (or otherwise object) used to register a scope. # @return [Porolog::Scope] the default Scope. def self.[](name) @@scopes[name] end # Returns the names of all registered Scopes. # @return [Array<Symbol>] the names if scopes are named as Symbols (the intended case). # @return [Array<Object>] the names if the names are not actually Symbols. def self.scopes @@scopes.keys end # Looks up a Predicate in the Scope by its name. # @param name [Object] the name (or otherwise object) used to register a scope. # @return [Porolog::Predicate] the Predicate indicated by the name. def [](name) @predicates[name.to_sym] end # Assigns a Predicate to the Scope by its name. # @param name [Object] the name (or otherwise object) used to register a scope. # @param predicate [Porolog::Predicate] a Predicate to be assigned to the Scope. # @return [Porolog::Predicate] the Predicate assigned to the Scope. # @raise [NotPredicateError] when provided predicate is not actually a Predicate. def []=(name, predicate) raise NotPredicateError, "#{predicate.inspect} is not a Predicate" unless predicate.is_a?(Predicate) @predicates[name.to_sym] = predicate end # Returns the Predicates contained in the Scope. # @return [Array<Porolog::Predicate>] Predicates assigned to the Scope. def predicates @predicates.values end end |
Class Method Details
.[](name) ⇒ Porolog::Scope
Looks up a Scope by its name.
53 54 55 |
# File 'lib/porolog/scope.rb', line 53 def self.[](name) @@scopes[name] end |
.new(name) ⇒ Porolog::Scope
Creates a new Scope unless it already exists.
28 29 30 |
# File 'lib/porolog/scope.rb', line 28 def self.new(name) @@scopes[name] || super end |
.reset ⇒ Porolog::Scope
Clears all scopes and re-creates the default Scope.
43 44 45 46 |
# File 'lib/porolog/scope.rb', line 43 def self.reset @@scopes = {} new(:default) end |
Instance Method Details
#[](name) ⇒ Porolog::Predicate
Looks up a Predicate in the Scope by its name.
67 68 69 |
# File 'lib/porolog/scope.rb', line 67 def [](name) @predicates[name.to_sym] end |
#[]=(name, predicate) ⇒ Porolog::Predicate
Assigns a Predicate to the Scope by its name.
76 77 78 79 |
# File 'lib/porolog/scope.rb', line 76 def []=(name, predicate) raise NotPredicateError, "#{predicate.inspect} is not a Predicate" unless predicate.is_a?(Predicate) @predicates[name.to_sym] = predicate end |
#predicates ⇒ Array<Porolog::Predicate>
Returns the Predicates contained in the Scope.
83 84 85 |
# File 'lib/porolog/scope.rb', line 83 def predicates @predicates.values end |