/usr/lib/ruby/vendor_ruby/hashery/association.rb is in ruby-hashery 2.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | module Hashery
# TODO: Should associations be singleton?
#
# TODO: Is it really wise to keep a table of all associations?
# 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
#
class Association
include Comparable
class << self
#
# Store association references.
#
# Returns `Hash` of all associaitons.
#
def reference
@reference ||= Hash.new{ |h,k,v| h[k]=[] }
end
#
# Shortcut for #new.
#
# index - The "index key" of the association.
# value - The "value" of the association.
#
# Returns `Association`.
#
def [](index, value)
new(index, value)
end
#def new(index, value)
# lookup[[index, value]] ||= new(index, value)
#end
#def lookup
# @lookup ||= {}
#end
end
#
# The "index key" of the association.
#
attr_accessor :index
#
# The "value" of the association.
#
attr_accessor :value
#
# Initialize new Association.
#
# index - The "index key" of the association.
# value - The "value" of the association.
#
def initialize(index, value=nil)
@index = index
@value = value
unless index.associations.include?(value)
index.associations << value
end
end
#
# Compare the values of two associations.
#
# TODO: Comparions with non-associations?
#
# assoc - The other `Association`.
#
# Returns [Integer] `1`, `0`, or `-1`.
#
def <=>(assoc)
return -1 if self.value < assoc.value
return 1 if self.value > assoc.value
return 0 if self.value == assoc.value
end
#
# Invert association, making the index the value and vice-versa.
#
# Returns [Array] with two-elements reversed.
#
def invert!
temp = @index
@index = @value
@value = temp
end
#
# Produce a string representation.
#
# Returns [String].
#
def to_s
return "#{index} >> #{value}"
end
#
# Produce a literal code string for creating an association.
#
# Returns [String].
#
def inspect
"#{index.inspect} >> #{value.inspect}"
end
#
# Convert to two-element associative array.
#
# Returns [Array] Two-element Array of index and value pair.
#
def to_ary
[index, value]
end
#
# Object extensions.
#
module Kernel
#
# Define an association for +self+.
#
# to - The value of the association.
#
# Returns [Association].
#
def >>(to)
Association.new(self, to)
end
#
# List of associations for this object.
#
# Returns an `Array` of `Associations`.
#
def associations
Association.reference[self]
end
end
end
end
class Object #:nodoc:
include Hashery::Association::Kernel
end
#--
# Setup the >> method in classes that use it already.
#
# This is a bad idea b/c it can cause backward compability issues.
#
# class Bignum
# alias_method( :rshift, :>>) if method_defined?(:>>)
# remove_method :>>
# end
#
# class Fixnum
# alias_method( :rshift, :>>) if method_defined?(:>>)
# remove_method :>>
# end
#
# class Date
# alias_method( :months_later, :>>) if method_defined?(:>>)
# remove_method :>>
# end
#
# class IPAddr
# alias_method( :rshift, :>>) if method_defined?(:>>)
# remove_method :>>
# end
#
# class Process::Status
# alias_method( :rshift, :>>) if method_defined?(:>>)
# remove_method :>>
# end
#++
# Copyright (c) 2005 Rubyworks, Thomas Sawyer
|