prototype 0.0.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 +69 -0
- data/gemspec.rb +23 -0
- data/install.rb +206 -0
- data/lib/prototype-0.0.0.rb +78 -0
- data/lib/prototype.rb +78 -0
- data/prototype.rb +74 -0
- data/samples/a.rb +9 -0
- data/samples/b.rb +17 -0
- data/samples/c.rb +10 -0
- metadata +49 -0
data/README
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
NAME
|
2
|
+
|
3
|
+
prototype.rb
|
4
|
+
|
5
|
+
URIS
|
6
|
+
|
7
|
+
http://codeforpeople.com/lib/ruby/
|
8
|
+
http://rubyforge.org/projects/codeforpeople/
|
9
|
+
|
10
|
+
SYNOPSIS
|
11
|
+
|
12
|
+
prototype.rb implements the prototype design pattern
|
13
|
+
|
14
|
+
http://en.wikipedia.org/wiki/Prototype-based_programming
|
15
|
+
|
16
|
+
for ruby
|
17
|
+
|
18
|
+
WHY
|
19
|
+
|
20
|
+
prototype based programming can look very nice ;-)
|
21
|
+
|
22
|
+
EXAMPLES
|
23
|
+
|
24
|
+
~ > cat samples/a.rb
|
25
|
+
require 'prototype'
|
26
|
+
|
27
|
+
singleton = Prototype.new{
|
28
|
+
@a, @b = 40, 2
|
29
|
+
|
30
|
+
def answer() @a + @b end
|
31
|
+
}
|
32
|
+
|
33
|
+
p singleton.answer
|
34
|
+
|
35
|
+
|
36
|
+
~ > ruby samples/a.rb
|
37
|
+
42
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
~ > cat samples/b.rb
|
43
|
+
require 'prototype'
|
44
|
+
|
45
|
+
DB = Prototype.new{
|
46
|
+
host 'localhost'
|
47
|
+
port 4242
|
48
|
+
|
49
|
+
def connect() p [host, port] end
|
50
|
+
}
|
51
|
+
|
52
|
+
p DB
|
53
|
+
p DB.host
|
54
|
+
p DB.port
|
55
|
+
DB.connect
|
56
|
+
|
57
|
+
|
58
|
+
~ > ruby samples/b.rb
|
59
|
+
"localhost"
|
60
|
+
4242
|
61
|
+
["localhost", 4242]
|
62
|
+
|
63
|
+
|
64
|
+
DOCS
|
65
|
+
|
66
|
+
see
|
67
|
+
|
68
|
+
lib/*rb
|
69
|
+
samples/*rb
|
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
|
@@ -0,0 +1,78 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# http://en.wikipedia.org/wiki/Prototype-based_programming
|
4
|
+
#
|
5
|
+
|
6
|
+
class Prototype
|
7
|
+
VERSION = '0.0.0'
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def version() VERSION end
|
11
|
+
|
12
|
+
def new proto = Object, *a, &b
|
13
|
+
proto = proto.protoclass unless Class === proto
|
14
|
+
|
15
|
+
c = Class.new proto
|
16
|
+
|
17
|
+
c.module_eval do
|
18
|
+
define_method(:protoclass) do
|
19
|
+
c
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
c.module_eval <<-code
|
24
|
+
class << self
|
25
|
+
def method_missing m, *a, &b
|
26
|
+
ivar = "@%s" % m
|
27
|
+
if a.size == 0
|
28
|
+
if defined? ivar
|
29
|
+
instance_variable_get ivar
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
elsif a.size == 1
|
34
|
+
instance_variable_set ivar, a.shift
|
35
|
+
attr_accessor m
|
36
|
+
else
|
37
|
+
super
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
code
|
42
|
+
|
43
|
+
c.module_eval &b if b
|
44
|
+
|
45
|
+
table =
|
46
|
+
c.instance_variables.inject({}) do |t,ivar|
|
47
|
+
value =
|
48
|
+
c.instance_eval do
|
49
|
+
begin
|
50
|
+
instance_variable_get ivar
|
51
|
+
ensure
|
52
|
+
remove_instance_variable ivar
|
53
|
+
end
|
54
|
+
end
|
55
|
+
t.update ivar => value
|
56
|
+
end
|
57
|
+
|
58
|
+
c.const_set 'PROTOTABLE', table
|
59
|
+
|
60
|
+
c.module_eval <<-code
|
61
|
+
def initialize(*a, &b)
|
62
|
+
PROTOTABLE.each do |ivar, value|
|
63
|
+
instance_variable_set ivar, value
|
64
|
+
end
|
65
|
+
super
|
66
|
+
end
|
67
|
+
code
|
68
|
+
|
69
|
+
obj = c.new *a
|
70
|
+
|
71
|
+
obj
|
72
|
+
end
|
73
|
+
|
74
|
+
alias_method "exnihilo", "new"
|
75
|
+
alias_method "ex_nihilo", "new"
|
76
|
+
alias_method "clone", "new"
|
77
|
+
end
|
78
|
+
end
|
data/lib/prototype.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# http://en.wikipedia.org/wiki/Prototype-based_programming
|
4
|
+
#
|
5
|
+
|
6
|
+
class Prototype
|
7
|
+
VERSION = '0.0.0'
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def version() VERSION end
|
11
|
+
|
12
|
+
def new proto = Object, *a, &b
|
13
|
+
proto = proto.protoclass unless Class === proto
|
14
|
+
|
15
|
+
c = Class.new proto
|
16
|
+
|
17
|
+
c.module_eval do
|
18
|
+
define_method(:protoclass) do
|
19
|
+
c
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
c.module_eval <<-code
|
24
|
+
class << self
|
25
|
+
def method_missing m, *a, &b
|
26
|
+
ivar = "@%s" % m
|
27
|
+
if a.size == 0
|
28
|
+
if defined? ivar
|
29
|
+
instance_variable_get ivar
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
elsif a.size == 1
|
34
|
+
instance_variable_set ivar, a.shift
|
35
|
+
attr_accessor m
|
36
|
+
else
|
37
|
+
super
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
code
|
42
|
+
|
43
|
+
c.module_eval &b if b
|
44
|
+
|
45
|
+
table =
|
46
|
+
c.instance_variables.inject({}) do |t,ivar|
|
47
|
+
value =
|
48
|
+
c.instance_eval do
|
49
|
+
begin
|
50
|
+
instance_variable_get ivar
|
51
|
+
ensure
|
52
|
+
remove_instance_variable ivar
|
53
|
+
end
|
54
|
+
end
|
55
|
+
t.update ivar => value
|
56
|
+
end
|
57
|
+
|
58
|
+
c.const_set 'PROTOTABLE', table
|
59
|
+
|
60
|
+
c.module_eval <<-code
|
61
|
+
def initialize(*a, &b)
|
62
|
+
PROTOTABLE.each do |ivar, value|
|
63
|
+
instance_variable_set ivar, value
|
64
|
+
end
|
65
|
+
super
|
66
|
+
end
|
67
|
+
code
|
68
|
+
|
69
|
+
obj = c.new *a
|
70
|
+
|
71
|
+
obj
|
72
|
+
end
|
73
|
+
|
74
|
+
alias_method "exnihilo", "new"
|
75
|
+
alias_method "ex_nihilo", "new"
|
76
|
+
alias_method "clone", "new"
|
77
|
+
end
|
78
|
+
end
|
data/prototype.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# http://en.wikipedia.org/wiki/Prototype-based_programming
|
4
|
+
#
|
5
|
+
|
6
|
+
class Prototype
|
7
|
+
class << self
|
8
|
+
def new proto = Object, *a, &b
|
9
|
+
proto = proto.protoclass unless Class === proto
|
10
|
+
|
11
|
+
c = Class.new proto
|
12
|
+
|
13
|
+
c.module_eval do
|
14
|
+
define_method(:protoclass) do
|
15
|
+
c
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
c.module_eval <<-code
|
20
|
+
class << self
|
21
|
+
def method_missing m, *a, &b
|
22
|
+
ivar = "@%s" % m
|
23
|
+
if a.size == 0
|
24
|
+
if defined? ivar
|
25
|
+
instance_variable_get ivar
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
elsif a.size == 1
|
30
|
+
instance_variable_set ivar, a.shift
|
31
|
+
attr_accessor m
|
32
|
+
else
|
33
|
+
super
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
code
|
38
|
+
|
39
|
+
c.module_eval &b if b
|
40
|
+
|
41
|
+
table =
|
42
|
+
c.instance_variables.inject({}) do |t,ivar|
|
43
|
+
value =
|
44
|
+
c.instance_eval do
|
45
|
+
begin
|
46
|
+
instance_variable_get ivar
|
47
|
+
ensure
|
48
|
+
remove_instance_variable ivar
|
49
|
+
end
|
50
|
+
end
|
51
|
+
t.update ivar => value
|
52
|
+
end
|
53
|
+
|
54
|
+
c.const_set 'PROTOTABLE', table
|
55
|
+
|
56
|
+
c.module_eval <<-code
|
57
|
+
def initialize(*a, &b)
|
58
|
+
PROTOTABLE.each do |ivar, value|
|
59
|
+
instance_variable_set ivar, value
|
60
|
+
end
|
61
|
+
super
|
62
|
+
end
|
63
|
+
code
|
64
|
+
|
65
|
+
obj = c.new *a
|
66
|
+
|
67
|
+
obj
|
68
|
+
end
|
69
|
+
|
70
|
+
alias_method "exnihilo", "new"
|
71
|
+
alias_method "ex_nihilo", "new"
|
72
|
+
alias_method "clone", "new"
|
73
|
+
end
|
74
|
+
end
|
data/samples/a.rb
ADDED
data/samples/b.rb
ADDED
data/samples/c.rb
ADDED
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: prototype
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.0
|
7
|
+
date: 2006-07-12 00:00:00.000000 -06:00
|
8
|
+
summary: prototype
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: ara.t.howard@noaa.gov
|
12
|
+
homepage: http://codeforpeople.com/lib/ruby/prototype/
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: prototype
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
signing_key:
|
28
|
+
cert_chain:
|
29
|
+
authors:
|
30
|
+
- Ara T. Howard
|
31
|
+
files:
|
32
|
+
- lib
|
33
|
+
- prototype.rb
|
34
|
+
- install.rb
|
35
|
+
- gemspec.rb
|
36
|
+
- samples
|
37
|
+
- README
|
38
|
+
- lib/prototype-0.0.0.rb
|
39
|
+
- lib/prototype.rb
|
40
|
+
- samples/c.rb
|
41
|
+
- samples/a.rb
|
42
|
+
- samples/b.rb
|
43
|
+
test_files: []
|
44
|
+
rdoc_options: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
requirements: []
|
49
|
+
dependencies: []
|