alib 0.4.0 → 0.5.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.
@@ -0,0 +1,200 @@
1
+
2
+ begin
3
+ require 'rubygems'
4
+ rescue LoadError
5
+ nil
6
+ end
7
+
8
+ begin
9
+ require 'attributes'
10
+ rescue LoadError
11
+ warn <<-msg
12
+ attributes.rb not found!
13
+
14
+ download from
15
+ - http://rubyforge.org/projects/codeforpeople/
16
+
17
+ or
18
+
19
+ - http://codeforpeople.com/lib/ruby/
20
+ msg
21
+ raise
22
+ end
23
+
24
+ #
25
+ # http://en.wikipedia.org/wiki/Prototype-based_programming
26
+ #
27
+
28
+ class Prototype
29
+ VERSION = '0.3.0'
30
+
31
+ module Prototypical
32
+ module ClassMethods
33
+ def __prototype_table__ # uses closure/init to avoid instance-var
34
+ __prototype_singleton_class__{
35
+ table = {}
36
+ define_method('__prototype_table__'){ table }
37
+ }
38
+ __prototype_table__ # does not recurse!
39
+ end
40
+
41
+ def __prototype_table_inject__ other
42
+ __prototype_table__.each do |ivar, value|
43
+ other.instance_variable_set ivar, value
44
+ end
45
+ __prototype_table__.clear
46
+ end
47
+
48
+ def __prototype_eval__ &b
49
+ tid = Thread.current.object_id.abs
50
+ src, dst = "method_missing", "__method_missing_#{ tid }__"
51
+ aliased = false
52
+
53
+ __prototype_singleton_class__{
54
+ unless respond_to? dst
55
+ alias_method dst, src
56
+ aliased = true
57
+ end
58
+
59
+ def method_missing m, *a, &b
60
+ __prototype_barewords_can_set_and_get_ivars_and_define_methods__ m, *a, &b
61
+ end
62
+
63
+ def __prototype_barewords_can_set_and_get_ivars_and_define_methods__ m, *a, &b
64
+ if b
65
+ define_method m, *a, &b
66
+ else
67
+ ivar = "@#{ m }"
68
+ if a.size == 0
69
+ defined?(ivar) ? instance_variable_get(ivar) : super
70
+ elsif a.size == 1
71
+ instance_variable_set ivar, a.shift
72
+ else
73
+ __method_missing__(m, *a, &b)
74
+ end
75
+ end
76
+ end
77
+ }
78
+
79
+ module_eval &b
80
+
81
+ ensure
82
+ __prototype_singleton_class__{ alias_method src, dst } if aliased
83
+ end
84
+
85
+ def __prototype_singleton_class__ &b
86
+ sc =
87
+ class << self
88
+ self
89
+ end
90
+ sc.module_eval &b if b
91
+ sc
92
+ end
93
+
94
+ def __prototype_prototype__ &b
95
+ return unless b
96
+
97
+ __prototype_eval__ &b
98
+
99
+ table =
100
+ instance_variables.inject({}) do |t,ivar|
101
+ value =
102
+ instance_eval do
103
+ begin
104
+ instance_variable_get ivar
105
+ ensure
106
+ remove_instance_variable ivar
107
+ end
108
+ end
109
+ t.update ivar => value
110
+ end
111
+
112
+ __prototype_table__.update table
113
+ end
114
+ end
115
+
116
+ module InstanceMethods
117
+ def initialize *a, &b
118
+ __prototype_init__
119
+ super
120
+ end
121
+
122
+ def __prototype_init__ c = self.class
123
+ c.__prototype_table__.each do |ivar, value|
124
+ #defined = instance_eval "defined? #{ ivar }"
125
+ #unless defined
126
+ instance_variable_set ivar, value
127
+ #end
128
+
129
+ a = ivar[%r/\w+/]
130
+ defined = respond_to? a
131
+ unless defined
132
+ self.class.module_eval{ attribute a }
133
+ end
134
+ end
135
+ end
136
+
137
+ def extend *a, &b
138
+ unless a.empty?
139
+ super
140
+ else
141
+ self.class.__prototype_prototype__ &b
142
+ __prototype_init__
143
+ self
144
+ end
145
+ end
146
+
147
+ def clone *a, &b
148
+ obj = prototype(self.class, *a, &b)
149
+ instance_variables.each do |ivar|
150
+ value = instance_variable_get ivar
151
+ obj.instance_variable_set ivar, value
152
+ end
153
+ obj
154
+ end
155
+ end
156
+
157
+ def self.extend_object c
158
+ c.extend ClassMethods
159
+ c.module_eval{ include InstanceMethods }
160
+ super
161
+ end
162
+ end
163
+
164
+ class << self
165
+ def new parent = Object, *a, &b
166
+ parent = parent.class unless Class === parent
167
+ c = Class.new parent
168
+ c.extend Prototypical
169
+ c.__prototype_table__.update parent.__prototype_table__ if
170
+ parent.respond_to? '__prototype_table__'
171
+ c.__prototype_prototype__ &b
172
+ obj = c.new *a
173
+ obj
174
+ end
175
+
176
+ alias_method "exnihilo", "new"
177
+ alias_method "ex_nihilo", "new"
178
+
179
+ def prototyping obj, *a, &b
180
+ c =
181
+ class << obj
182
+ self
183
+ end
184
+ parent = c.class
185
+ c.extend Prototypical unless Prototypical === c
186
+ c.__prototype_table__.update parent.__prototype_table__ if
187
+ parent.respond_to? '__prototype_table__'
188
+ c.__prototype_prototype__ &b
189
+ obj.__prototype_init__ c
190
+ obj
191
+ end
192
+ end
193
+ end
194
+
195
+ class Object
196
+ def Prototype(*a, &b) Prototype.new *a, &b end
197
+ def prototype(*a, &b) Prototype.new *a, &b end
198
+ def Prototyping(*a, &b) Prototype.prototyping self, *a, &b end
199
+ def prototyping(*a, &b) Prototype.prototyping self, *a, &b end
200
+ end
@@ -0,0 +1,200 @@
1
+
2
+ begin
3
+ require 'rubygems'
4
+ rescue LoadError
5
+ nil
6
+ end
7
+
8
+ begin
9
+ require 'attributes'
10
+ rescue LoadError
11
+ warn <<-msg
12
+ attributes.rb not found!
13
+
14
+ download from
15
+ - http://rubyforge.org/projects/codeforpeople/
16
+
17
+ or
18
+
19
+ - http://codeforpeople.com/lib/ruby/
20
+ msg
21
+ raise
22
+ end
23
+
24
+ #
25
+ # http://en.wikipedia.org/wiki/Prototype-based_programming
26
+ #
27
+
28
+ class Prototype
29
+ VERSION = '0.3.0'
30
+
31
+ module Prototypical
32
+ module ClassMethods
33
+ def __prototype_table__ # uses closure/init to avoid instance-var
34
+ __prototype_singleton_class__{
35
+ table = {}
36
+ define_method('__prototype_table__'){ table }
37
+ }
38
+ __prototype_table__ # does not recurse!
39
+ end
40
+
41
+ def __prototype_table_inject__ other
42
+ __prototype_table__.each do |ivar, value|
43
+ other.instance_variable_set ivar, value
44
+ end
45
+ __prototype_table__.clear
46
+ end
47
+
48
+ def __prototype_eval__ &b
49
+ tid = Thread.current.object_id.abs
50
+ src, dst = "method_missing", "__method_missing_#{ tid }__"
51
+ aliased = false
52
+
53
+ __prototype_singleton_class__{
54
+ unless respond_to? dst
55
+ alias_method dst, src
56
+ aliased = true
57
+ end
58
+
59
+ def method_missing m, *a, &b
60
+ __prototype_barewords_can_set_and_get_ivars_and_define_methods__ m, *a, &b
61
+ end
62
+
63
+ def __prototype_barewords_can_set_and_get_ivars_and_define_methods__ m, *a, &b
64
+ if b
65
+ define_method m, *a, &b
66
+ else
67
+ ivar = "@#{ m }"
68
+ if a.size == 0
69
+ defined?(ivar) ? instance_variable_get(ivar) : super
70
+ elsif a.size == 1
71
+ instance_variable_set ivar, a.shift
72
+ else
73
+ __method_missing__(m, *a, &b)
74
+ end
75
+ end
76
+ end
77
+ }
78
+
79
+ module_eval &b
80
+
81
+ ensure
82
+ __prototype_singleton_class__{ alias_method src, dst } if aliased
83
+ end
84
+
85
+ def __prototype_singleton_class__ &b
86
+ sc =
87
+ class << self
88
+ self
89
+ end
90
+ sc.module_eval &b if b
91
+ sc
92
+ end
93
+
94
+ def __prototype_prototype__ &b
95
+ return unless b
96
+
97
+ __prototype_eval__ &b
98
+
99
+ table =
100
+ instance_variables.inject({}) do |t,ivar|
101
+ value =
102
+ instance_eval do
103
+ begin
104
+ instance_variable_get ivar
105
+ ensure
106
+ remove_instance_variable ivar
107
+ end
108
+ end
109
+ t.update ivar => value
110
+ end
111
+
112
+ __prototype_table__.update table
113
+ end
114
+ end
115
+
116
+ module InstanceMethods
117
+ def initialize *a, &b
118
+ __prototype_init__
119
+ super
120
+ end
121
+
122
+ def __prototype_init__ c = self.class
123
+ c.__prototype_table__.each do |ivar, value|
124
+ #defined = instance_eval "defined? #{ ivar }"
125
+ #unless defined
126
+ instance_variable_set ivar, value
127
+ #end
128
+
129
+ a = ivar[%r/\w+/]
130
+ defined = respond_to? a
131
+ unless defined
132
+ self.class.module_eval{ attribute a }
133
+ end
134
+ end
135
+ end
136
+
137
+ def extend *a, &b
138
+ unless a.empty?
139
+ super
140
+ else
141
+ self.class.__prototype_prototype__ &b
142
+ __prototype_init__
143
+ self
144
+ end
145
+ end
146
+
147
+ def clone *a, &b
148
+ obj = prototype(self.class, *a, &b)
149
+ instance_variables.each do |ivar|
150
+ value = instance_variable_get ivar
151
+ obj.instance_variable_set ivar, value
152
+ end
153
+ obj
154
+ end
155
+ end
156
+
157
+ def self.extend_object c
158
+ c.extend ClassMethods
159
+ c.module_eval{ include InstanceMethods }
160
+ super
161
+ end
162
+ end
163
+
164
+ class << self
165
+ def new parent = Object, *a, &b
166
+ parent = parent.class unless Class === parent
167
+ c = Class.new parent
168
+ c.extend Prototypical
169
+ c.__prototype_table__.update parent.__prototype_table__ if
170
+ parent.respond_to? '__prototype_table__'
171
+ c.__prototype_prototype__ &b
172
+ obj = c.new *a
173
+ obj
174
+ end
175
+
176
+ alias_method "exnihilo", "new"
177
+ alias_method "ex_nihilo", "new"
178
+
179
+ def prototyping obj, *a, &b
180
+ c =
181
+ class << obj
182
+ self
183
+ end
184
+ parent = c.class
185
+ c.extend Prototypical unless Prototypical === c
186
+ c.__prototype_table__.update parent.__prototype_table__ if
187
+ parent.respond_to? '__prototype_table__'
188
+ c.__prototype_prototype__ &b
189
+ obj.__prototype_init__ c
190
+ obj
191
+ end
192
+ end
193
+ end
194
+
195
+ class Object
196
+ def Prototype(*a, &b) Prototype.new *a, &b end
197
+ def prototype(*a, &b) Prototype.new *a, &b end
198
+ def Prototyping(*a, &b) Prototype.prototyping self, *a, &b end
199
+ def prototyping(*a, &b) Prototype.prototyping self, *a, &b end
200
+ end
@@ -0,0 +1,174 @@
1
+ class Object
2
+ def singleton_class obj = self, &b
3
+ #--{{{
4
+ sc =
5
+ class << obj
6
+ self
7
+ end
8
+ sc.module_eval &b
9
+ sc
10
+ #--}}}
11
+ end
12
+ def tap &b
13
+ #--{{{
14
+ b.arity == 1 ? yield(self) : instance_eval(&b)
15
+ return self
16
+ #--}}}
17
+ end
18
+ end
19
+
20
+ class Numeric
21
+ def of head = nil, *tail, &b
22
+ #--{{{
23
+ if head
24
+ a = tail.empty? ? head : [head]+tail
25
+ b = lambda{ a }
26
+ end
27
+ Array.new(self).map &b
28
+ #--}}}
29
+ end
30
+
31
+ def microseconds() Float(self * (10 ** -6)) end
32
+ def milliseconds() Float(self * (10 ** -3)) end
33
+ def seconds() self end
34
+ def minutes() 60 * seconds end
35
+ def hours() 60 * minutes end
36
+ def days() 24 * hours end
37
+ def weeks() 7 * days end
38
+ def months() 30 * days end
39
+ def years() 365 * days end
40
+ def decades() 10 * years end
41
+
42
+ %w[
43
+ microseconds
44
+ milliseconds
45
+ seconds
46
+ minutes
47
+ hours
48
+ days
49
+ weeks
50
+ months
51
+ years
52
+ decades
53
+ ].each{|m| alias_method m.chop, m}
54
+ end
55
+
56
+ class Hash
57
+ def getopt opt, default = nil
58
+ #--{{{
59
+ hash = self
60
+ keys = opt.respond_to?('each') ? opt : [opt]
61
+ keys.each do |key|
62
+ return hash[key] if hash.has_key? key
63
+ key = "#{ key }"
64
+ return hash[key] if hash.has_key? key
65
+ key = key.intern
66
+ return hash[key] if hash.has_key? key
67
+ end
68
+ return default
69
+ #--}}}
70
+ end
71
+ def hasopt? opt, default = nil
72
+ #--{{{
73
+ hash = self
74
+ keys = opt.respond_to?('each') ? opt : [opt]
75
+ keys.each do |key|
76
+ return key if hash.has_key? key
77
+ key = "#{ key }"
78
+ return key if hash.has_key? key
79
+ key = key.intern
80
+ return key if hash.has_key? key
81
+ end
82
+ return default
83
+ #--}}}
84
+ end
85
+ alias_method "hasopt", "hasopt?"
86
+ end
87
+
88
+ class Array
89
+ def self.step i, *a, &b
90
+ #--{{{
91
+ j, s, ignored = *a
92
+ i, j = 0, i if j.nil?
93
+ s ||= (j < i ? -1 : 1)
94
+ list = new
95
+ i.step(j,s){|k| list << k}
96
+ list.map! &b if b
97
+ list
98
+ #--}}}
99
+ end
100
+ end
101
+
102
+ module Enumerable
103
+ def sum n = 0
104
+ inject(n){|n,i| n += i}
105
+ end
106
+ def product n = 1
107
+ return 0 if empty?
108
+ inject(n){|n,i| n *= i}
109
+ end
110
+ end
111
+
112
+ class Module
113
+ def tattrs *list
114
+ #--{{{
115
+ list.flatten.compact.map do |t|
116
+ module_eval <<-code
117
+ def #{ t } *a
118
+ return(send('#{ t }', a.shift)) unless a.empty?
119
+ Thread.current['#{ t }']
120
+ end
121
+ def #{ t }= val
122
+ Thread.current['#{ t }?'] = true
123
+ Thread.current['#{ t }'] = val
124
+ end
125
+ def #{ t }?
126
+ Thread.current['#{ t }?']
127
+ end
128
+ code
129
+ t.to_s
130
+ end
131
+ #--}}}
132
+ end
133
+ alias_method "tattr", "tattrs"
134
+ end
135
+
136
+ =begin
137
+
138
+ class Thread
139
+ class << self
140
+ alias_method "__new__", "new"
141
+ def new *a, &b
142
+ child '__new__', *a, &b
143
+ end
144
+ alias_method "__start__", "start"
145
+ def start *a, &b
146
+ child '__start__', *a, &b
147
+ end
148
+ private
149
+ def child as, *a, &b
150
+ parent = Thread.current
151
+ send(as, *a) do |*a|
152
+ Thread.current.parent = parent
153
+ b.call *a
154
+ end
155
+ end
156
+ end
157
+ def parent
158
+ self['parent']
159
+ end
160
+ def parent= parent
161
+ self['parent'] = parent
162
+ end
163
+ def ancestors
164
+ return self['ancestors'] if self['ancestors']
165
+ list = [t = self]
166
+ while((t = t.parent))
167
+ list << t
168
+ end
169
+ self['ancestors'] = list
170
+ end
171
+ end
172
+
173
+ =end
174
+