├── meta ├── license ├── suite ├── version ├── created ├── name ├── release ├── authors ├── homepage ├── summary ├── repository └── description ├── HISTORY ├── test └── test_association.rb ├── Syckfile ├── README └── lib └── association.rb /meta/license: -------------------------------------------------------------------------------- 1 | LGPLv3 2 | -------------------------------------------------------------------------------- /meta/suite: -------------------------------------------------------------------------------- 1 | death 2 | -------------------------------------------------------------------------------- /meta/version: -------------------------------------------------------------------------------- 1 | 1.0.0 2 | -------------------------------------------------------------------------------- /meta/created: -------------------------------------------------------------------------------- 1 | 2005-01-01 2 | -------------------------------------------------------------------------------- /meta/name: -------------------------------------------------------------------------------- 1 | association 2 | -------------------------------------------------------------------------------- /meta/release: -------------------------------------------------------------------------------- 1 | 2009-06-26 2 | -------------------------------------------------------------------------------- /meta/authors: -------------------------------------------------------------------------------- 1 | trans 2 | -------------------------------------------------------------------------------- /meta/homepage: -------------------------------------------------------------------------------- 1 | http://death.rubyforge.org/ 2 | -------------------------------------------------------------------------------- /meta/summary: -------------------------------------------------------------------------------- 1 | Associate any object to another. 2 | -------------------------------------------------------------------------------- /meta/repository: -------------------------------------------------------------------------------- 1 | svn://rubyforge.org/var/svn/death/association 2 | -------------------------------------------------------------------------------- /HISTORY: -------------------------------------------------------------------------------- 1 | == 1.0.0 // 2009-07-01 2 | 3 | * Ported from Ruby Facets. 4 | 5 | -------------------------------------------------------------------------------- /meta/description: -------------------------------------------------------------------------------- 1 | General binary association allows one object to be 2 | associated with another. It has a variety of uses, 3 | link-lists, simple ordered maps and mixed collections, 4 | among them. 5 | -------------------------------------------------------------------------------- /test/test_association.rb: -------------------------------------------------------------------------------- 1 | require 'association.rb' 2 | require 'test/unit' 3 | 4 | class TC_Associations < Test::Unit::TestCase 5 | 6 | def setup 7 | @complex_hierarchy = [ 8 | 'parent' >> 'child', 9 | 'childless', 10 | 'another_parent' >> [ 11 | 'subchildless', 12 | 'subparent' >> 'subchild' 13 | ] 14 | ] 15 | end 16 | 17 | def test_ohash 18 | k,v = [],[] 19 | ohash = [ 'A' >> '3', 'B' >> '2', 'C' >> '1' ] 20 | ohash.each { |e1,e2| k << e1 ; v << e2 } 21 | assert_equal( ['A','B','C'], k ) 22 | assert_equal( ['3','2','1'], v ) 23 | end 24 | 25 | def test_complex 26 | complex = [ 'Drop Menu' >> [ 'Button 1', 'Button 2', 'Button 3' ], 'Help' ] 27 | assert_equal( 'Drop Menu', complex[0].index ) 28 | end 29 | 30 | def test_associations 31 | complex = [ :a >> :b, :a >> :c ] 32 | assert_equal( [ :b, :c ], :a.associations ) 33 | end 34 | 35 | end 36 | -------------------------------------------------------------------------------- /Syckfile: -------------------------------------------------------------------------------- 1 | --- 2 | email: 3 | service : Email 4 | file : ~ 5 | subject : ~ 6 | mailto : ruby-talk@ruby-lang.org 7 | active : true 8 | 9 | grancher: 10 | service: grancher 11 | active: true 12 | 13 | gemcutter: 14 | service: GemCutter 15 | active: true 16 | 17 | box: 18 | service: Box 19 | active : true 20 | types : [gem, gz] 21 | include: [bin, demo, lib, meta, plug, test, "[A-Z]*"] 22 | exclude: ~ 23 | master : false 24 | 25 | ridoc: 26 | service: RIDoc 27 | include: ~ 28 | exclude: ~ 29 | active : true 30 | 31 | rdoc: 32 | service: RDoc 33 | include: ~ 34 | exclude: [ Syckfile ] 35 | active : true 36 | 37 | syntax: 38 | service : Syntax 39 | loadpath : ~ 40 | exclude : ~ 41 | active : false 42 | 43 | testrb: 44 | service : testrb 45 | tests : ~ 46 | exclude : ~ 47 | loadpath : ~ 48 | requires : ~ 49 | live : false 50 | active : false 51 | 52 | dnote: 53 | service : DNote 54 | loadpath : ~ 55 | labels : ~ 56 | exclude : [work] 57 | output : ~ 58 | format : ~ 59 | active : true 60 | 61 | stats: 62 | service : Stats 63 | title : ~ 64 | loadpath : ~ 65 | exclude : ~ 66 | output : ~ 67 | active : true 68 | 69 | vclog: 70 | service : VClog 71 | formats : [json] 72 | #layout : rel # gnu 73 | typed : false 74 | output : ~ 75 | active : false 76 | 77 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | = Association 2 | 3 | http://death.rubyforge.org 4 | 5 | 6 | == DESCRIPTION 7 | 8 | General binary associations allows one object to be 9 | associated with another. It has a variety of uses, 10 | link-lists, simple ordered maps and mixed collections, 11 | among them. 12 | 13 | 14 | == FEATURES/ISSUES 15 | 16 | * Create linked-lists with operator (#>>). 17 | * Operator notation does not work for: 18 | * Bignum 19 | * Fixnum 20 | * Date 21 | * IPAddr 22 | * Process::Status 23 | Or any other class that defines #>> for something else. 24 | * An object can have multiple associations. 25 | 26 | 27 | == SYNOPSIS 28 | 29 | Gerenal binary association allows one object to be 30 | associated with another. It has a variety of uses, 31 | link-lists, simple ordered maps and mixed collections, 32 | among them. 33 | 34 | Associations can be used to draw simple relationships. 35 | 36 | :Apple >> :Fruit 37 | :Apple >> :Red 38 | :Apple.associations #=> [ :Fruit, :Red ] 39 | 40 | It can also be used for simple lists of ordered pairs. 41 | 42 | c = [ :a >> 1, :b >> 2 ] 43 | c.each { |k,v| puts "#{k} associated with #{v} } 44 | 45 | produces 46 | 47 | a associated with 1 48 | b associated with 2 49 | 50 | The method :>> is used to construct the association. 51 | It is a rarely used method so it is generally available. 52 | But you can't use an Association while extending 53 | any of the following classes becuase they use #>> for 54 | other things. 55 | 56 | Bignum 57 | Fixnum 58 | Date 59 | IPAddr 60 | Process::Status 61 | 62 | 63 | == HOW TO INSTALL 64 | 65 | Describe your installation procedure here. 66 | 67 | To install with RubyGems simply open a console and type: 68 | 69 | gem install association 70 | 71 | Local installation requires Setup.rb (gem install setup), 72 | then download the tarball package and type: 73 | 74 | tar -xvzf association-1.0.0.tgz 75 | cd association-1.0.0 76 | sudo setup.rb all 77 | 78 | Windows users use 'ruby setup.rb all'. 79 | 80 | 81 | == COPYING 82 | 83 | Copyright (c) 2005 The Coding Dead 84 | 85 | This program is ditributed unser the terms of the LGPLv3 license. 86 | 87 | See LICENSE file for details. 88 | 89 | -------------------------------------------------------------------------------- /lib/association.rb: -------------------------------------------------------------------------------- 1 | # = Association 2 | # 3 | # General binary association allows one object to be 4 | # associated with another. It has a variety of uses, 5 | # link-lists, simple ordered maps and mixed collections, 6 | # among them. 7 | # 8 | # == Usage 9 | # 10 | # Associations can be used to draw simple relationships. 11 | # 12 | # :Apple >> :Fruit 13 | # :Apple >> :Red 14 | # 15 | # :Apple.associations #=> [ :Fruit, :Red ] 16 | # 17 | # It can also be used for simple lists of ordered pairs. 18 | # 19 | # c = [ :a >> 1, :b >> 2 ] 20 | # c.each { |k,v| puts "#{k} associated with #{v} } 21 | # 22 | # produces 23 | # 24 | # a associated with 1 25 | # b associated with 2 26 | # 27 | # == Limitations 28 | # 29 | # The method :>> is used to construct the association. 30 | # It is a rarely used method so it is generally available. 31 | # But you can't use an Association while extending 32 | # any of the following classes becuase they use #>> for 33 | # other things. 34 | # 35 | # Bignum 36 | # Fixnum 37 | # Date 38 | # IPAddr 39 | # Process::Status 40 | # 41 | # == Author 42 | # 43 | # * Thomas Sawyer 44 | # 45 | # == License 46 | # 47 | # Copyright (c) 2005 Thomas Sawyer 48 | # 49 | # Ruby License 50 | # 51 | # This module is free software. You may use, modify, and/or redistribute this 52 | # software under the same terms as Ruby. 53 | # 54 | # This program is distributed in the hope that it will be useful, but WITHOUT 55 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 56 | # FOR A PARTICULAR PURPOSE. 57 | 58 | 59 | # = Association 60 | # 61 | # General binary association allows one object to be 62 | # associated with another. It has a variety of uses, 63 | # link-lists, simple ordered maps and mixed collections, 64 | # among them. 65 | # 66 | # == Usage 67 | # 68 | # Associations can be used to draw simple relationships. 69 | # 70 | # :Apple >> :Fruit 71 | # :Apple >> :Red 72 | # 73 | # :Apple.associations #=> [ :Fruit, :Red ] 74 | # 75 | # It can also be used for simple lists of ordered pairs. 76 | # 77 | # c = [ :a >> 1, :b >> 2 ] 78 | # c.each { |k,v| puts "#{k} associated with #{v} } 79 | # 80 | # produces 81 | # 82 | # a associated with 1 83 | # b associated with 2 84 | # 85 | # == Limitations 86 | # 87 | # The method :>> is used to construct the association. 88 | # It is a rarely used method so it is generally available. 89 | # But you can't use an Association while extending 90 | # any of the following classes becuase they use #>> for 91 | # other things. 92 | # 93 | # Bignum 94 | # Fixnum 95 | # Date 96 | # IPAddr 97 | # Process::Status 98 | # 99 | # TODO: Should associations be singleton? 100 | 101 | class Association 102 | include Comparable 103 | 104 | class << self 105 | # Store association references. 106 | def reference 107 | @reference ||= Hash.new{ |h,k,v| h[k]=[] } 108 | end 109 | 110 | def [](index, value) 111 | new(index, value) 112 | end 113 | 114 | #def new(index, value) 115 | # lookup[[index, value]] ||= new(index, value) 116 | #end 117 | 118 | #def lookup 119 | # @lookup ||= {} 120 | #end 121 | end 122 | 123 | attr_accessor :index 124 | attr_accessor :value 125 | 126 | def initialize(index, value=nil) 127 | @index = index 128 | @value = value 129 | 130 | unless index.associations.include?(value) 131 | index.associations << value 132 | end 133 | end 134 | 135 | def <=>(assoc) 136 | return -1 if self.value < assoc.value 137 | return 1 if self.value > assoc.value 138 | return 0 if self.value == assoc.value 139 | end 140 | 141 | def invert! 142 | temp = @index 143 | @index = @value 144 | @value = temp 145 | end 146 | 147 | def to_s 148 | return "#{index.to_s}#{value.to_s}" 149 | end 150 | 151 | def inspect 152 | %{#{@index.inspect} >> #{@value.inspect}} 153 | end 154 | 155 | def to_ary 156 | [ @index, @value ] 157 | end 158 | 159 | # Object extensions. 160 | # 161 | module Kernel 162 | 163 | # Define an association with +self+. 164 | def >>(to) 165 | Association.new(self, to) 166 | end 167 | 168 | def associations 169 | Association.reference[self] 170 | end 171 | 172 | end 173 | 174 | end 175 | 176 | class Object #:nodoc: 177 | include Association::Kernel 178 | end 179 | 180 | 181 | #-- 182 | # Setup the >> method in classes that use it already. 183 | # 184 | # This is a bad idea b/c it can cause backward compability issues. 185 | # 186 | # class Bignum 187 | # alias_method( :rshift, :>>) if method_defined?(:>>) 188 | # remove_method :>> 189 | # end 190 | # 191 | # class Fixnum 192 | # alias_method( :rshift, :>>) if method_defined?(:>>) 193 | # remove_method :>> 194 | # end 195 | # 196 | # class Date 197 | # alias_method( :months_later, :>>) if method_defined?(:>>) 198 | # remove_method :>> 199 | # end 200 | # 201 | # class IPAddr 202 | # alias_method( :rshift, :>>) if method_defined?(:>>) 203 | # remove_method :>> 204 | # end 205 | # 206 | # class Process::Status 207 | # alias_method( :rshift, :>>) if method_defined?(:>>) 208 | # remove_method :>> 209 | # end 210 | #++ 211 | --------------------------------------------------------------------------------