ckuru-tools 0.0.7 → 0.0.9
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/lib/ckuru-tools.rb +70 -60
- data/lib/ckuru-tools.rb~ +490 -0
- metadata +5 -4
data/lib/ckuru-tools.rb
CHANGED
|
@@ -44,6 +44,72 @@ EOF
|
|
|
44
44
|
|
|
45
45
|
module CkuruTools
|
|
46
46
|
|
|
47
|
+
def self.parameters(h,*args)
|
|
48
|
+
raise ArgumentError.new("argument to #{self.class}##{current_method} must be of class Hash; you may get this error if you don't call a function with a hash; check the initial call") unless h.is_a? Hash
|
|
49
|
+
ret = []
|
|
50
|
+
args.each do |a|
|
|
51
|
+
name,options = a
|
|
52
|
+
options = options || {}
|
|
53
|
+
unless h[:no_recurse]
|
|
54
|
+
vals = only_these_parameters(
|
|
55
|
+
options.merge!(:no_recurse => true),
|
|
56
|
+
[:instance_that_inherits_from, {:instance_of => Class}],
|
|
57
|
+
[:instance_of, {:instance_of => Class}],
|
|
58
|
+
[:klass_that_inherits_from, {:instance_of => Class}],
|
|
59
|
+
[:klass_of, {:instance_of => Class}],
|
|
60
|
+
[:no_recurse, {:instance_of => TrueClass}],
|
|
61
|
+
[:required, {:instance_of => TrueClass}],
|
|
62
|
+
[:default, {:instance_of => TrueClass}]
|
|
63
|
+
)
|
|
64
|
+
instance_that_inherits_from, instance_of, klass_that_inherits_from, klass_of, no_recurse, required, default = vals
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
if val = h[name]
|
|
68
|
+
if instance_that_inherits_from
|
|
69
|
+
unless val.class.inherits_from? instance_that_inherits_from
|
|
70
|
+
raise ArgumentError.new(
|
|
71
|
+
"argument :#{name} to #{self.class}##{calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not")
|
|
72
|
+
end
|
|
73
|
+
elsif instance_of
|
|
74
|
+
unless val.class == instance_of
|
|
75
|
+
raise ArgumentError.new(
|
|
76
|
+
"argument :#{name} to #{self.class}##{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
|
|
77
|
+
end
|
|
78
|
+
elsif klass_that_inherits_from
|
|
79
|
+
unless val.inherits_from? klass_that_inherits_from
|
|
80
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
|
81
|
+
end
|
|
82
|
+
elsif klass_of
|
|
83
|
+
unless val == klass_to
|
|
84
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must be of class #{klass_of}, not #{val}")
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
else
|
|
88
|
+
if options[:default]
|
|
89
|
+
val = options[:default]
|
|
90
|
+
elsif options[:required]
|
|
91
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} is required")
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
ret.push val
|
|
95
|
+
end
|
|
96
|
+
ret
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def self.only_these_parameters(h,*args)
|
|
101
|
+
ret = parameters(h,*args)
|
|
102
|
+
keys = h.keys
|
|
103
|
+
args.each do |a|
|
|
104
|
+
name,options = a
|
|
105
|
+
keys.delete name
|
|
106
|
+
end
|
|
107
|
+
if keys.length > 0
|
|
108
|
+
raise ArgumentError.new("unknown parameters #{keys.inspect} passed to #{self.class}##{calling_method}")
|
|
109
|
+
end
|
|
110
|
+
ret
|
|
111
|
+
end
|
|
112
|
+
|
|
47
113
|
def self.validate_hash_arguments(h,*args)
|
|
48
114
|
raise ArgumentError.new("argument to #{current_method} must be of class Hash") unless h.is_a? Hash
|
|
49
115
|
ret = []
|
|
@@ -80,7 +146,7 @@ EOF
|
|
|
80
146
|
raise ArgumentError.new("argument :#{name} to #{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
|
81
147
|
end
|
|
82
148
|
elsif klass_of
|
|
83
|
-
unless val ==
|
|
149
|
+
unless val == klass_of
|
|
84
150
|
raise ArgumentError.new("argument :#{name} to #{calling_method} must be of class #{klass_of}, not #{val}")
|
|
85
151
|
end
|
|
86
152
|
end
|
|
@@ -175,69 +241,12 @@ EOF
|
|
|
175
241
|
# view = parameters(h,[:view,{:klass => View,:required => true}])
|
|
176
242
|
|
|
177
243
|
def parameters(h,*args)
|
|
178
|
-
|
|
179
|
-
ret = []
|
|
180
|
-
args.each do |a|
|
|
181
|
-
name,options = a
|
|
182
|
-
options = options || {}
|
|
183
|
-
unless h[:no_recurse]
|
|
184
|
-
vals = only_these_parameters(
|
|
185
|
-
options.merge!(:no_recurse => true),
|
|
186
|
-
[:instance_that_inherits_from, {:instance_of => Class}],
|
|
187
|
-
[:instance_of, {:instance_of => Class}],
|
|
188
|
-
[:klass_that_inherits_from, {:instance_of => Class}],
|
|
189
|
-
[:klass_of, {:instance_of => Class}],
|
|
190
|
-
[:no_recurse, {:instance_of => TrueClass}],
|
|
191
|
-
[:required, {:instance_of => TrueClass}],
|
|
192
|
-
[:default, {:instance_of => TrueClass}]
|
|
193
|
-
)
|
|
194
|
-
instance_that_inherits_from, instance_of, klass_that_inherits_from, klass_of, no_recurse, required, default = vals
|
|
195
|
-
end
|
|
196
|
-
|
|
197
|
-
if val = h[name]
|
|
198
|
-
if instance_that_inherits_from
|
|
199
|
-
unless val.class.inherits_from? instance_that_inherits_from
|
|
200
|
-
raise ArgumentError.new(
|
|
201
|
-
"argument :#{name} to #{self.class}##{calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not")
|
|
202
|
-
end
|
|
203
|
-
elsif instance_of
|
|
204
|
-
unless val.class == instance_of
|
|
205
|
-
raise ArgumentError.new(
|
|
206
|
-
"argument :#{name} to #{self.class}##{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
|
|
207
|
-
end
|
|
208
|
-
elsif klass_that_inherits_from
|
|
209
|
-
unless val.inherits_from? klass
|
|
210
|
-
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
|
211
|
-
end
|
|
212
|
-
elsif klass_of
|
|
213
|
-
unless val == klass
|
|
214
|
-
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must be of class #{klass_of}, not #{val}")
|
|
215
|
-
end
|
|
216
|
-
end
|
|
217
|
-
else
|
|
218
|
-
if options[:default]
|
|
219
|
-
val = options[:default]
|
|
220
|
-
elsif options[:required]
|
|
221
|
-
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} is required")
|
|
222
|
-
end
|
|
223
|
-
end
|
|
224
|
-
ret.push val
|
|
225
|
-
end
|
|
226
|
-
ret
|
|
244
|
+
CkuruTools.parameters(h,args)
|
|
227
245
|
end
|
|
228
246
|
|
|
229
247
|
# insure that only the defined parameters have been passed to a function
|
|
230
248
|
def only_these_parameters(h,*args)
|
|
231
|
-
|
|
232
|
-
keys = h.keys
|
|
233
|
-
args.each do |a|
|
|
234
|
-
name,options = a
|
|
235
|
-
keys.delete name
|
|
236
|
-
end
|
|
237
|
-
if keys.length > 0
|
|
238
|
-
raise ArgumentError.new("unknown parameters #{keys.inspect} passed to #{self.class}##{calling_method}")
|
|
239
|
-
end
|
|
240
|
-
ret
|
|
249
|
+
CkuruTools.only_these_parameters(h,args)
|
|
241
250
|
end
|
|
242
251
|
|
|
243
252
|
|
|
@@ -399,6 +408,7 @@ EOF
|
|
|
399
408
|
require 'ruby-debug'
|
|
400
409
|
end
|
|
401
410
|
end
|
|
411
|
+
raise "sorry ...exiting"
|
|
402
412
|
end
|
|
403
413
|
end
|
|
404
414
|
|
data/lib/ckuru-tools.rb~
ADDED
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
# $Id$
|
|
2
|
+
|
|
3
|
+
# Equivalent to a header guard in C/C++
|
|
4
|
+
# Used to prevent the class/module from being loaded more than once
|
|
5
|
+
unless defined? CkuruTools
|
|
6
|
+
|
|
7
|
+
class Module
|
|
8
|
+
def instance_of_class_accessor( klass, *symbols )
|
|
9
|
+
|
|
10
|
+
symbols.each do |symbol|
|
|
11
|
+
module_eval( "def #{symbol}() @#{symbol}; end" )
|
|
12
|
+
module_eval( "def #{symbol}=(val) raise ArgumentError.new('#{symbol} must be a #{klass}') unless val.class.inherits_from? #{klass} ; @#{symbol} = val; end" )
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
def class_accessor( klass, *symbols )
|
|
16
|
+
symbols.each do |symbol|
|
|
17
|
+
module_eval( "def #{symbol}() @#{symbol}; end" )
|
|
18
|
+
module_eval( "
|
|
19
|
+
def #{symbol}=(val)
|
|
20
|
+
raise ArgumentError.new('argument to #{symbol} must be a class') unless val.is_a? Class
|
|
21
|
+
raise ArgumentError.new('#{symbol} must be a #{klass}') unless val.inherits_from? #{klass}
|
|
22
|
+
@#{symbol} = val
|
|
23
|
+
end" )
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# raise ArgumentError.new("assignment must be of class CkuruTools::TypedArray, not \#{arr.class}") unless arr.instance_inherits_from? CkuruTools::TypedArray
|
|
28
|
+
# raise ArgumentError.new("TypedArray must require #{klass}") unless arr.required_type == #{klass}
|
|
29
|
+
def typed_array_accessor(klass,*symbols)
|
|
30
|
+
symbols.each do |symbol|
|
|
31
|
+
module_eval( "def #{symbol}() @#{symbol}; end" )
|
|
32
|
+
module_eval <<"EOF"
|
|
33
|
+
def #{symbol}=(arr)
|
|
34
|
+
@#{symbol} = CkuruTools::TypedArray.new(#{klass})
|
|
35
|
+
arr.each do |elem|
|
|
36
|
+
@#{symbol}.push elem
|
|
37
|
+
end
|
|
38
|
+
@#{symbol}
|
|
39
|
+
end
|
|
40
|
+
EOF
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
module CkuruTools
|
|
46
|
+
|
|
47
|
+
def self.validate_hash_arguments(h,*args)
|
|
48
|
+
raise ArgumentError.new("argument to #{current_method} must be of class Hash") unless h.is_a? Hash
|
|
49
|
+
ret = []
|
|
50
|
+
args.each do |a|
|
|
51
|
+
name,options = a
|
|
52
|
+
options = options || {}
|
|
53
|
+
unless h[:no_recurse]
|
|
54
|
+
vals = only_these_parameters(
|
|
55
|
+
options.merge!(:no_recurse => true),
|
|
56
|
+
[:instance_that_inherits_from, {:instance_of => Class}],
|
|
57
|
+
[:instance_of, {:instance_of => Class}],
|
|
58
|
+
[:klass_that_inherits_from, {:instance_of => Class}],
|
|
59
|
+
[:klass_of, {:instance_of => Class}],
|
|
60
|
+
[:no_recurse, {:instance_of => TrueClass}],
|
|
61
|
+
[:required, {:instance_of => TrueClass}],
|
|
62
|
+
[:default, {:instance_of => TrueClass}]
|
|
63
|
+
)
|
|
64
|
+
instance_that_inherits_from, instance_of, klass_that_inherits_from, klass_of, no_recurse, required, default = vals
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
if val = h[name]
|
|
68
|
+
if instance_that_inherits_from
|
|
69
|
+
unless val.class.inherits_from? instance_that_inherits_from
|
|
70
|
+
raise ArgumentError.new(
|
|
71
|
+
"argument :#{name} to #{calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not")
|
|
72
|
+
end
|
|
73
|
+
elsif instance_of
|
|
74
|
+
unless val.class == instance_of
|
|
75
|
+
raise ArgumentError.new(
|
|
76
|
+
"argument :#{name} to #{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
|
|
77
|
+
end
|
|
78
|
+
elsif klass_that_inherits_from
|
|
79
|
+
unless val.inherits_from? klass
|
|
80
|
+
raise ArgumentError.new("argument :#{name} to #{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
|
81
|
+
end
|
|
82
|
+
elsif klass_of
|
|
83
|
+
unless val == klass_of
|
|
84
|
+
raise ArgumentError.new("argument :#{name} to #{calling_method} must be of class #{klass_of}, not #{val}")
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
else
|
|
88
|
+
if options[:default]
|
|
89
|
+
val = options[:default]
|
|
90
|
+
elsif options[:required]
|
|
91
|
+
raise ArgumentError.new("argument :#{name} to #{calling_method} is required")
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
ret.push val
|
|
95
|
+
end
|
|
96
|
+
ret
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def self.class_space
|
|
101
|
+
ret = TypedArray.new(Class)
|
|
102
|
+
ObjectSpace.each_object {|x| ret.push(x) if x.is_a? Class}
|
|
103
|
+
ret.uniq
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# :stopdoc:
|
|
107
|
+
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
|
108
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
|
109
|
+
# :startdoc:
|
|
110
|
+
|
|
111
|
+
# Returns the version string for the library.
|
|
112
|
+
#
|
|
113
|
+
def self.version
|
|
114
|
+
VERSION
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Returns the library path for the module. If any arguments are given,
|
|
118
|
+
# they will be joined to the end of the libray path using
|
|
119
|
+
# <tt>File.join</tt>.
|
|
120
|
+
#
|
|
121
|
+
def self.libpath( *args )
|
|
122
|
+
args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Returns the lpath for the module. If any arguments are given,
|
|
126
|
+
# they will be joined to the end of the path using
|
|
127
|
+
# <tt>File.join</tt>.
|
|
128
|
+
#
|
|
129
|
+
def self.path( *args )
|
|
130
|
+
args.empty? ? PATH : ::File.join(PATH, *args)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Utility method used to rquire all files ending in .rb that lie in the
|
|
134
|
+
# directory below this file that has the same name as the filename passed
|
|
135
|
+
# in. Optionally, a specific _directory_ name can be passed in such that
|
|
136
|
+
# the _filename_ does not have to be equivalent to the directory.
|
|
137
|
+
#
|
|
138
|
+
def self.require_all_libs_relative_to( fname, dir = nil )
|
|
139
|
+
dir ||= ::File.basename(fname, '.*')
|
|
140
|
+
search_me = ::File.expand_path(
|
|
141
|
+
::File.join(::File.dirname(fname), dir, '**', '*.rb'))
|
|
142
|
+
|
|
143
|
+
Dir.glob(search_me).sort.each {|rb| require rb}
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
#
|
|
147
|
+
# extending HashInitalizerClass enables you to get Object.new(hash) functionality
|
|
148
|
+
# where each key will dispatch to an object setter method.
|
|
149
|
+
#
|
|
150
|
+
# Usage:
|
|
151
|
+
#
|
|
152
|
+
# class MyClass < HashInitalizerClass
|
|
153
|
+
# ..attr_accessor :foo
|
|
154
|
+
# end
|
|
155
|
+
#
|
|
156
|
+
# m = MyClass(:foo => "bar")
|
|
157
|
+
# m.foo
|
|
158
|
+
# => "bar"
|
|
159
|
+
#
|
|
160
|
+
#
|
|
161
|
+
|
|
162
|
+
class HashInitializerClass
|
|
163
|
+
# check for required attributes
|
|
164
|
+
def required_attributes(*symbols)
|
|
165
|
+
symbols.each do |symbol|
|
|
166
|
+
raise ArgumentError.new("attribute :#{symbol} must be set in #{self.class}.initialize") if self.send(symbol).nil?
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# parse and return parameters from a hash
|
|
171
|
+
#
|
|
172
|
+
# view = only_these_parameters(h,[:view,{:klass => View,:required => true}])
|
|
173
|
+
# or
|
|
174
|
+
#
|
|
175
|
+
# view = parameters(h,[:view,{:klass => View,:required => true}])
|
|
176
|
+
|
|
177
|
+
def parameters(h,*args)
|
|
178
|
+
raise ArgumentError.new("argument to #{self.class}##{current_method} must be of class Hash; you may get this error if you don't call a function with a hash; check the initial call") unless h.is_a? Hash
|
|
179
|
+
ret = []
|
|
180
|
+
args.each do |a|
|
|
181
|
+
name,options = a
|
|
182
|
+
options = options || {}
|
|
183
|
+
unless h[:no_recurse]
|
|
184
|
+
vals = only_these_parameters(
|
|
185
|
+
options.merge!(:no_recurse => true),
|
|
186
|
+
[:instance_that_inherits_from, {:instance_of => Class}],
|
|
187
|
+
[:instance_of, {:instance_of => Class}],
|
|
188
|
+
[:klass_that_inherits_from, {:instance_of => Class}],
|
|
189
|
+
[:klass_of, {:instance_of => Class}],
|
|
190
|
+
[:no_recurse, {:instance_of => TrueClass}],
|
|
191
|
+
[:required, {:instance_of => TrueClass}],
|
|
192
|
+
[:default, {:instance_of => TrueClass}]
|
|
193
|
+
)
|
|
194
|
+
instance_that_inherits_from, instance_of, klass_that_inherits_from, klass_of, no_recurse, required, default = vals
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
if val = h[name]
|
|
198
|
+
if instance_that_inherits_from
|
|
199
|
+
unless val.class.inherits_from? instance_that_inherits_from
|
|
200
|
+
raise ArgumentError.new(
|
|
201
|
+
"argument :#{name} to #{self.class}##{calling_method} must be an instance that inherits from #{instance_that_inherits_from}, #{val.class} does not")
|
|
202
|
+
end
|
|
203
|
+
elsif instance_of
|
|
204
|
+
unless val.class == instance_of
|
|
205
|
+
raise ArgumentError.new(
|
|
206
|
+
"argument :#{name} to #{self.class}##{calling_method} must be an instance of class #{instance_of}, not #{val.class}")
|
|
207
|
+
end
|
|
208
|
+
elsif klass_that_inherits_from
|
|
209
|
+
unless val.inherits_from? klass_that_inherits_from
|
|
210
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must inherits from class #{klass_that_inherits_from}, #{val} does not")
|
|
211
|
+
end
|
|
212
|
+
elsif klass_of
|
|
213
|
+
unless val == klass_to
|
|
214
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} must be of class #{klass_of}, not #{val}")
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
else
|
|
218
|
+
if options[:default]
|
|
219
|
+
val = options[:default]
|
|
220
|
+
elsif options[:required]
|
|
221
|
+
raise ArgumentError.new("argument :#{name} to #{self.class}##{calling_method} is required")
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
ret.push val
|
|
225
|
+
end
|
|
226
|
+
ret
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# insure that only the defined parameters have been passed to a function
|
|
230
|
+
def only_these_parameters(h,*args)
|
|
231
|
+
ret = parameters(h,*args)
|
|
232
|
+
keys = h.keys
|
|
233
|
+
args.each do |a|
|
|
234
|
+
name,options = a
|
|
235
|
+
keys.delete name
|
|
236
|
+
end
|
|
237
|
+
if keys.length > 0
|
|
238
|
+
raise ArgumentError.new("unknown parameters #{keys.inspect} passed to #{self.class}##{calling_method}")
|
|
239
|
+
end
|
|
240
|
+
ret
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def initialize(h={})
|
|
245
|
+
raise ArgumentError.new("argument to #{self.class}##{current_method} must be of class Hash") unless h.is_a? Hash
|
|
246
|
+
h.keys.each do |k|
|
|
247
|
+
self.send("#{k}=",h[k])
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
yield self if block_given?
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
end # module CkuruTools
|
|
255
|
+
|
|
256
|
+
CkuruTools.require_all_libs_relative_to __FILE__
|
|
257
|
+
CkuruTools.require_all_libs_relative_to CkuruTools.libpath
|
|
258
|
+
|
|
259
|
+
#
|
|
260
|
+
# class Foo < ActiveRecord::Base
|
|
261
|
+
# end
|
|
262
|
+
#
|
|
263
|
+
# Foo.inherits_from? ActiveRecord::Base
|
|
264
|
+
# => true
|
|
265
|
+
#
|
|
266
|
+
class Class
|
|
267
|
+
|
|
268
|
+
# show's live decendants of this class
|
|
269
|
+
def decendants
|
|
270
|
+
CkuruTools.class_space.select {|x| x.inherits_from? self and x != self}
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def inherits_from?(klass)
|
|
274
|
+
raise ArgumentError.new("argument must be of type Class") unless klass.is_a? Class
|
|
275
|
+
if klass == self
|
|
276
|
+
true
|
|
277
|
+
elsif self.superclass.is_a? Class
|
|
278
|
+
self.superclass.inherits_from? klass
|
|
279
|
+
else
|
|
280
|
+
false
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
class Array
|
|
286
|
+
def to_comma_separated_string
|
|
287
|
+
ret = ''
|
|
288
|
+
self.each do |elem|
|
|
289
|
+
ret += ',' if ret.length > 0
|
|
290
|
+
ret += elem.to_s
|
|
291
|
+
end
|
|
292
|
+
ret
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
class Object
|
|
297
|
+
# this method allows a an 'initialize' method to define itself as a abstract class; yet still run some code.
|
|
298
|
+
#
|
|
299
|
+
# To use effectively place at the last line of an initialize method like so:
|
|
300
|
+
#
|
|
301
|
+
# class DataSource < ::CkuruTools::HashInitializerClass
|
|
302
|
+
# def initialize(h={})
|
|
303
|
+
# super h
|
|
304
|
+
# this_is_an_abstract_constructor_for AuraVisualize::DataSource
|
|
305
|
+
# end
|
|
306
|
+
# end
|
|
307
|
+
|
|
308
|
+
def this_is_an_abstract_constructor_for(klass)
|
|
309
|
+
unless self.class.superclass.inherits_from? klass # abstract constructor
|
|
310
|
+
str = ''
|
|
311
|
+
self.class.decendants.each do |d|
|
|
312
|
+
str += "#{d}\n"
|
|
313
|
+
end
|
|
314
|
+
raise <<"EOF"
|
|
315
|
+
Do not call method of #{self.class}.#{calling_method} directly, you must instantiate a base class.
|
|
316
|
+
|
|
317
|
+
Maybe you want one of these?:
|
|
318
|
+
|
|
319
|
+
#{str}
|
|
320
|
+
EOF
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# see if this object's class inherits from another class
|
|
325
|
+
def instance_inherits_from?(klass)
|
|
326
|
+
self.class.inherits_from?(klass)
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def _require ; each {|r| require r } ; end
|
|
330
|
+
|
|
331
|
+
def docmd(cmd,dir=nil)
|
|
332
|
+
ret = docmdi(cmd,dir)
|
|
333
|
+
if ret.exitstatus != 0
|
|
334
|
+
raise "cmd #{cmd} exitstatus #{ret.exitstatus} : #{ret}"
|
|
335
|
+
end
|
|
336
|
+
ret
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def docmdi(cmd,dir=nil)
|
|
340
|
+
if dir
|
|
341
|
+
unless Dir.chdir(dir)
|
|
342
|
+
ckebug 0, "failed to cd to #{dir}"
|
|
343
|
+
return nil
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
ret = msg_exec "running #{cmd}" do
|
|
347
|
+
cmd.gsub!(/\\/,"\\\\\\\\\\\\\\\\")
|
|
348
|
+
cmd.gsub!(/\'/,"\\\\'")
|
|
349
|
+
cmd.gsub!(/\"/,"\\\\\\\\\\\"")
|
|
350
|
+
system("bash -c \"#{cmd}\"")
|
|
351
|
+
if $?.exitstatus != 0
|
|
352
|
+
print " failed exit code #{$?.exitstatus} "
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
$?
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
################################################################################
|
|
359
|
+
|
|
360
|
+
def docmd_dir(h={})
|
|
361
|
+
ret = nil
|
|
362
|
+
if h[:dir]
|
|
363
|
+
unless Dir.chdir(h[:dir])
|
|
364
|
+
ckebug 0, "failed to cd to #{h[:dir]}"
|
|
365
|
+
return nil
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
if h[:commands]
|
|
369
|
+
h[:commands].each do |cmd|
|
|
370
|
+
ret = docmd(cmd)
|
|
371
|
+
if ret.exitstatus != 0
|
|
372
|
+
ckebug 0, "giving up"
|
|
373
|
+
return ret
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
ret
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
def emacs_trace
|
|
381
|
+
begin
|
|
382
|
+
yield
|
|
383
|
+
rescue Exception => e
|
|
384
|
+
puts e
|
|
385
|
+
puts "... exception thrown from ..."
|
|
386
|
+
e.backtrace.each do |trace|
|
|
387
|
+
a = trace.split(/:/)
|
|
388
|
+
if a[0].match(/^\//)
|
|
389
|
+
puts "#{a[0]}:#{a[1]}: #{a[2..a.length].join(':')}"
|
|
390
|
+
else
|
|
391
|
+
d = File.dirname(a[0])
|
|
392
|
+
f = File.basename(a[0])
|
|
393
|
+
dir = `cd #{d}; pwd`.chomp
|
|
394
|
+
goodpath = File.join(dir,f)
|
|
395
|
+
|
|
396
|
+
puts "#{goodpath}:#{a[1]}: #{a[2..a.length].join(':')}"
|
|
397
|
+
end
|
|
398
|
+
if $emacs_trace_debugger
|
|
399
|
+
require 'ruby-debug'
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
raise "sorry ...exiting"
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
def ckebug(level,msg)
|
|
407
|
+
CkuruTools::Debug.instance.debug(level,msg)
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def current_method
|
|
411
|
+
caller[0].match(/`(.*?)'/)[1]
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def calling_method
|
|
415
|
+
caller[1] ? caller[1].match(/`(.*?)'/)[1] : ""
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
def calling_method2
|
|
419
|
+
if caller[2]
|
|
420
|
+
if matchdata = caller[2].match(/`(.*?)'/)
|
|
421
|
+
matchdata[1]
|
|
422
|
+
else
|
|
423
|
+
""
|
|
424
|
+
end
|
|
425
|
+
else
|
|
426
|
+
""
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
def calling_method3
|
|
431
|
+
if caller[3]
|
|
432
|
+
if matchdata = caller[3].match(/`(.*?)'/)
|
|
433
|
+
matchdata[2]
|
|
434
|
+
else
|
|
435
|
+
""
|
|
436
|
+
end
|
|
437
|
+
else
|
|
438
|
+
""
|
|
439
|
+
end
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def calling_method_sig
|
|
445
|
+
caller[1] ? caller[1] : ""
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
# def calling_method
|
|
451
|
+
# if caller[2]
|
|
452
|
+
# caller[2].match(/`(.*?)'/)[1]
|
|
453
|
+
# else
|
|
454
|
+
# ""
|
|
455
|
+
# end
|
|
456
|
+
|
|
457
|
+
# end
|
|
458
|
+
|
|
459
|
+
end
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
class Time
|
|
463
|
+
def ckuru_time_string
|
|
464
|
+
strftime("%m/%d/%Y %H:%M:%S")
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
def printmsg(msg,newline=true)
|
|
469
|
+
print "#{Time.new.ckuru_time_string}: #{msg}"
|
|
470
|
+
puts if newline
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
#module MlImportUtils
|
|
475
|
+
def msg_exec(msg)
|
|
476
|
+
printmsg("#{msg} ... ",false)
|
|
477
|
+
t1 = Time.new
|
|
478
|
+
ret = yield
|
|
479
|
+
puts "done (#{Time.new - t1})"
|
|
480
|
+
ret
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
def chatty_exec(level,name)
|
|
484
|
+
ret = yield
|
|
485
|
+
ckebug level, "#{name} is #{ret}"
|
|
486
|
+
ret
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
# EOF
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ckuru-tools
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 13
|
|
5
5
|
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 0
|
|
9
|
-
-
|
|
10
|
-
version: 0.0.
|
|
9
|
+
- 9
|
|
10
|
+
version: 0.0.9
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Bret Weinraub
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2011-
|
|
18
|
+
date: 2011-05-05 00:00:00 Z
|
|
19
19
|
dependencies: []
|
|
20
20
|
|
|
21
21
|
description: The infamous ckuru-tools gem. A miscellaneous grab bag of ruby class extensions, utility classes, etc.
|
|
@@ -32,6 +32,7 @@ files:
|
|
|
32
32
|
- ./lib/debug.rb
|
|
33
33
|
- ./lib/args.rb
|
|
34
34
|
- ./lib/ckuru-tools.rb
|
|
35
|
+
- ./lib/ckuru-tools.rb~
|
|
35
36
|
- ./tasks/notes.rake
|
|
36
37
|
- ./tasks/rdoc.rake
|
|
37
38
|
- ./tasks/bones.rake
|