orderedhash 0.0.1

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/gemspec.rb ADDED
@@ -0,0 +1,28 @@
1
+
2
+ lib, version = File::basename(File::dirname(File::expand_path(__FILE__))).split %r/-/, 2
3
+
4
+ require 'rubygems'
5
+
6
+ Gem::Specification::new do |spec|
7
+ $VERBOSE = nil
8
+ spec.name = lib
9
+ spec.version = version
10
+ spec.platform = Gem::Platform::RUBY
11
+ spec.summary = lib
12
+
13
+ spec.files = Dir::glob "**/**"
14
+ spec.executables = Dir::glob("bin/*").map{|exe| File::basename exe}
15
+
16
+ spec.require_path = "lib"
17
+ spec.autorequire = lib
18
+
19
+ spec.has_rdoc = File::exist? "doc"
20
+ spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
21
+ #spec.add_dependency 'lib', '>= version'
22
+
23
+ spec.extensions << "extconf.rb" if File::exists? "extconf.rb"
24
+
25
+ spec.author = "Ara T. Howard"
26
+ spec.email = "ara.t.howard@gmail.com"
27
+ spec.homepage = "http://codeforpeople.com/lib/ruby/#{ lib }/"
28
+ end
data/install.rb ADDED
@@ -0,0 +1,210 @@
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
+ system "#{ sudo } #{ $ruby } pre-install.rb" if test(?s, 'pre-install.rb')
196
+
197
+ unless no_linkify
198
+ linked = linkify('lib') + linkify('bin')
199
+ end
200
+
201
+ system "#{ $ruby } extconf.rb && make && #{ sudo } make install" if test(?s, 'extconf.rb')
202
+
203
+ install_rb(LIBDIR, libdir, LIBDIR_MODE)
204
+ install_rb(BINDIR, bindir, BINDIR_MODE, bin=true)
205
+
206
+ if linked
207
+ linked.each{|path| File::rm_f path}
208
+ end
209
+
210
+ system "#{ sudo } #{ $ruby } post-install.rb" if test(?s, 'post-install.rb')
@@ -0,0 +1,23 @@
1
+ #
2
+ # auto vivifying ordered hash that dumps as yaml nicely
3
+ #
4
+ class AutoOrderedHash < OrderedHash
5
+ #--{{{
6
+ def initialize(*args)
7
+ #--{{{
8
+ super(*args){|a,k| a[k] = __class__.new(*args)}
9
+ #--}}}
10
+ end
11
+ def class # for nice yaml
12
+ #--{{{
13
+ Hash
14
+ #--}}}
15
+ end
16
+ def __class__
17
+ #--{{{
18
+ AutoOrderedHash
19
+ #--}}}
20
+ end
21
+ #--}}}
22
+ end # class AutoOrderedHash
23
+ OrderedAutoHash = AutoOrderedHash
@@ -0,0 +1,246 @@
1
+ # AUTHOR
2
+ # jan molic /mig/at/1984/dot/cz/
3
+ #
4
+ # DESCRIPTION
5
+ # Hash with preserved order and some array-like extensions
6
+ # Public domain.
7
+ #
8
+ # THANKS
9
+ # Andrew Johnson for his suggestions and fixes of Hash[],
10
+ # merge, to_a, inspect and shift
11
+ class OrderedHash < ::Hash
12
+ #--{{{
13
+ attr_accessor :order
14
+
15
+ class << self
16
+ #--{{{
17
+ def [] *args
18
+ #--{{{
19
+ hsh = OrderedHash.new
20
+ if Hash === args[0]
21
+ hsh.replace args[0]
22
+ elsif (args.size % 2) != 0
23
+ raise ArgumentError, "odd number of elements for Hash"
24
+ else
25
+ 0.step(args.size - 1, 2) do |a|
26
+ b = a + 1
27
+ hsh[args[a]] = args[b]
28
+ end
29
+ end
30
+ hsh
31
+ #--}}}
32
+ end
33
+ #--}}}
34
+ end
35
+ def initialize(*a, &b)
36
+ #--{{{
37
+ super
38
+ @order = []
39
+ #--}}}
40
+ end
41
+ def store_only a,b
42
+ #--{{{
43
+ store a,b
44
+ #--}}}
45
+ end
46
+ alias orig_store store
47
+ def store a,b
48
+ #--{{{
49
+ @order.push a unless has_key? a
50
+ super a,b
51
+ #--}}}
52
+ end
53
+ alias []= store
54
+ def == hsh2
55
+ #--{{{
56
+ return false if @order != hsh2.order
57
+ super hsh2
58
+ #--}}}
59
+ end
60
+ def clear
61
+ #--{{{
62
+ @order = []
63
+ super
64
+ #--}}}
65
+ end
66
+ def delete key
67
+ #--{{{
68
+ @order.delete key
69
+ super
70
+ #--}}}
71
+ end
72
+ def each_key
73
+ #--{{{
74
+ @order.each { |k| yield k }
75
+ self
76
+ #--}}}
77
+ end
78
+ def each_value
79
+ #--{{{
80
+ @order.each { |k| yield self[k] }
81
+ self
82
+ #--}}}
83
+ end
84
+ def each
85
+ #--{{{
86
+ @order.each { |k| yield k,self[k] }
87
+ self
88
+ #--}}}
89
+ end
90
+ alias each_pair each
91
+ def delete_if
92
+ #--{{{
93
+ @order.clone.each { |k|
94
+ delete k if yield
95
+ }
96
+ self
97
+ #--}}}
98
+ end
99
+ def values
100
+ #--{{{
101
+ ary = []
102
+ @order.each { |k| ary.push self[k] }
103
+ ary
104
+ #--}}}
105
+ end
106
+ def keys
107
+ #--{{{
108
+ @order
109
+ #--}}}
110
+ end
111
+ def invert
112
+ #--{{{
113
+ hsh2 = Hash.new
114
+ @order.each { |k| hsh2[self[k]] = k }
115
+ hsh2
116
+ #--}}}
117
+ end
118
+ def reject &block
119
+ #--{{{
120
+ self.dup.delete_if &block
121
+ #--}}}
122
+ end
123
+ def reject! &block
124
+ #--{{{
125
+ hsh2 = reject &block
126
+ self == hsh2 ? nil : hsh2
127
+ #--}}}
128
+ end
129
+ def replace hsh2
130
+ #--{{{
131
+ @order = hsh2.keys
132
+ super hsh2
133
+ #--}}}
134
+ end
135
+ def shift
136
+ #--{{{
137
+ key = @order.first
138
+ key ? [key,delete(key)] : super
139
+ #--}}}
140
+ end
141
+ def unshift k,v
142
+ #--{{{
143
+ unless self.include? k
144
+ @order.unshift k
145
+ orig_store(k,v)
146
+ true
147
+ else
148
+ false
149
+ end
150
+ #--}}}
151
+ end
152
+ def push k,v
153
+ #--{{{
154
+ unless self.include? k
155
+ @order.push k
156
+ orig_store(k,v)
157
+ true
158
+ else
159
+ false
160
+ end
161
+ #--}}}
162
+ end
163
+ def pop
164
+ #--{{{
165
+ key = @order.last
166
+ key ? [key,delete(key)] : nil
167
+ #--}}}
168
+ end
169
+ def to_a
170
+ #--{{{
171
+ ary = []
172
+ each { |k,v| ary << [k,v] }
173
+ ary
174
+ #--}}}
175
+ end
176
+ def to_s
177
+ #--{{{
178
+ self.to_a.to_s
179
+ #--}}}
180
+ end
181
+ def inspect
182
+ #--{{{
183
+ ary = []
184
+ each {|k,v| ary << k.inspect + "=>" + v.inspect}
185
+ '{' + ary.join(", ") + '}'
186
+ #--}}}
187
+ end
188
+ def update hsh2
189
+ #--{{{
190
+ hsh2.each { |k,v| self[k] = v }
191
+ self
192
+ #--}}}
193
+ end
194
+ alias :merge! update
195
+ def merge hsh2
196
+ #--{{{
197
+ self.dup update(hsh2)
198
+ #--}}}
199
+ end
200
+ def select
201
+ #--{{{
202
+ ary = []
203
+ each { |k,v| ary << [k,v] if yield k,v }
204
+ ary
205
+ #--}}}
206
+ end
207
+ def class
208
+ #--{{{
209
+ Hash
210
+ #--}}}
211
+ end
212
+ def __class__
213
+ #--{{{
214
+ OrderedHash
215
+ #--}}}
216
+ end
217
+
218
+ attr_accessor "to_yaml_style"
219
+ def yaml_inline= bool
220
+ if respond_to?("to_yaml_style")
221
+ self.to_yaml_style = :inline
222
+ else
223
+ unless defined? @__yaml_inline_meth
224
+ @__yaml_inline_meth =
225
+ lambda {|opts|
226
+ YAML::quick_emit(object_id, opts) {|emitter|
227
+ emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }'
228
+ }
229
+ }
230
+ class << self
231
+ def to_yaml opts = {}
232
+ begin
233
+ @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
234
+ rescue
235
+ @to_yaml_style = :inline
236
+ super
237
+ end
238
+ end
239
+ end
240
+ end
241
+ end
242
+ @__yaml_inline = bool
243
+ end
244
+ def yaml_inline!() self.yaml_inline = true end
245
+ #--}}}
246
+ end # class OrderedHash
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: orderedhash
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-09-26 00:00:00 -06:00
8
+ summary: orderedhash
9
+ require_paths:
10
+ - lib
11
+ email: ara.t.howard@gmail.com
12
+ homepage: http://codeforpeople.com/lib/ruby/orderedhash/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: orderedhash
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Ara T. Howard
31
+ files:
32
+ - gemspec.rb
33
+ - install.rb
34
+ - lib
35
+ - lib/orderedautohash.rb
36
+ - lib/orderedhash.rb
37
+ test_files: []
38
+
39
+ rdoc_options: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ requirements: []
48
+
49
+ dependencies: []
50
+