Class: Association

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/more/facets/association.rb

Overview

Association

General binary association allows one object to be associated with another. It has a variety of uses, link-lists, simple ordered maps and mixed collections, among them.

Usage

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

Limitations

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

Bignum
Fixnum
Date
IPAddr
Process::Status

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparable

#at_least, #at_most, #cap, #clip, #cmp

Constructor Details

#initialize(index, value = nil) ⇒ Association

Returns a new instance of Association.



79
80
81
82
# File 'lib/more/facets/association.rb', line 79

def initialize(index, value=nil)
  @index = index
  @value = value
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



73
74
75
# File 'lib/more/facets/association.rb', line 73

def index
  @index
end

#valueObject

Returns the value of attribute value.



73
74
75
# File 'lib/more/facets/association.rb', line 73

def value
  @value
end

Class Method Details

.[](*args) ⇒ Object



75
76
77
# File 'lib/more/facets/association.rb', line 75

def self.[](*args)
  new(*args)
end

Instance Method Details

#<=>(assoc) ⇒ Object



84
85
86
87
88
# File 'lib/more/facets/association.rb', line 84

def <=>(assoc)
   return -1 if self.value < assoc.value
   return 1 if self.value > assoc.value
   return 0 if self.value == assoc.value
end

#inspectObject



100
101
102
# File 'lib/more/facets/association.rb', line 100

def inspect
  %{#{@index.inspect} >> #{@value.inspect}}
end

#invert!Object



90
91
92
93
94
# File 'lib/more/facets/association.rb', line 90

def invert!
  temp = @index
  @index = @value
  @value = temp
end

#to_aryObject



104
105
106
# File 'lib/more/facets/association.rb', line 104

def to_ary
  [ @index, @value ]
end

#to_sObject



96
97
98
# File 'lib/more/facets/association.rb', line 96

def to_s
  return "#{index.to_s}#{value.to_s}"
end