Class: Porolog::Scope

Inherits:
Object show all
Defined in:
lib/porolog/scope.rb

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.

Author:

  • Luis Esteban

Defined Under Namespace

Classes: Error, NotPredicateError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Scope

Initializes and registers the Scope.

Parameters:

  • name (Object)

    the name (or otherwise object) used to register a 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

#nameObject (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.

Parameters:

  • name (Object)

    the name (or otherwise object) used to register a scope.

Returns:



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.

Parameters:

  • name (Symbol, Object)

    the name (or otherwise object) used to register a scope.

Returns:



28
29
30
# File 'lib/porolog/scope.rb', line 28

def self.new(name)
  @@scopes[name] || super
end

.resetPorolog::Scope

Clears all scopes and re-creates the default Scope.

Returns:



43
44
45
46
# File 'lib/porolog/scope.rb', line 43

def self.reset
  @@scopes = {}
  new(:default)
end

.scopesArray<Symbol>, Array<Object>

Returns the names of all registered Scopes.

Returns:

  • (Array<Symbol>)

    the names if scopes are named as Symbols (the intended case).

  • (Array<Object>)

    the names if the names are not actually Symbols.



60
61
62
# File 'lib/porolog/scope.rb', line 60

def self.scopes
  @@scopes.keys
end

Instance Method Details

#[](name) ⇒ Porolog::Predicate

Looks up a Predicate in the Scope by its name.

Parameters:

  • name (Object)

    the name (or otherwise object) used to register a scope.

Returns:



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.

Parameters:

  • name (Object)

    the name (or otherwise object) used to register a scope.

  • predicate (Porolog::Predicate)

    a Predicate to be assigned to the Scope.

Returns:

Raises:



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

#predicatesArray<Porolog::Predicate>

Returns the Predicates contained in the Scope.

Returns:



83
84
85
# File 'lib/porolog/scope.rb', line 83

def predicates
  @predicates.values
end