arrayfields 3.5.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,127 @@
1
+ URLS:
2
+
3
+ - http://raa.ruby-lang.org/project/arrayfields/
4
+ - http://www.codeforpeople.com/lib/ruby/arrayfields/
5
+ - http://rubyforge.org/projects/arrayfields/
6
+
7
+ SYNOPSIS:
8
+
9
+ allow keyword access to arrays:
10
+
11
+ require 'arrayfields'
12
+
13
+ fields = 'name', 'age'
14
+ row = [ 'bob', 30 ]
15
+
16
+ row.fields = fields
17
+
18
+ row[ 'name' ] #=> 'bob'
19
+ row.indices 'name', 'age' #=> [ 'bob', 30 ]
20
+
21
+ assigning to un-named fields appends:
22
+
23
+ stack = []
24
+ stack.fields = %w(zero one)
25
+ stack['zero'] = 'zero'
26
+ stack['one'] = 'one'
27
+ stack #=> [ 'zero', 'one' ]
28
+
29
+ useful for database work:
30
+
31
+ relation = pgconn.query sql
32
+ relation.size #=> 65536
33
+
34
+ # yikes! do we really want to re-construct a hash for for each tuple when
35
+ # we already have Arrays?
36
+
37
+ fields = %w(ssn name position)
38
+ table.each{|tuple| tuple.fields = fields}
39
+
40
+ tuples[34578]['ssn'] #=> 574865032
41
+
42
+ LIST OF OVERRIDDEN METHODS:
43
+
44
+ - Array#[]
45
+ - Array#[]=
46
+ - Array#at
47
+ - Array#delete_at
48
+ - Array#fill
49
+ - Array#values_at
50
+ - Array#indices
51
+ - Array#indexes
52
+ - Array#slice
53
+ - Array#slice!
54
+
55
+ LIST OF NEW Array METHODS:
56
+
57
+ - Array#fields=
58
+ - Array#each_with_field
59
+
60
+ DOCS/USAGE/SAMPLE:
61
+
62
+ - lib/arrayfields.rb
63
+ - test/arrayfields.rb
64
+
65
+ AUTHOR:
66
+
67
+ ara.t.howard@noaa.gov
68
+
69
+ HISTORY:
70
+
71
+ 3.5.0:
72
+ - added more hash-like methods
73
+ - update
74
+ - replace
75
+ - invert
76
+
77
+ 3.4.0:
78
+ - added FieldedArray[] ctor
79
+ - added methods to make Arrays with fields set behave more closely to Hashes
80
+ - each_pair
81
+ - each_key
82
+ - each_value
83
+ - fetch
84
+ - has_key?
85
+ - member?
86
+ - key?
87
+ - has_value?
88
+ - value?
89
+ - keys?
90
+ - store
91
+ - values
92
+
93
+ 3.3.0:
94
+ - added gemspec file - thnx Assaph Mehr
95
+ - added FieldedArray proxy class which minimizes modifications to class
96
+ Array and allow ArrayFields to work (potientially) other arraylike object.
97
+ thnks Sean O'Dell
98
+ - added ArrayFields#to_hash method - this seems like an obvious one to add!
99
+ - remedied bug where using append feature of assigning with unknow field
100
+ appedended but did not append to acutal fields
101
+ - added samples
102
+ - created rubyforge accnt @ http://rubyforge.org/projects/arrayfields/
103
+
104
+ 3.2.0:
105
+ - precedence fix in many methods - thnx. nobu
106
+ - test for #slice! were not being run - corrected
107
+ - added test for appeding via "a['new_field'] = 42"
108
+
109
+ 3.1.0:
110
+ - added FieldSet class to reduce ram - thnx. Kirk Haines for profiliing
111
+ memory and prompting this change
112
+
113
+ - interface changed every so slightly so
114
+
115
+ a.fields = 'a', 'b', 'c'
116
+
117
+ is not allowed. use
118
+
119
+ a.fields = %w(a b c)
120
+
121
+ or
122
+
123
+ a.fields = ['a', 'b', 'c']
124
+
125
+
126
+ 3.0.0:
127
+ - added unit tests
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 3.6.0
data/gemspec.rb ADDED
@@ -0,0 +1,23 @@
1
+ lib, version = File::basename(File::dirname(File::expand_path(__FILE__))).split %r/-/, 2
2
+
3
+ require 'rubygems'
4
+
5
+ Gem::Specification::new do |spec|
6
+ spec.name = lib
7
+ spec.version = version
8
+ spec.platform = Gem::Platform::RUBY
9
+ spec.summary = lib
10
+
11
+ spec.files = Dir::glob "**/**"
12
+ spec.executables = Dir::glob("bin/*").map{|exe| File::basename exe}
13
+
14
+ spec.require_path = "lib"
15
+ spec.autorequire = lib
16
+
17
+ spec.has_rdoc = File::exist? "doc"
18
+ spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
19
+
20
+ spec.author = "Ara T. Howard"
21
+ spec.email = "ara.t.howard@noaa.gov"
22
+ spec.homepage = "http://codeforpeople.com/lib/ruby/#{ lib }/"
23
+ end
data/install.rb ADDED
@@ -0,0 +1,206 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rbconfig'
3
+ require 'find'
4
+ require 'ftools'
5
+ require 'tempfile'
6
+ include Config
7
+
8
+ LIBDIR = "lib"
9
+ LIBDIR_MODE = 0644
10
+
11
+ BINDIR = "bin"
12
+ BINDIR_MODE = 0755
13
+
14
+
15
+ $srcdir = CONFIG["srcdir"]
16
+ $version = CONFIG["MAJOR"]+"."+CONFIG["MINOR"]
17
+ $libdir = File.join(CONFIG["libdir"], "ruby", $version)
18
+ $archdir = File.join($libdir, CONFIG["arch"])
19
+ $site_libdir = $:.find {|x| x =~ /site_ruby$/}
20
+ $bindir = CONFIG["bindir"] || CONFIG['BINDIR']
21
+ $ruby_install_name = CONFIG['ruby_install_name'] || CONFIG['RUBY_INSTALL_NAME'] || 'ruby'
22
+ $ruby_ext = CONFIG['EXEEXT'] || ''
23
+ $ruby = File.join($bindir, ($ruby_install_name + $ruby_ext))
24
+
25
+ if !$site_libdir
26
+ $site_libdir = File.join($libdir, "site_ruby")
27
+ elsif $site_libdir !~ %r/#{Regexp.quote($version)}/
28
+ $site_libdir = File.join($site_libdir, $version)
29
+ end
30
+
31
+ def install_rb(srcdir=nil, destdir=nil, mode=nil, bin=nil)
32
+ #{{{
33
+ path = []
34
+ dir = []
35
+ Find.find(srcdir) do |f|
36
+ next unless FileTest.file?(f)
37
+ next if (f = f[srcdir.length+1..-1]) == nil
38
+ next if (/CVS$/ =~ File.dirname(f))
39
+ next if f =~ %r/\.lnk/
40
+ path.push f
41
+ dir |= [File.dirname(f)]
42
+ end
43
+ for f in dir
44
+ next if f == "."
45
+ next if f == "CVS"
46
+ File::makedirs(File.join(destdir, f))
47
+ end
48
+ for f in path
49
+ next if (/\~$/ =~ f)
50
+ next if (/^\./ =~ File.basename(f))
51
+ unless bin
52
+ File::install(File.join(srcdir, f), File.join(destdir, f), mode, true)
53
+ else
54
+ from = File.join(srcdir, f)
55
+ to = File.join(destdir, f)
56
+ shebangify(from) do |sf|
57
+ $deferr.print from, " -> ", File::catname(from, to), "\n"
58
+ $deferr.printf "chmod %04o %s\n", mode, to
59
+ File::install(sf, to, mode, false)
60
+ end
61
+ end
62
+ end
63
+ #}}}
64
+ end
65
+ def shebangify f
66
+ #{{{
67
+ open(f) do |fd|
68
+ buf = fd.read 42
69
+ if buf =~ %r/^\s*#\s*!.*ruby/o
70
+ ftmp = Tempfile::new("#{ $$ }_#{ File::basename(f) }")
71
+ begin
72
+ fd.rewind
73
+ ftmp.puts "#!#{ $ruby }"
74
+ while((buf = fd.read(8192)))
75
+ ftmp.write buf
76
+ end
77
+ ftmp.close
78
+ yield ftmp.path
79
+ ensure
80
+ ftmp.close!
81
+ end
82
+ else
83
+ yield f
84
+ end
85
+ end
86
+ #}}}
87
+ end
88
+ def ARGV.switch
89
+ #{{{
90
+ return nil if self.empty?
91
+ arg = self.shift
92
+ return nil if arg == '--'
93
+ if arg =~ /^-(.)(.*)/
94
+ return arg if $1 == '-'
95
+ raise 'unknown switch "-"' if $2.index('-')
96
+ self.unshift "-#{$2}" if $2.size > 0
97
+ "-#{$1}"
98
+ else
99
+ self.unshift arg
100
+ nil
101
+ end
102
+ #}}}
103
+ end
104
+ def ARGV.req_arg
105
+ #{{{
106
+ self.shift || raise('missing argument')
107
+ #}}}
108
+ end
109
+ def linkify d, linked = []
110
+ #--{{{
111
+ if test ?d, d
112
+ versioned = Dir[ File::join(d, "*-[0-9].[0-9].[0-9].rb") ]
113
+ versioned.each do |v|
114
+ src, dst = v, v.gsub(%r/\-[\d\.]+\.rb$/, '.rb')
115
+ lnk = nil
116
+ begin
117
+ if test ?l, dst
118
+ lnk = "#{ dst }.lnk"
119
+ puts "#{ dst } -> #{ lnk }"
120
+ File::rename dst, lnk
121
+ end
122
+ unless test ?e, dst
123
+ puts "#{ src } -> #{ dst }"
124
+ File::copy src, dst
125
+ linked << dst
126
+ end
127
+ ensure
128
+ if lnk
129
+ at_exit do
130
+ puts "#{ lnk } -> #{ dst }"
131
+ File::rename lnk, dst
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ linked
138
+ #--}}}
139
+ end
140
+
141
+
142
+ #
143
+ # main program
144
+ #
145
+
146
+ libdir = $site_libdir
147
+ bindir = $bindir
148
+ no_linkify = false
149
+ linked = nil
150
+ help = false
151
+
152
+ usage = <<-usage
153
+ #{ File::basename $0 }
154
+ -d, --destdir <destdir>
155
+ -l, --libdir <libdir>
156
+ -b, --bindir <bindir>
157
+ -r, --ruby <ruby>
158
+ -n, --no_linkify
159
+ -s, --sudo
160
+ -h, --help
161
+ usage
162
+
163
+ begin
164
+ while switch = ARGV.switch
165
+ case switch
166
+ when '-d', '--destdir'
167
+ libdir = ARGV.req_arg
168
+ when '-l', '--libdir'
169
+ libdir = ARGV.req_arg
170
+ when '-b', '--bindir'
171
+ bindir = ARGV.req_arg
172
+ when '-r', '--ruby'
173
+ $ruby = ARGV.req_arg
174
+ when '-n', '--no_linkify'
175
+ no_linkify = true
176
+ when '-s', '--sudo'
177
+ sudo = 'sudo'
178
+ when '-h', '--help'
179
+ help = true
180
+ else
181
+ raise "unknown switch #{switch.dump}"
182
+ end
183
+ end
184
+ rescue
185
+ STDERR.puts $!.to_s
186
+ STDERR.puts usage
187
+ exit 1
188
+ end
189
+
190
+ if help
191
+ STDOUT.puts usage
192
+ exit
193
+ end
194
+
195
+ unless no_linkify
196
+ linked = linkify('lib') + linkify('bin')
197
+ end
198
+
199
+ system "#{ $ruby } extconf.rb && make && #{ sudo } make install" if test(?s, 'extconf.rb')
200
+
201
+ install_rb(LIBDIR, libdir, LIBDIR_MODE)
202
+ install_rb(BINDIR, bindir, BINDIR_MODE, bin=true)
203
+
204
+ if linked
205
+ linked.each{|path| File::rm_f path}
206
+ end
@@ -5,7 +5,7 @@
5
5
  #
6
6
  module ArrayFields
7
7
  #{{{
8
- VERSION = '3.5.0'
8
+ VERSION = '3.6.0'
9
9
  #
10
10
  # multiton cache of fields - wraps fields and fieldpos map to save memory
11
11
  #
@@ -45,9 +45,14 @@
45
45
  @fields = fields
46
46
  #}}}
47
47
  end
48
- def pos field
49
- #{{{
50
- @fieldpos[field]
48
+ def pos f
49
+ #{{{
50
+ return @fieldpos[f] if @fieldpos.has_key? f
51
+ f = f.to_s
52
+ return @fieldpos[f] if @fieldpos.has_key? f
53
+ f = f.intern
54
+ return @fieldpos[f] if @fieldpos.has_key? f
55
+ nil
51
56
  #}}}
52
57
  end
53
58
  #}}}
data/lib/arrayfields.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  #
6
6
  module ArrayFields
7
7
  #{{{
8
- VERSION = '3.5.0'
8
+ VERSION = '3.6.0'
9
9
  #
10
10
  # multiton cache of fields - wraps fields and fieldpos map to save memory
11
11
  #
@@ -45,9 +45,14 @@
45
45
  @fields = fields
46
46
  #}}}
47
47
  end
48
- def pos field
49
- #{{{
50
- @fieldpos[field]
48
+ def pos f
49
+ #{{{
50
+ return @fieldpos[f] if @fieldpos.has_key? f
51
+ f = f.to_s
52
+ return @fieldpos[f] if @fieldpos.has_key? f
53
+ f = f.intern
54
+ return @fieldpos[f] if @fieldpos.has_key? f
55
+ nil
51
56
  #}}}
52
57
  end
53
58
  #}}}
data/sample/a.rb ADDED
@@ -0,0 +1,66 @@
1
+ #
2
+ # you must require the arrayfields package
3
+ #
4
+ $:.unshift 'lib'
5
+ $:.unshift '../lib'
6
+ $:.unshift '.'
7
+ require 'arrayfields'
8
+
9
+ #
10
+ # the class Array has only a few added method, one is for setting the fields,
11
+ # when the fields are set for an array THIS INSTANCE ONLY will be modified to
12
+ # allow keyword access. other arrays will not be affected!
13
+ #
14
+ a = [0,1,2]
15
+ fields = ['zero', 'one', 'two']
16
+ a.fields = fields # ONLY the Array 'a' is affected!
17
+ #
18
+ # keyword access is now allowed for many methods
19
+ #
20
+ p a['zero'] #=> 0
21
+ p a['one'] #=> 1
22
+ p a['two'] #=> 2
23
+ p a.at('one') #=> 1
24
+ p a.values_at('zero', 'two') #=> [0, 2]
25
+ #
26
+ # assigmnet is allowed
27
+ #
28
+ a['zero'] = 42
29
+ p a['zero'] #=> 0
30
+ a['zero'] = 0
31
+ #
32
+ # assignment to non-fields results in the element being appended and the field
33
+ # being added for future use (also appended)
34
+ #
35
+ p(a.fields.join(',')) #=> "zero, one, two"
36
+ p a['three'] #=> nil
37
+ a['three'] = 3
38
+ p(a.fields.join(',')) #=> "zero, one, two, three"
39
+ p a['three'] #=> 3
40
+ #
41
+ # other detructive methods are also keyword enabled
42
+ #
43
+ a.fill 42, 'zero', len = a.size
44
+ p(a.values_at(a.fields)) #=> [42, 42, 42, 42]
45
+ a.replace [0,1,2,3]
46
+
47
+ a.slice! 'two', 2
48
+ p a #=> [0,1]
49
+
50
+ __END__
51
+
52
+ the expected output of this program :
53
+
54
+ 0
55
+ 1
56
+ 2
57
+ 1
58
+ [0, 2]
59
+ 42
60
+ "zero,one,two"
61
+ nil
62
+ "zero,one,two,three"
63
+ 3
64
+ [42, 42, 42, 42]
65
+ [0, 1]
66
+
data/test/memtest.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'lib/arrayfields'
2
+ STDOUT.sync = true
3
+
4
+ n = Integer((ARGV.shift or (2 ** 16)))
5
+
6
+ #
7
+ # hash mem usage - around 13016 on my machine
8
+ #
9
+ fork do
10
+ a = []
11
+ n.times do
12
+ a << {'a' => 0, 'b' => 1, 'c' => 2}
13
+ end
14
+
15
+ puts "pid <#{ Process.pid }>"
16
+ print "run top to examine mem usage of <#{ n }> hashes (enter when done) >"
17
+ STDIN.gets
18
+ end
19
+ Process.wait
20
+
21
+
22
+ #
23
+ # arrayfields mem usage - around 8752 on my machine
24
+ #
25
+ fork do
26
+ fields = %w( a b c )
27
+ a = []
28
+ n.times do
29
+ t = [0,1,2]
30
+ t.fields = fields
31
+ t.extend ArrayFields
32
+ a << [0,1,2]
33
+ end
34
+
35
+ puts "pid <#{ Process.pid }>"
36
+ print "run top to examine mem usage of <#{ n }> extended arrays (enter when done) >"
37
+ STDIN.gets
38
+ end
39
+ Process.wait
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: arrayfields
5
5
  version: !ruby/object:Gem::Version
6
- version: 3.5.0
7
- date: 2005-11-01 00:00:00.000000 -07:00
6
+ version: 3.6.0
7
+ date: 2006-08-23 00:00:00.000000 -06:00
8
8
  summary: arrayfields
9
9
  require_paths:
10
10
  - lib
@@ -29,8 +29,18 @@ cert_chain:
29
29
  authors:
30
30
  - Ara T. Howard
31
31
  files:
32
+ - install.rb
33
+ - README
34
+ - VERSION
35
+ - gemspec.rb
36
+ - lib
37
+ - test
38
+ - sample
32
39
  - lib/arrayfields.rb
33
- - lib/arrayfields-3.5.0.rb
40
+ - lib/arrayfields-3.6.0.rb
41
+ - test/arrayfields.rb
42
+ - test/memtest.rb
43
+ - sample/a.rb
34
44
  test_files:
35
45
  - test/arrayfields.rb
36
46
  rdoc_options: []