Class: LinkParser::Connection

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/linkparser/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn1, conn2, lword, rword) ⇒ Connection

Returns a new instance of Connection.

Raises:



28
29
30
31
32
33
34
35
36
# File 'lib/linkparser/connection.rb', line 28

def initialize( conn1, conn2, lword, rword )
	raise( ParseError::new( "A connection cannot be formed between two words that do not match: %s and %s" %
						   [ conn1, conn2 ]) ) unless
		conn1.match(conn2)
	@name, @subName = conn1.generalize(conn2)
	@lword = lword
	@rword = rword
	@length = rword.position - lword.position
end

Instance Attribute Details

#lengthObject (readonly)

the number of words spanned by this connection



51
52
53
# File 'lib/linkparser/connection.rb', line 51

def length
  @length
end

#lwordObject (readonly)

the index of the word connected to this link on the left



45
46
47
# File 'lib/linkparser/connection.rb', line 45

def lword
  @lword
end

#nameObject (readonly)

the major name of the link



39
40
41
# File 'lib/linkparser/connection.rb', line 39

def name
  @name
end

#rwordObject (readonly)

the index of the word connected to this link on the right



48
49
50
# File 'lib/linkparser/connection.rb', line 48

def rword
  @rword
end

#subNameObject (readonly)

the minor name of the link



42
43
44
# File 'lib/linkparser/connection.rb', line 42

def subName
  @subName
end

Instance Method Details

#<=>(other) ⇒ Object

Provide a consistent basis for sorting connections within a linkage to allow two linkages to be compared.



60
61
62
63
64
65
66
# File 'lib/linkparser/connection.rb', line 60

def <=>(other)
	pos = self.lword.position <=> other.lword.position
	return pos if pos.nonzero?
	len = self.length <=> other.length
	return len if len.nonzero?
	return self.to_s <=> other.to_s
end

#eql?(o) ⇒ Boolean

Equality based only on the name and subname.

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/linkparser/connection.rb', line 69

def eql?(o)
	@name == o.name &&
		@subName == o.subName &&
		@length == o.length
end

#hashObject

Hash on the important part - the name and subname.



54
55
56
# File 'lib/linkparser/connection.rb', line 54

def hash 
	[@name, @subName].hash
end

#to_sObject

Stringification delegated to the @general connector.



76
77
78
# File 'lib/linkparser/connection.rb', line 76

def to_s 
	@name + @subName
end