chingu 0.5.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data.tar.gz.sig +0 -0
  2. data/History.txt +21 -0
  3. data/LICENSE +504 -0
  4. data/Manifest.txt +72 -0
  5. data/README.rdoc +588 -0
  6. data/Rakefile +19 -0
  7. data/benchmarks/README.txt +1 -0
  8. data/benchmarks/benchmark.rb +6 -0
  9. data/benchmarks/benchmark3.rb +23 -0
  10. data/benchmarks/benchmark4.rb +71 -0
  11. data/benchmarks/benchmark5.rb +91 -0
  12. data/benchmarks/benchmark6.rb +23 -0
  13. data/benchmarks/meta_benchmark.rb +67 -0
  14. data/benchmarks/meta_benchmark2.rb +39 -0
  15. data/chingu.gemspec +34 -0
  16. data/examples/example1.rb +37 -0
  17. data/examples/example10.rb +75 -0
  18. data/examples/example11.rb +51 -0
  19. data/examples/example12.rb +67 -0
  20. data/examples/example2.rb +115 -0
  21. data/examples/example3.rb +40 -0
  22. data/examples/example4.rb +175 -0
  23. data/examples/example5.rb +107 -0
  24. data/examples/example6.rb +57 -0
  25. data/examples/example7.rb +133 -0
  26. data/examples/example8.rb +109 -0
  27. data/examples/example9.rb +106 -0
  28. data/examples/media/Parallax-scroll-example-layer-0.png +0 -0
  29. data/examples/media/Parallax-scroll-example-layer-1.png +0 -0
  30. data/examples/media/Parallax-scroll-example-layer-2.png +0 -0
  31. data/examples/media/Parallax-scroll-example-layer-3.png +0 -0
  32. data/examples/media/background1.png +0 -0
  33. data/examples/media/fire_bullet.png +0 -0
  34. data/examples/media/fireball.png +0 -0
  35. data/examples/media/particle.png +0 -0
  36. data/examples/media/ruby.png +0 -0
  37. data/examples/media/spaceship.png +0 -0
  38. data/examples/media/stickfigure.bmp +0 -0
  39. data/examples/media/stickfigure.png +0 -0
  40. data/examples/media/video_games.png +0 -0
  41. data/lib/chingu.rb +32 -0
  42. data/lib/chingu/actor.rb +17 -0
  43. data/lib/chingu/animation.rb +142 -0
  44. data/lib/chingu/assets.rb +64 -0
  45. data/lib/chingu/basic_game_object.rb +132 -0
  46. data/lib/chingu/core_extensions.rb +53 -0
  47. data/lib/chingu/effects.rb +36 -0
  48. data/lib/chingu/fpscounter.rb +62 -0
  49. data/lib/chingu/game_object.rb +127 -0
  50. data/lib/chingu/game_object_list.rb +91 -0
  51. data/lib/chingu/game_state.rb +137 -0
  52. data/lib/chingu/game_state_manager.rb +284 -0
  53. data/lib/chingu/game_states/debug.rb +65 -0
  54. data/lib/chingu/game_states/fade_to.rb +91 -0
  55. data/lib/chingu/game_states/pause.rb +57 -0
  56. data/lib/chingu/gfx_helpers.rb +89 -0
  57. data/lib/chingu/helpers.rb +166 -0
  58. data/lib/chingu/inflector.rb +34 -0
  59. data/lib/chingu/input.rb +100 -0
  60. data/lib/chingu/named_resource.rb +254 -0
  61. data/lib/chingu/parallax.rb +83 -0
  62. data/lib/chingu/particle.rb +21 -0
  63. data/lib/chingu/rect.rb +612 -0
  64. data/lib/chingu/require_all.rb +133 -0
  65. data/lib/chingu/text.rb +56 -0
  66. data/lib/chingu/traits/collision_detection.rb +172 -0
  67. data/lib/chingu/traits/effect.rb +113 -0
  68. data/lib/chingu/traits/input.rb +38 -0
  69. data/lib/chingu/traits/retrofy.rb +53 -0
  70. data/lib/chingu/traits/rotation_center.rb +84 -0
  71. data/lib/chingu/traits/timer.rb +90 -0
  72. data/lib/chingu/traits/velocity.rb +67 -0
  73. data/lib/chingu/window.rb +170 -0
  74. metadata +162 -0
  75. metadata.gz.sig +1 -0
@@ -0,0 +1,89 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+
23
+ module Chingu
24
+ #
25
+ # Various helper-methods to manipulate the screen.
26
+ # All drawings depend on the global variable $window which should be an instance of Gosu::Window or Chingu::Window
27
+ #
28
+ module GFXHelpers
29
+
30
+ #
31
+ # Fills whole window with color 'color'.
32
+ #
33
+ def fill(color)
34
+ $window.draw_quad(0, 0, color,
35
+ $window.width, 0, color,
36
+ $window.width, $window.height, color,
37
+ 0, $window.height, color,
38
+ 0, :default)
39
+ end
40
+
41
+ #
42
+ # Fills a given Rect 'rect' with Color 'color'
43
+ #
44
+ def fill_rect(rect, color)
45
+ rect = Rect.new(rect) # Make sure it's a rect
46
+ $window.draw_quad( rect.x, rect.y, color,
47
+ rect.right, rect.y, color,
48
+ rect.right, rect.bottom, color,
49
+ rect.x, rect.bottom, color,
50
+ 0, :default)
51
+ end
52
+
53
+ #
54
+ # Fills window or a given rect with a gradient between two colors.
55
+ #
56
+ # :from - Start with this color
57
+ # :to - End with this color
58
+ # :rect - Only fill rectangle :rect with the gradient, either a Rect-instance or [x,y,width,height] Array.
59
+ # :orientation - Either :vertical (top to bottom) or :horizontal (left to right)
60
+ #
61
+ def fill_gradient(options)
62
+ default_options = { :from => Gosu::Color.new(255,0,0,0),
63
+ :to => Gosu::Color.new(255,255,255,255),
64
+ :thickness => 10,
65
+ :orientation => :vertical,
66
+ :rect => Rect.new([0, 0, $window.width, $window.height]),
67
+ :zorder => 0,
68
+ :mode => :default
69
+ }
70
+ options = default_options.merge(options)
71
+ rect = Rect.new(options[:rect])
72
+ if options[:orientation] == :vertical
73
+ $window.draw_quad( rect.x, rect.y, options[:from],
74
+ rect.right, rect.y, options[:from],
75
+ rect.right, rect.bottom, options[:to],
76
+ rect.x, rect.bottom, options[:to],
77
+ options[:zorder], options[:mode]
78
+ )
79
+ else
80
+ $window.draw_quad( rect.x, rect.y, options[:from],
81
+ rect.x, rect.bottom, options[:from],
82
+ rect.right, rect.bottom, options[:to],
83
+ rect.right, rect.y, options[:to],
84
+ options[:zorder], options[:mode]
85
+ )
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,166 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+
23
+ module Chingu
24
+
25
+ module InputClient
26
+ def input=(input_map)
27
+ @input = input_map
28
+ @parent.add_input_client(self) if @parent
29
+ end
30
+
31
+ def input
32
+ @input
33
+ end
34
+ end
35
+
36
+ module InputDispatcher
37
+ attr_reader :input_clients
38
+
39
+ def add_input_client(object)
40
+ @input_clients << object
41
+ end
42
+
43
+ def remove_input_client(object)
44
+ @input_clients.delete(object)
45
+ end
46
+
47
+ def dispatch_button_down(id, object)
48
+ return if(object.nil? || object.input.nil?)
49
+
50
+ object.input.each do |symbol, action|
51
+ if Input::SYMBOL_TO_CONSTANT[symbol] == id
52
+ dispatch_action(action, object)
53
+ end
54
+ end
55
+ end
56
+
57
+ def dispatch_button_up(id, object)
58
+ return if object.nil? || object.input.nil?
59
+
60
+ object.input.each do |symbol, action|
61
+ if symbol.to_s.include? "released_"
62
+ symbol = symbol.to_s.sub("released_", "").to_sym
63
+ if Input::SYMBOL_TO_CONSTANT[symbol] == id
64
+ dispatch_action(action, object)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ #
71
+ # Dispatches input-mapper for any given object
72
+ #
73
+ def dispatch_input_for(object, prefix = "holding_")
74
+ return if object.nil? || object.input.nil?
75
+
76
+ object.input.each do |symbol, action|
77
+ if symbol.to_s.include? prefix
78
+ symbol = symbol.to_s.sub(prefix, "").to_sym
79
+ if $window.button_down?(Input::SYMBOL_TO_CONSTANT[symbol])
80
+ dispatch_action(action, object)
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ #
87
+ # For a given object, dispatch "action".
88
+ # An action can be:
89
+ #
90
+ # * Symbol (:p, :space), translates into a method-call
91
+ # * Proc/Lambda, call() it
92
+ # * GameState-instance, push it on top of stack
93
+ # * GameState-inherited class, create a new instance, cache it and push it on top of stack
94
+ #
95
+ def dispatch_action(action, object)
96
+ # puts "Dispatch Action: #{action} - Objects class: #{object.class.to_s}"
97
+ if action.is_a? Symbol
98
+ object.send(action)
99
+ elsif action.is_a? Proc
100
+ action.call
101
+ elsif action.is_a? Chingu::GameState
102
+ push_game_state(action)
103
+ elsif action.superclass == Chingu::GameState
104
+ push_game_state(action)
105
+ end
106
+ end
107
+ end
108
+
109
+ #
110
+ # push_game_state accepts either a class inherited from GameState or an object-instance from such a class.
111
+ #
112
+ # It will make call new() on a class, and just push an object.
113
+ #
114
+ module GameStateHelpers
115
+ def push_game_state(state, options = {})
116
+ $window.game_state_manager.push_game_state(state, options)
117
+ end
118
+
119
+ def pop_game_state(options = {})
120
+ $window.game_state_manager.pop_game_state(options)
121
+ end
122
+
123
+ def switch_game_state(state, options = {})
124
+ $window.game_state_manager.switch_game_state(state, options)
125
+ end
126
+
127
+ def transitional_game_state(state, options = {})
128
+ $window.game_state_manager.transitional_game_state(state, options)
129
+ end
130
+
131
+ def current_game_state
132
+ $window.game_state_manager.current_game_state
133
+ end
134
+
135
+ def previous_game_state
136
+ $window.game_state_manager.previous_game_state
137
+ end
138
+
139
+ def clear_game_states
140
+ $window.game_state_manager.clear_game_states
141
+ end
142
+ end
143
+
144
+ module GameObjectHelpers
145
+
146
+ def add_game_object(object)
147
+ @game_objects.add_game_object(object)
148
+ end
149
+
150
+ def remove_game_object(object)
151
+ @game_objects.remove_game_object(object)
152
+ end
153
+
154
+ def game_objects
155
+ @game_objects
156
+ end
157
+
158
+ #
159
+ # Fetch game objects of a certain type/class
160
+ #
161
+ def game_objects_of_class(klass)
162
+ @game_objects.select { |game_object| game_object.is_a? klass }
163
+ end
164
+ end
165
+
166
+ end
@@ -0,0 +1,34 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+
23
+ module Chingu
24
+ module Inflector
25
+ def Inflector.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
26
+ if first_letter_in_uppercase
27
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
28
+ else
29
+ lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,100 @@
1
+ module Chingu
2
+ module Input
3
+ include Gosu::Button
4
+
5
+ #
6
+ # Ruby symbols describing http://www.libgosu.org/rdoc/classes/Gosu.html
7
+ #
8
+ CONSTANT_TO_SYMBOL = {
9
+ Kb0 => [:zero],
10
+ Kb1 => [:one],
11
+ Kb2 => [:two],
12
+ Kb3 => [:three],
13
+ Kb4 => [:four],
14
+ Kb5 => [:five],
15
+ Kb6 => [:six],
16
+ Kb7 => [:seven],
17
+ Kb8 => [:eight],
18
+ Kb9 => [:nine],
19
+
20
+ KbBackspace => [:backspace],
21
+ KbDelete => [:delete, :del],
22
+ KbDown => [:down_arrow, :down],
23
+ KbEnd => [:end],
24
+ KbEnter => [:enter],
25
+ KbEscape => [:escape, :esc],
26
+
27
+ KbHome => [:home],
28
+ KbInsert => [:insert, :ins],
29
+ KbLeft => [:left_arrow, :left],
30
+ KbLeftAlt => [:left_alt, :lalt],
31
+ KbLeftControl => [:left_control, :left_ctrl, :lctrl],
32
+ KbLeftShift => [:left_shift, :lshift],
33
+
34
+
35
+ KbNumpadAdd => [:"+", :add],
36
+ KbNumpadDivide => [:"/", :divide],
37
+ KbNumpadMultiply => [:"*", :multiply],
38
+ KbNumpadSubtract => [:"-", :subtract],
39
+ KbPageDown => [:page_down],
40
+ KbPageUp => [:page_up],
41
+ # KbPause => [:pause],
42
+ KbReturn => [:return],
43
+ KbRight => [:right_arrow, :right],
44
+ KbRightAlt => [:right_alt, :ralt],
45
+ KbRightControl => [:right_control, :right_ctrl, :rctrl],
46
+ KbRightShift => [:right_shift, :rshift],
47
+ KbSpace => [:" ", :space],
48
+ KbTab => [:tabulator, :tab],
49
+ KbUp => [:up_arrow, :up],
50
+
51
+ MsLeft => [:left_mouse_button, :mouse_left],
52
+ MsMiddle => [:middle_mouse_button, :mouse_middle],
53
+ MsRight => [:right_mouse_button, :mouse_right],
54
+ MsWheelDown => [:mouse_wheel_down, :wheel_down],
55
+ MsWheelUp => [:mouse_wheel_up, :wheel_up],
56
+
57
+ GpDown => [:gamepad_down, :gp_down, :pad_down],
58
+ GpLeft => [:gamepad_left, :gp_left, :pad_left],
59
+ GpRight => [:gamepad_right, :gp_right, :pad_right],
60
+ GpUp => [:gamepad_up, :gp_up, :pad_up]
61
+ }
62
+
63
+ # Letters, A-Z
64
+ ("A".."Z").each do |letter|
65
+ CONSTANT_TO_SYMBOL[eval("Kb#{letter}")] = [letter.downcase.to_sym]
66
+ end
67
+
68
+ # Numbers, 0-9
69
+ (0..9).each do |number|
70
+ CONSTANT_TO_SYMBOL[eval("Kb#{number.to_s}")] = [number.to_s.to_sym]
71
+ end
72
+
73
+ # Numpad-numbers, 0-9
74
+ (0..9).each do |number|
75
+ CONSTANT_TO_SYMBOL[eval("KbNumpad#{number.to_s}")] = ["numpad_#{number.to_s}".to_sym]
76
+ end
77
+
78
+ #F-keys, F1-F12
79
+ (1..12).each do |number|
80
+ CONSTANT_TO_SYMBOL[eval("KbF#{number.to_s}")] = ["f#{number.to_s}".to_sym]
81
+ end
82
+
83
+ # Gamepad-buttons 0-15
84
+ (0..15).each do |number|
85
+ CONSTANT_TO_SYMBOL[eval("GpButton#{number.to_s}")] = ["gamepad_button_#{number.to_s}"]
86
+ end
87
+
88
+ #
89
+ # Reverse CONSTANT_TO_SYMBOL -> SYMBOL_TO_CONSTNT
90
+ # like: SYMBOL_TO_CONSTANT = CONSTANT_TO_SYMBOL.invert.dup
91
+ #
92
+ SYMBOL_TO_CONSTANT = Hash.new
93
+ CONSTANT_TO_SYMBOL.each_pair do |constant, symbols|
94
+ symbols.each do |symbol|
95
+ SYMBOL_TO_CONSTANT[symbol] = constant
96
+ end
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,254 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2004-2008 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+
21
+ module Chingu
22
+
23
+ # NamedResource is a mix-in module to implement a globally-available
24
+ # resource table, a @name variable and accessors, and a system for
25
+ # automatically loading resources when they are first needed.
26
+ #
27
+ # This module is used for Rubygame::Music, Rubygame::Sound, and
28
+ # Rubygame::Surface. You can use it in your own classes this way:
29
+ #
30
+ # 1. Do 'include Rubygame::NamedResource' in your class definition.
31
+ #
32
+ # 2. Set MyClass.autoload_dirs to an Array of directories to look
33
+ # for files when autoloading. Tip: use File.join to create
34
+ # paths that work on any operating system.
35
+ #
36
+ # 3. Define #autoload to implement the behavior for your class,
37
+ # or leave it as the default if you don't need autoloading.
38
+ # See the docs for #autoload for more information.
39
+ #
40
+ # Here's an example of how you could use this for a class which
41
+ # loads maps from a file:
42
+ #
43
+ # class Map
44
+ # include Rubygame::NamedResource
45
+ #
46
+ # Map.autoload_dirs = [ File.join("maps","world_1"),
47
+ # File.join("maps","custom") ]
48
+ #
49
+ # def autoload( name )
50
+ # # Searches autoload_dirs for the file
51
+ # path = find_file( name )
52
+ #
53
+ # if( path )
54
+ # return load_map( path )
55
+ # else
56
+ # return nil
57
+ # end
58
+ # end
59
+ #
60
+ # def load_map( path )
61
+ # # Your code to do the real loading, then return
62
+ # # the created instance of Map class.
63
+ # # ...
64
+ # return map_instance
65
+ # end
66
+ # end
67
+ #
68
+ # Here's an example of how you could then use the Map class:
69
+ #
70
+ # map = Map["level_1.map"]
71
+ #
72
+ # if( map )
73
+ # start_playing( map )
74
+ # else
75
+ # raise "Oops! The map file for Level 1 doesn't exist!"
76
+ # end
77
+ #
78
+ module NamedResource
79
+
80
+
81
+ # Adds class methods when the NamedResource module is included
82
+ # in a class. (Here, we are assuming that the NamedResource
83
+ # module was included in a class called MyClass.)
84
+ module NamedResourceClassMethods
85
+
86
+ # An Array of paths to check for files. See #find_file.
87
+ attr_accessor :autoload_dirs
88
+
89
+
90
+ # call-seq:
91
+ # MyClass[ name ] -> instance or nil
92
+ #
93
+ # Retrieves an instance of the class from a per-class resource
94
+ # table (Hash).
95
+ #
96
+ # If no object has been saved under the given name, invoke
97
+ # #autoload to try to load a new instance, store it in the
98
+ # Hash table under this name, and sets the instance's @name
99
+ # to this name.
100
+ #
101
+ def []( name )
102
+ result = @resources[name]
103
+
104
+ if result.nil?
105
+ result = autoload(name)
106
+ if result
107
+ self[name] = result
108
+ result.name = name
109
+ end
110
+ end
111
+
112
+ return result
113
+ end
114
+
115
+
116
+ # call-seq:
117
+ # MyClass[ name ] = instance
118
+ #
119
+ # Stores an instance of the class in a per-class resource table
120
+ # (Hash) for future access. If another object is already stored
121
+ # with this name, the old record is lost.
122
+ #
123
+ # May raise: TypeError, if you try to store anything
124
+ # that is not kind of this class.
125
+ #
126
+ def []=( name, value )
127
+ ##if( value.kind_of? self )
128
+ @resources[name] = value
129
+ ##else
130
+ ## raise TypeError, "#{self}#[]= can only store instances of #{self}"
131
+ ##end
132
+ end
133
+
134
+ # call-seq:
135
+ # MyClass.autoload( name ) -> instance or nil
136
+ #
137
+ # This method is invoked when a non-existing resource is
138
+ # accessed with #[]. By default, this method simply returns
139
+ # nil, effectively disabling autoloading.
140
+ #
141
+ # You should override this method in your class to provide
142
+ # class-specific loading behavior, or leave it as the default if
143
+ # you don't need autoloading. Your method should return either
144
+ # an instance of the class, or nil.
145
+ #
146
+ # NOTE: The #find_file method is useful for getting the full
147
+ # path to a file which matches the name. That's what it's there
148
+ # for, so you should use it!
149
+ #
150
+ def autoload( name )
151
+ nil
152
+ end
153
+
154
+
155
+ # call-seq:
156
+ # MyClass.basename( path ) -> filename
157
+ #
158
+ # Returns the basename for the path (i.e. the
159
+ # filename without the directory). Same as
160
+ # File.basename
161
+ #
162
+ def basename( path )
163
+ File.basename( path )
164
+ end
165
+
166
+
167
+ # call-seq:
168
+ # MyClass.exist?( path ) -> true or false
169
+ #
170
+ # True if the given path points to a file
171
+ # that exists, otherwise false. Same as
172
+ # File.exist?
173
+ #
174
+ def exist?( path )
175
+ File.exist?(path)
176
+ end
177
+
178
+
179
+ # call-seq:
180
+ # MyClass.find_file( filename ) -> path or nil
181
+ #
182
+ # Checks every directory in @autoload_dirs for
183
+ # a file with the given name, and returns the
184
+ # path (directory and name) for the first match.
185
+ #
186
+ # If no directories have a file with that name,
187
+ # return nil.
188
+ #
189
+ def find_file( filename )
190
+ dir = @autoload_dirs.find { |dir|
191
+ exist?( File.join(dir,filename) )
192
+ }
193
+
194
+ if dir
195
+ return File.join(dir,filename)
196
+ else
197
+ return nil
198
+ end
199
+ end
200
+
201
+ end
202
+
203
+
204
+ # Sets up the class when this module is included.
205
+ # Adds the class methods and defines class instance
206
+ # variables.
207
+ def self.included( object ) # :nodoc:
208
+
209
+ class << object
210
+ include NamedResourceClassMethods
211
+ end
212
+
213
+ object.instance_eval do
214
+ @resources = Hash.new
215
+ @autoload_dirs = []
216
+ end
217
+
218
+ end
219
+
220
+
221
+ # Returns the instance's @name. See also #name=.
222
+ def name
223
+ @name
224
+ end
225
+
226
+ #
227
+ # Sets the instance's @name to the given String, or nil to
228
+ # unset the name. See also #name.
229
+ #
230
+ # NOTE: This does not automatically store the instance in the
231
+ # class resource table by name. Use the #[]= class method to do
232
+ # that.
233
+ #
234
+ # The string is dup'ed and frozen before being stored.
235
+ #
236
+ # May raise: TypeError, if new_name is not a String or nil.
237
+ #
238
+ def name=( new_name )
239
+ if new_name.nil?
240
+ return @name = nil
241
+ end
242
+
243
+ unless new_name.kind_of? String
244
+ raise TypeError, "name must be a String (got #{new_name.class})"
245
+ end
246
+
247
+ @name = new_name.dup
248
+ @name.freeze
249
+ end
250
+
251
+
252
+ end
253
+
254
+ end