class Hashery::Association

Association is a general binary association that allows one object to be associated with another. It has a variety of uses, such as linked-lists, simple ordered maps and mixed collections, among them.

NOTE: This class is still fairly experimental. And it is not loaded along with the other Hashery libraries when using `require 'hashery'`. It must be required independently.

Associations can be used to draw simple relationships.

:Apple >> :Fruit
:Apple >> :Red

:Apple.associations #=> [ :Fruit, :Red ]

It can also be used for simple lists of ordered pairs.

c = [ :a >> 1, :b >> 2 ]
c.each { |k,v| puts "#{k} associated with #{v} }

produces

a associated with 1
b associated with 2

The method :>> is used to construct the association. It is a rarely used method so it is generally available. But you can't use it for any of the following classes becuase they use >> for other things.

Bignum
Fixnum
Date
IPAddr
Process::Status

Attributes

index[RW]

The “index key” of the association.

value[RW]

The “value” of the association.

Public Class Methods

[](index, value) click to toggle source

Shortcut for new.

index - The “index key” of the association. value - The “value” of the association.

Returns `Association`.

# File lib/hashery/association.rb, line 65
def [](index, value)
  new(index, value)
end
new(index, value=nil) click to toggle source

Initialize new Association.

index - The “index key” of the association. value - The “value” of the association.

# File lib/hashery/association.rb, line 94
def initialize(index, value=nil)
  @index = index
  @value = value

  unless index.associations.include?(value)
    index.associations << value
  end
end
reference() click to toggle source

Store association references.

Returns `Hash` of all associaitons.

# File lib/hashery/association.rb, line 53
def reference
  @reference ||= Hash.new{ |h,k,v| h[k]=[] }
end

Public Instance Methods

<=>(assoc) click to toggle source

Compare the values of two associations.

TODO: Comparions with non-associations?

assoc - The other `Association`.

Returns [Integer] `1`, `0`, or `-1`.

# File lib/hashery/association.rb, line 112
def <=>(assoc)
  return -1 if self.value < assoc.value
  return  1 if self.value > assoc.value
  return  0 if self.value == assoc.value
end
inspect() click to toggle source

Produce a literal code string for creating an association.

Returns [String].

# File lib/hashery/association.rb, line 143
def inspect
  "#{index.inspect} >> #{value.inspect}"
end
invert!() click to toggle source

Invert association, making the index the value and vice-versa.

Returns [Array] with two-elements reversed.

# File lib/hashery/association.rb, line 123
def invert!
  temp = @index
  @index = @value
  @value = temp
end
to_ary() click to toggle source

Convert to two-element associative array.

Returns [Array] Two-element Array of index and value pair.

# File lib/hashery/association.rb, line 152
def to_ary
  [index, value]
end
to_s() click to toggle source

Produce a string representation.

Returns [String].

# File lib/hashery/association.rb, line 134
def to_s
  return "#{index} >> #{value}"
end