Class: Namespace

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/graph/namespace.rb

Overview

Instances of Namespace are immutable.

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ Namespace

Returns a new instance of Namespace.



10
11
12
# File 'lib/graph/namespace.rb', line 10

def initialize array
  @array = array || Array.new # do not want a nil array for namespace, better to have an empty array
end

Instance Method Details

#does_include?(other) ⇒ Boolean

Checks if the Namespace include the other Namespace. ex. A::B::C includes B::C since B::C could belong to A other is a Namespace object

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/graph/namespace.rb', line 26

def does_include? other
  return false if other.count > self.count
  return true if self.eql? other
  return self.drop(self.count - other.count).to_a.eql? other.to_a
end

#each(&block) ⇒ Object

postcondition: order will be preserved



42
43
44
# File 'lib/graph/namespace.rb', line 42

def each &block
  @array.each &block
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


32
33
34
# File 'lib/graph/namespace.rb', line 32

def eql? other
  @array.eql?(other.to_a) && self.class.eql?(other.class)
end

#hashObject



37
38
39
# File 'lib/graph/namespace.rb', line 37

def hash
  @array.hash
end

#is_included_by?(other) ⇒ Boolean

Checks if the Namespace could be included by the other Namespace. ex. B::C is included by A::B::C since B::C could belong to A other is a Namespace object

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/graph/namespace.rb', line 17

def is_included_by? other
  return false if self.count > other.count
  return true if self.eql? other
  return other.drop(other.count - self.count).to_a.eql? self.to_a
end

#to_sObject



46
47
48
# File 'lib/graph/namespace.rb', line 46

def to_s
  @array.join '::'
end