ckuru-tools 0.0.6 → 0.0.7

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.
Files changed (2) hide show
  1. data/lib/ckuru-tools.rb +228 -0
  2. metadata +16 -18
data/lib/ckuru-tools.rb CHANGED
@@ -4,8 +4,105 @@
4
4
  # Used to prevent the class/module from being loaded more than once
5
5
  unless defined? CkuruTools
6
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
+
7
45
  module CkuruTools
8
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
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
+
9
106
  # :stopdoc:
10
107
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
11
108
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
@@ -63,6 +160,87 @@ unless defined? CkuruTools
63
160
  #
64
161
 
65
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") 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
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
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
+
66
244
  def initialize(h={})
67
245
  raise ArgumentError.new("argument to #{self.class}##{current_method} must be of class Hash") unless h.is_a? Hash
68
246
  h.keys.each do |k|
@@ -86,6 +264,12 @@ unless defined? CkuruTools
86
264
  # => true
87
265
  #
88
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
+
89
273
  def inherits_from?(klass)
90
274
  raise ArgumentError.new("argument must be of type Class") unless klass.is_a? Class
91
275
  if klass == self
@@ -110,6 +294,37 @@ unless defined? CkuruTools
110
294
  end
111
295
 
112
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
113
328
 
114
329
  def _require ; each {|r| require r } ; end
115
330
 
@@ -211,6 +426,19 @@ unless defined? CkuruTools
211
426
  end
212
427
  end
213
428
 
429
+ def calling_method3
430
+ if caller[3]
431
+ if matchdata = caller[3].match(/`(.*?)'/)
432
+ matchdata[2]
433
+ else
434
+ ""
435
+ end
436
+ else
437
+ ""
438
+ end
439
+ end
440
+
441
+
214
442
 
215
443
  def calling_method_sig
216
444
  caller[1] ? caller[1] : ""
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: 19
5
- prerelease: false
4
+ hash: 17
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bret Weinraub
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-12 00:00:00 +00:00
19
- default_executable:
18
+ date: 2011-04-27 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description: The infamous ckuru-tools gem. A miscellaneous grab bag of ruby class extensions, utility classes, etc.
@@ -31,25 +30,24 @@ files:
31
30
  - ./spec/ckuru-tools_spec.rb
32
31
  - ./spec/spec_helper.rb
33
32
  - ./lib/debug.rb
34
- - ./lib/ckuru-tools.rb
35
33
  - ./lib/args.rb
36
- - ./tasks/ann.rake
34
+ - ./lib/ckuru-tools.rb
35
+ - ./tasks/notes.rake
36
+ - ./tasks/rdoc.rake
37
+ - ./tasks/bones.rake
38
+ - ./tasks/git.rake
37
39
  - ./tasks/gem.rake
38
- - ./tasks/rubyforge.rake
40
+ - ./tasks/svn.rake
41
+ - ./tasks/ann.rake
39
42
  - ./tasks/post_load.rake
40
- - ./tasks/test.rake
41
- - ./tasks/rdoc.rake
42
- - ./tasks/manifest.rake
43
+ - ./tasks/rubyforge.rake
43
44
  - ./tasks/setup.rb
44
- - ./tasks/svn.rake
45
45
  - ./tasks/spec.rake
46
- - ./tasks/git.rake
47
- - ./tasks/notes.rake
48
- - ./tasks/bones.rake
46
+ - ./tasks/manifest.rake
47
+ - ./tasks/test.rake
49
48
  - ./bin/ckuru-tools
50
49
  - ./bin/generate_as_controller
51
50
  - ./bin/ckuru-arg-tester
52
- has_rdoc: true
53
51
  homepage: http://www.aura-software.com
54
52
  licenses: []
55
53
 
@@ -79,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
77
  requirements: []
80
78
 
81
79
  rubyforge_project:
82
- rubygems_version: 1.3.7
80
+ rubygems_version: 1.7.2
83
81
  signing_key:
84
82
  specification_version: 3
85
83
  summary: The infamous ckuru-tools gem. A miscellaneous grab bag of ruby class extensions, utility classes, etc.