hotcocoa 0.5.1 → 0.6.0pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. data/.yardopts +14 -0
  2. data/History.txt +21 -0
  3. data/README.markdown +84 -0
  4. data/Rakefile +47 -0
  5. data/bin/hotcocoa +20 -10
  6. data/docs/Contributors.markdown +25 -0
  7. data/docs/Mappings.markdown +352 -0
  8. data/docs/MappingsExplained.markdown +22 -0
  9. data/docs/Overview.markdown +58 -0
  10. data/docs/Resources.markdown +25 -0
  11. data/docs/Tutorial.markdown +3 -0
  12. data/lib/hotcocoa/application/builder.rb +237 -0
  13. data/lib/hotcocoa/application/specification.rb +373 -0
  14. data/lib/hotcocoa/application_builder.rb +338 -242
  15. data/lib/hotcocoa/attributed_string_helpers.rb +128 -0
  16. data/lib/hotcocoa/behaviors.rb +39 -6
  17. data/lib/hotcocoa/core_extensions/kernel.rb +67 -0
  18. data/lib/hotcocoa/core_extensions/nsarray.rb +15 -0
  19. data/lib/hotcocoa/core_extensions/nsmutable_attributed_string.rb +74 -0
  20. data/lib/hotcocoa/core_extensions/nsstring.rb +36 -0
  21. data/lib/hotcocoa/core_extensions/nsurl.rb +6 -0
  22. data/lib/hotcocoa/core_extensions/numeric.rb +9 -0
  23. data/lib/hotcocoa/core_extensions/object.rb +36 -0
  24. data/lib/hotcocoa/core_extensions/range.rb +30 -0
  25. data/lib/hotcocoa/core_extensions.rb +8 -0
  26. data/lib/hotcocoa/data_sources/combo_box_data_source.rb +38 -38
  27. data/lib/hotcocoa/data_sources/outline_view_data_source.rb +33 -0
  28. data/lib/hotcocoa/data_sources/table_data_source.rb +12 -13
  29. data/lib/hotcocoa/data_sources.rb +3 -0
  30. data/lib/hotcocoa/delegate_builder.rb +114 -79
  31. data/lib/hotcocoa/graphics/canvas.rb +211 -214
  32. data/lib/hotcocoa/graphics/color.rb +89 -91
  33. data/lib/hotcocoa/graphics/elements/particle.rb +16 -19
  34. data/lib/hotcocoa/graphics/elements/rope.rb +12 -14
  35. data/lib/hotcocoa/graphics/elements/sandpainter.rb +11 -13
  36. data/lib/hotcocoa/graphics/gradient.rb +13 -14
  37. data/lib/hotcocoa/graphics/image.rb +110 -83
  38. data/lib/hotcocoa/graphics/path.rb +44 -50
  39. data/lib/hotcocoa/graphics/pdf.rb +17 -19
  40. data/lib/hotcocoa/graphics.rb +21 -20
  41. data/lib/hotcocoa/kvo_accessors.rb +10 -11
  42. data/lib/hotcocoa/layout_view.rb +394 -379
  43. data/lib/hotcocoa/mapper.rb +346 -207
  44. data/lib/hotcocoa/mapping_methods.rb +107 -37
  45. data/lib/hotcocoa/mappings/appkit/alert.rb +25 -0
  46. data/lib/hotcocoa/mappings/appkit/application.rb +112 -0
  47. data/lib/hotcocoa/mappings/{array_controller.rb → appkit/array_controller.rb} +33 -35
  48. data/lib/hotcocoa/mappings/appkit/box.rb +37 -0
  49. data/lib/hotcocoa/mappings/appkit/button.rb +88 -0
  50. data/lib/hotcocoa/mappings/appkit/collection_view.rb +42 -0
  51. data/lib/hotcocoa/mappings/appkit/color.rb +40 -0
  52. data/lib/hotcocoa/mappings/appkit/column.rb +19 -0
  53. data/lib/hotcocoa/mappings/appkit/combo_box.rb +22 -0
  54. data/lib/hotcocoa/mappings/appkit/control.rb +31 -0
  55. data/lib/hotcocoa/mappings/appkit/font.rb +58 -0
  56. data/lib/hotcocoa/mappings/appkit/gradient.rb +19 -0
  57. data/lib/hotcocoa/mappings/appkit/image.rb +15 -0
  58. data/lib/hotcocoa/mappings/appkit/image_view.rb +41 -0
  59. data/lib/hotcocoa/mappings/appkit/label.rb +23 -0
  60. data/lib/hotcocoa/mappings/appkit/layout_view.rb +9 -0
  61. data/lib/hotcocoa/mappings/appkit/line.rb +16 -0
  62. data/lib/hotcocoa/mappings/appkit/menu.rb +78 -0
  63. data/lib/hotcocoa/mappings/appkit/menu_item.rb +47 -0
  64. data/lib/hotcocoa/mappings/appkit/outline_view.rb +62 -0
  65. data/lib/hotcocoa/mappings/appkit/popup.rb +106 -0
  66. data/lib/hotcocoa/mappings/appkit/progress_indicator.rb +61 -0
  67. data/lib/hotcocoa/mappings/appkit/scroll_view.rb +28 -0
  68. data/lib/hotcocoa/mappings/appkit/search_field.rb +9 -0
  69. data/lib/hotcocoa/mappings/appkit/secure_text_field.rb +15 -0
  70. data/lib/hotcocoa/mappings/appkit/segmented_control.rb +103 -0
  71. data/lib/hotcocoa/mappings/appkit/slider.rb +23 -0
  72. data/lib/hotcocoa/mappings/appkit/sound.rb +9 -0
  73. data/lib/hotcocoa/mappings/appkit/speech_synthesizer.rb +22 -0
  74. data/lib/hotcocoa/mappings/appkit/split_view.rb +19 -0
  75. data/lib/hotcocoa/mappings/appkit/status_bar.rb +7 -0
  76. data/lib/hotcocoa/mappings/appkit/status_item.rb +9 -0
  77. data/lib/hotcocoa/mappings/appkit/table_view.rb +108 -0
  78. data/lib/hotcocoa/mappings/appkit/text_field.rb +36 -0
  79. data/lib/hotcocoa/mappings/appkit/text_view.rb +13 -0
  80. data/lib/hotcocoa/mappings/appkit/toolbar.rb +99 -0
  81. data/lib/hotcocoa/mappings/appkit/toolbar_item.rb +36 -0
  82. data/lib/hotcocoa/mappings/appkit/tracking_area.rb +24 -0
  83. data/lib/hotcocoa/mappings/appkit/view.rb +65 -0
  84. data/lib/hotcocoa/mappings/appkit/window.rb +124 -0
  85. data/lib/hotcocoa/mappings/foundation/net_service.rb +65 -0
  86. data/lib/hotcocoa/mappings/foundation/net_service_browser.rb +27 -0
  87. data/lib/hotcocoa/mappings/foundation/notification.rb +24 -0
  88. data/lib/hotcocoa/mappings/foundation/sort_descriptor.rb +17 -0
  89. data/lib/hotcocoa/mappings/foundation/timer.rb +38 -0
  90. data/lib/hotcocoa/mappings/foundation/user_defaults.rb +43 -0
  91. data/lib/hotcocoa/mappings/foundation/xml_parser.rb +44 -0
  92. data/lib/hotcocoa/mappings/qtkit/movie.rb +15 -0
  93. data/lib/hotcocoa/mappings/qtkit/movie_view.rb +28 -0
  94. data/lib/hotcocoa/mappings/webkit/web_view.rb +20 -0
  95. data/lib/hotcocoa/mappings.rb +57 -111
  96. data/lib/hotcocoa/mvc.rb +90 -81
  97. data/lib/hotcocoa/notification_listener.rb +135 -58
  98. data/lib/hotcocoa/standard_rake_tasks.rb +17 -9
  99. data/lib/hotcocoa/target_action_convenience.rb +39 -0
  100. data/lib/hotcocoa/template.rb +90 -21
  101. data/lib/hotcocoa/version.rb +3 -0
  102. data/lib/hotcocoa.rb +12 -14
  103. data/template/Rakefile +31 -3
  104. data/template/__APPLICATION_NAME__.appspec +8 -0
  105. data/template/lib/application.rb +14 -17
  106. data/template/lib/menu.rb +11 -11
  107. data/test/application/test_builder.rb +28 -0
  108. data/test/application/test_specification.rb +280 -0
  109. data/test/core_extensions/test_kernel.rb +66 -0
  110. data/test/core_extensions/test_nsarray.rb +9 -0
  111. data/test/core_extensions/test_nsmutable_attributed_string.rb +86 -0
  112. data/test/core_extensions/test_nsstring.rb +20 -0
  113. data/test/core_extensions/test_nsurl.rb +9 -0
  114. data/test/core_extensions/test_numeric.rb +10 -0
  115. data/test/core_extensions/test_object.rb +37 -0
  116. data/test/core_extensions/test_range.rb +28 -0
  117. data/test/helper.rb +30 -0
  118. data/test/mappings/test_bonjour.rb +79 -0
  119. data/test/mappings/test_color.rb +16 -0
  120. data/test/mappings/test_font.rb +21 -0
  121. data/test/mappings/test_tracking_area.rb +18 -0
  122. data/test/test_application_builder.rb +95 -0
  123. data/test/test_attributed_string_helpers.rb +63 -0
  124. data/test/test_behaviours.rb +26 -0
  125. data/test/test_bin.rb +75 -0
  126. data/test/test_mapper.rb +100 -0
  127. data/test/test_mapping_methods.rb +67 -0
  128. data/test/test_mappings.rb +68 -0
  129. data/test/test_notification_listener.rb +19 -0
  130. data/test/test_template.rb +65 -0
  131. metadata +217 -90
  132. data/lib/hotcocoa/attributed_string.rb +0 -143
  133. data/lib/hotcocoa/kernel_ext.rb +0 -14
  134. data/lib/hotcocoa/mappings/alert.rb +0 -25
  135. data/lib/hotcocoa/mappings/application.rb +0 -112
  136. data/lib/hotcocoa/mappings/box.rb +0 -39
  137. data/lib/hotcocoa/mappings/button.rb +0 -92
  138. data/lib/hotcocoa/mappings/collection_view.rb +0 -44
  139. data/lib/hotcocoa/mappings/color.rb +0 -28
  140. data/lib/hotcocoa/mappings/column.rb +0 -21
  141. data/lib/hotcocoa/mappings/combo_box.rb +0 -24
  142. data/lib/hotcocoa/mappings/control.rb +0 -33
  143. data/lib/hotcocoa/mappings/font.rb +0 -44
  144. data/lib/hotcocoa/mappings/gradient.rb +0 -15
  145. data/lib/hotcocoa/mappings/image.rb +0 -15
  146. data/lib/hotcocoa/mappings/image_view.rb +0 -43
  147. data/lib/hotcocoa/mappings/label.rb +0 -25
  148. data/lib/hotcocoa/mappings/layout_view.rb +0 -9
  149. data/lib/hotcocoa/mappings/menu.rb +0 -71
  150. data/lib/hotcocoa/mappings/menu_item.rb +0 -47
  151. data/lib/hotcocoa/mappings/movie.rb +0 -13
  152. data/lib/hotcocoa/mappings/movie_view.rb +0 -27
  153. data/lib/hotcocoa/mappings/notification.rb +0 -17
  154. data/lib/hotcocoa/mappings/popup.rb +0 -110
  155. data/lib/hotcocoa/mappings/progress_indicator.rb +0 -68
  156. data/lib/hotcocoa/mappings/scroll_view.rb +0 -29
  157. data/lib/hotcocoa/mappings/search_field.rb +0 -9
  158. data/lib/hotcocoa/mappings/secure_text_field.rb +0 -17
  159. data/lib/hotcocoa/mappings/segmented_control.rb +0 -97
  160. data/lib/hotcocoa/mappings/slider.rb +0 -25
  161. data/lib/hotcocoa/mappings/sort_descriptor.rb +0 -13
  162. data/lib/hotcocoa/mappings/sound.rb +0 -9
  163. data/lib/hotcocoa/mappings/speech_synthesizer.rb +0 -25
  164. data/lib/hotcocoa/mappings/split_view.rb +0 -21
  165. data/lib/hotcocoa/mappings/status_bar.rb +0 -7
  166. data/lib/hotcocoa/mappings/status_item.rb +0 -9
  167. data/lib/hotcocoa/mappings/table_view.rb +0 -110
  168. data/lib/hotcocoa/mappings/text_field.rb +0 -41
  169. data/lib/hotcocoa/mappings/text_view.rb +0 -13
  170. data/lib/hotcocoa/mappings/timer.rb +0 -25
  171. data/lib/hotcocoa/mappings/toolbar.rb +0 -100
  172. data/lib/hotcocoa/mappings/toolbar_item.rb +0 -36
  173. data/lib/hotcocoa/mappings/view.rb +0 -67
  174. data/lib/hotcocoa/mappings/web_view.rb +0 -22
  175. data/lib/hotcocoa/mappings/window.rb +0 -118
  176. data/lib/hotcocoa/mappings/xml_parser.rb +0 -41
  177. data/lib/hotcocoa/object_ext.rb +0 -22
  178. data/lib/hotcocoa/plist.rb +0 -45
  179. data/template/config/build.yml +0 -8
  180. data/test/test_helper.rb +0 -3
  181. data/test/test_hotcocoa.rb +0 -11
data/lib/hotcocoa/mvc.rb CHANGED
@@ -1,122 +1,132 @@
1
1
  require 'hotcocoa'
2
2
 
3
3
  class HotCocoaApplication
4
- attr_accessor :shared_application, :application_controller, :controllers
5
-
6
4
  include HotCocoa
7
-
8
- def self.instance=(instance)
9
- @instance = instance
10
- end
11
-
12
- def self.instance
13
- @instance
5
+
6
+ # @return [NSApplication]
7
+ attr_accessor :shared_application
8
+
9
+ # @return []
10
+ attr_accessor :application_controller
11
+
12
+ # @return []
13
+ attr_accessor :controllers
14
+
15
+ class << self
16
+ ##
17
+ # The singleton instance of the application.
18
+ #
19
+ # @return [HotCocoaApplication]
20
+ attr_accessor :instance
14
21
  end
15
-
16
- def initialize(application_file)
22
+
23
+ def initialize application_file
17
24
  HotCocoaApplication.instance = self
25
+
18
26
  @controllers = {}
19
27
  load_controllers_and_views(directory_of(application_file))
28
+
20
29
  @shared_application = application(ApplicationView.options[:application])
21
30
  @shared_application.load_application_menu
22
31
  @application_controller = controller(:application_controller)
32
+
23
33
  shared_application.delegate_to(application_controller)
24
34
  end
25
-
35
+
26
36
  def start
27
37
  @shared_application.run
28
38
  end
29
-
30
- def controller(controller_name)
39
+
40
+ def controller controller_name
31
41
  controller_name_string = controller_name.to_s
32
- controller_class = Object.const_get(controller_name_string !~ /_/ && controller_name_string =~ /[A-Z]+.*/ ? controller_name_string : controller_name_string.split('_').map{|e| e.capitalize}.join)
42
+
43
+ controller_class = if Object.const_get(controller_name_string !~ /_/ && controller_name_string =~ /[A-Z]+.*/)
44
+ controller_name_string
45
+ else
46
+ controller_name_string.split('_').map { |e| e.capitalize }.join
47
+ end
48
+
33
49
  @controllers[controller_name] || create_controller_instance(controller_name, controller_class)
34
50
  end
35
-
51
+
36
52
  private
37
-
38
- def create_controller_instance(controller_name, controller_class)
39
- controller_instance = controller_class.new(self)
40
- @controllers[controller_name] = controller_instance
41
- controller_instance.application_window
42
- controller_instance
43
- end
44
-
45
- def directory_of(application_file)
46
- File.dirname(File.expand_path(application_file))
53
+
54
+ def create_controller_instance controller_name, controller_class
55
+ controller_instance = controller_class.new self
56
+
57
+ @controllers[controller_name] = controller_instance
58
+
59
+ controller_instance.application_window
60
+ controller_instance
61
+ end
62
+
63
+ def directory_of application_file
64
+ File.dirname(File.expand_path(application_file))
65
+ end
66
+
67
+ def load_controllers_and_views directory
68
+ Dir.glob(File.join(directory, 'controllers/**/*.rb')).each do |controller_file|
69
+ load(controller_file)
47
70
  end
48
-
49
- def load_controllers_and_views(directory)
50
- Dir.glob(File.join(directory, 'controllers', '**', '*.rb')).each do |controller_file|
51
- load(controller_file)
52
- end
53
- Dir.glob(File.join(directory, 'views', '**', '*.rb')).each do |view_file|
54
- load(view_file)
55
- end
71
+ Dir.glob(File.join(directory, 'views/**/*.rb')).each do |view_file|
72
+ load(view_file)
56
73
  end
57
-
74
+ end
58
75
  end
59
76
 
60
77
  class HotCocoaController
61
-
62
- def self.view_instances
63
- @view_instances ||= {}
78
+
79
+ class << self
80
+ attr_reader :view_instances
64
81
  end
65
-
82
+ @view_instances = {}
83
+
66
84
  attr_reader :application
67
-
68
- def initialize(application)
85
+
86
+ def initialize application
69
87
  @application = application
70
88
  end
71
-
89
+
72
90
  def application_window
73
91
  @application.application_controller.application_window
74
92
  end
75
-
76
93
  end
77
94
 
78
95
  class HotCocoaApplicationController < HotCocoaController
79
-
80
- def initialize(application)
81
- super(application)
82
- end
83
-
84
96
  def application_window
85
97
  @application_window ||= ApplicationWindow.new(self).application_window
86
98
  end
87
-
88
99
  end
89
100
 
90
101
  class HotCocoaWindow
91
-
92
- attr_reader :application_controller, :application_window
93
-
94
- def initialize(application_controller)
102
+ attr_reader :application_controller
103
+ attr_reader :application_window
104
+
105
+ def initialize application_controller
95
106
  @application_controller = application_controller
96
107
  render
97
108
  end
98
-
109
+
99
110
  def render
100
111
  @application_window = HotCocoa.window(ApplicationView.options[:window])
101
112
  @application_window.delegate_to(application_controller)
102
113
  @application_window.view << application_controller.application_view
103
114
  end
104
-
105
115
  end
106
116
 
107
117
  class HotCocoaView < HotCocoa::LayoutView
108
-
109
- DefaultLayoutOptions = {:expand => [:width, :height]}
110
-
118
+ DefaultLayoutOptions = { expand: [:width, :height] }
119
+
111
120
  module ClassMethods
112
- def controller(name=nil)
121
+ def controller name = nil
113
122
  if name
114
123
  @name = name
115
124
  else
116
125
  @name || :application_controller
117
126
  end
118
127
  end
119
- def options(options=nil)
128
+
129
+ def options options = nil
120
130
  if options
121
131
  @options = options
122
132
  else
@@ -124,22 +134,23 @@ class HotCocoaView < HotCocoa::LayoutView
124
134
  end
125
135
  end
126
136
  end
127
-
128
- def self.inherited(klass)
137
+
138
+ def self.inherited klass
129
139
  klass.extend(ClassMethods)
130
140
  klass.send(:include, HotCocoa::Behaviors)
131
- class_name = klass.name.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
141
+ class_name = klass.name.underscore
142
+
132
143
  HotCocoaController.class_eval %{
133
144
  def #{class_name}
134
145
  unless HotCocoaController.view_instances[:#{class_name}]
135
- HotCocoaController.view_instances[:#{class_name}] = #{klass.name}.alloc.initWithFrame([0,0,0,0])
146
+ HotCocoaController.view_instances[:#{class_name}] = #{klass.name}.alloc.initWithFrame(CGRectZero)
136
147
  HotCocoaController.view_instances[:#{class_name}].setup_view
137
148
  end
138
149
  HotCocoaController.view_instances[:#{class_name}]
139
150
  end
140
151
  }, __FILE__, __LINE__
141
152
  end
142
-
153
+
143
154
  attr_reader :controller
144
155
 
145
156
  def setup_view
@@ -149,27 +160,25 @@ class HotCocoaView < HotCocoa::LayoutView
149
160
  render
150
161
  end
151
162
  end
152
-
163
+
164
+
153
165
  private
154
-
155
- def class_controller
156
- HotCocoaApplication.instance.controller(self.class.controller)
157
- end
158
-
159
- def layout_options
160
- options = if self.class.options && self.class.options[:layout]
161
- self.class.options[:layout]
162
- else
163
- DefaultLayoutOptions
164
- end
165
- end
166
-
166
+
167
+ def class_controller
168
+ HotCocoaApplication.instance.controller(self.class.controller)
169
+ end
170
+
171
+ def layout_options
172
+ options = if self.class.options && self.class.options[:layout]
173
+ self.class.options[:layout]
174
+ else
175
+ DefaultLayoutOptions
176
+ end
177
+ end
167
178
  end
168
179
 
169
180
  class ApplicationWindow < HotCocoaWindow
170
-
171
181
  end
172
182
 
173
183
  class Application < HotCocoaApplication
174
-
175
184
  end
@@ -1,62 +1,139 @@
1
- module HotCocoa
2
-
3
- class NotificationListener
4
-
5
- DistributedBehaviors = {
6
- :drop => NSNotificationSuspensionBehaviorDrop,
7
- :coalesce => NSNotificationSuspensionBehaviorCoalesce,
8
- :hold => NSNotificationSuspensionBehaviorHold,
9
- :deliver_immediately => NSNotificationSuspensionBehaviorDeliverImmediately
10
- }
11
-
12
- attr_reader :callback, :name, :sender, :suspension_behavior
13
-
14
- def self.registered_listeners
15
- @registered_listeners ||= []
16
- end
17
-
18
- def initialize(options={}, &block)
19
- @callback = block
20
- @distributed = (options[:distributed] == true)
21
- @suspension_behavior = DistributedBehaviors[options[:when_suspended] || :coalesce]
22
- @name = options[:named]
23
- @sender = options[:sent_by]
24
- NotificationListener.registered_listeners << self
25
- observe
26
- end
27
-
28
- def distributed?
29
- @distributed
30
- end
31
-
32
- def receive(notification)
33
- callback.call(notification)
1
+ ##
2
+ # Object that can register a block to receive arbitrary notifications from
3
+ # the default NSNotificationCenter or NSDistributedNotificationCenter.
4
+ class HotCocoa::NotificationListener
5
+
6
+ ##
7
+ # Map Ruby-ish names for distributed behaviors.
8
+ #
9
+ # @return [Hash{Symbol=>NSNotificationSuspensionBehavior}]
10
+ DistributedBehaviors = {
11
+ drop: NSNotificationSuspensionBehaviorDrop,
12
+ coalesce: NSNotificationSuspensionBehaviorCoalesce,
13
+ hold: NSNotificationSuspensionBehaviorHold,
14
+ deliver_immediately: NSNotificationSuspensionBehaviorDeliverImmediately
15
+ }
16
+
17
+ ##
18
+ # Name of the notification to listen for.
19
+ #
20
+ # @return [String]
21
+ attr_reader :name
22
+
23
+ ##
24
+ # Name of the notification sender.
25
+ #
26
+ # @return [String]
27
+ attr_reader :sender
28
+
29
+ # @return [NSNotificationSuspensionBehavior]
30
+ attr_reader :suspension_behavior
31
+
32
+ ##
33
+ # Whether or not the object is registered with the default distributed
34
+ # or regular notification center.
35
+ #
36
+ # @return [Boolean]
37
+ attr_reader :distributed
38
+ alias_method :distributed?, :distributed
39
+
40
+ class << self
41
+ ##
42
+ # List of all {HotCocoa::NotificationListeners}.
43
+ #
44
+ # @return [HotCocoa::NotificationListener]
45
+ attr_reader :registered_listeners
46
+ end
47
+ @registered_listeners = []
48
+
49
+ ##
50
+ # @todo Would be better to use #define_singleton_method to define a new
51
+ # {#receive} for each instance?
52
+ #
53
+ # @param [Hash] options
54
+ # @options options [String] :named name of the notification
55
+ # @options options [nil] :sent_by (nil) the signature of the notification
56
+ # poster (i.e. the method to post notifications asks for a
57
+ # notificationSender, these values must match up), or set to nil to
58
+ # receive notifications from anyone
59
+ # @options options [NSNotificationSuspensionBehavior] :when_suspended (:coalesce)
60
+ # behavior if notification is not set to be being delivered immediately
61
+ # by the sender, see {HotCocoa::NotificationListener::DistributedBehaviors}
62
+ # for possible values
63
+ # @options options [Boolean] :distributed (false)
64
+ #
65
+ # @yield the block given here will become the callback for the
66
+ # notification
67
+ # @yieldparam [String] notification the name of the notification received
68
+ def initialize options = {}, &block
69
+ raise 'You must pass a block to act as the callback' unless block_given?
70
+ @callback = block
71
+
72
+ @name = options[:named]
73
+ @sender = options[:sent_by]
74
+ @suspension_behavior = DistributedBehaviors[options[:when_suspended] || :coalesce]
75
+ @distributed = (options[:distributed] == true)
76
+ NotificationListener.registered_listeners << self
77
+ observe
78
+ end
79
+
80
+ ##
81
+ # Stop the listener from listening to any future notifications. The
82
+ # options available here are the same as the {#initialize} methods
83
+ # `:named` and `:sent_by` options.
84
+ def stop_notifications options = {}
85
+ if options.has_key?(:named) || options.has_key?(:sent_by)
86
+ notification_center.removeObserver self,
87
+ name: options[:named],
88
+ object: options[:sent_by]
89
+ else
90
+ notification_center.removeObserver self
34
91
  end
35
-
36
- def stop_notifications(options={})
37
- if options.has_key?(:named) || options.has_key?(:sent_by)
38
- notification_center.removeObserver(self, name:options[:named], object:options[:sender])
39
- else
40
- notification_center.removeObserver(self)
41
- end
92
+ end
93
+
94
+ ##
95
+ # The callback called when a notification is posted. You should not be
96
+ # directly calling this yourself.
97
+ def receive notification
98
+ @callback.call notification
99
+ end
100
+
101
+
102
+ private
103
+
104
+ ##
105
+ # Register for the notification.
106
+ def observe
107
+ if distributed?
108
+ notification_center.addObserver self,
109
+ selector: 'receive:',
110
+ name: name,
111
+ object: sender,
112
+ suspensionBehavior: suspension_behavior
113
+
114
+ else
115
+ notification_center.addObserver self,
116
+ selector: 'receive:',
117
+ name: name,
118
+ object: sender
42
119
  end
43
-
44
- private
45
-
46
- def observe
47
- if distributed?
48
- notification_center.addObserver self, selector:'receive:', name:name, object:sender, suspensionBehavior:suspension_behavior
49
- else
50
- notification_center.addObserver self, selector:'receive:', name:name, object:sender
51
- end
52
- end
53
-
54
- def notification_center
55
- @notification_center ||= (distributed? ? NSDistributedNotificationCenter.defaultCenter : NSNotificationCenter.defaultCenter)
56
- end
57
120
  end
58
-
59
- def on_notification(options={}, &block)
60
- NotificationListener.new(options, &block)
121
+
122
+ ##
123
+ # Returns the notification center to which the listener is registered.
124
+ #
125
+ # @return [NSNotificationCenter,NSDistributedNotificationCenter]
126
+ def notification_center
127
+ @notification_center ||= (distributed ? NSDistributedNotificationCenter.defaultCenter : NSNotificationCenter.defaultCenter)
61
128
  end
62
- end
129
+ end
130
+
131
+ ##
132
+ # Register for a notification given a block. The options and block given
133
+ # here will be passed to {HotCocoa::NotificationListener#initialize};
134
+ # this method is merely syntactic sugar.
135
+ #
136
+ # @return [HotCocoa::NotificationListener
137
+ def HotCocoa.on_notification options = {}, &block
138
+ HotCocoa::NotificationListener.new(options, &block)
139
+ end
@@ -1,21 +1,29 @@
1
- AppConfig = HotCocoa::ApplicationBuilder::Configuration.new("config/build.yml")
1
+ $stderr.puts <<EOM
2
+ standard_rake_tasks.rb is deprecated in favour of a new app template
3
+ which declares the tasks directly in the template Rakefile.
2
4
 
3
- desc "Build a deployable version of the application"
4
- task :deploy => [:clean] do
5
- HotCocoa::ApplicationBuilder.build(AppConfig, :deploy => true)
5
+ You can update your Rakefile by copying the new tasks from Github at
6
+ https://github.com/ferrous26/hotcocoa/blob/master/template/Rakefile
7
+ EOM
8
+
9
+ AppConfig = HotCocoa::ApplicationBuilder::Configuration.new( 'config/build.yml' )
10
+
11
+ desc 'Build a deployable version of the application'
12
+ task :deploy do
13
+ HotCocoa::ApplicationBuilder.build AppConfig, deploy: true
6
14
  end
7
15
 
8
- desc "Build the application"
16
+ desc 'Build the application'
9
17
  task :build do
10
- HotCocoa::ApplicationBuilder.build(AppConfig)
18
+ HotCocoa::ApplicationBuilder.build AppConfig
11
19
  end
12
20
 
13
- desc "Build and execute the application"
21
+ desc 'Build and execute the application'
14
22
  task :run => [:build] do
15
23
  `"./#{AppConfig.name}.app/Contents/MacOS/#{AppConfig.name.gsub(/ /, '')}"`
16
24
  end
17
25
 
18
- desc "Cleanup build files"
26
+ desc 'Cleanup build files'
19
27
  task :clean do
20
- `/bin/rm -rf "#{AppConfig.name}.app"`
28
+ sh "/bin/rm -rf '#{AppConfig.name}.app'"
21
29
  end
@@ -0,0 +1,39 @@
1
+ ##
2
+ # This module should be mixed into mappings that want to support
3
+ # target actions.
4
+ module HotCocoa::Mappings::TargetActionConvenience
5
+
6
+ ##
7
+ # @todo testing
8
+ #
9
+ # @param [Proc] behavior
10
+ def on_action= behavior
11
+ if target && (
12
+ target.instance_variable_get(:@action_behavior) ||
13
+ target.instance_variable_get(:@double_action_behavior))
14
+
15
+ @object.instance_variable_set(:@action_behavior, behavior)
16
+ @object = target
17
+
18
+ else
19
+ @object = Object.new
20
+ @object.instance_variable_set(:@action_behavior, behavior)
21
+ setTarget(@object)
22
+ end
23
+
24
+ def @object.perform_action sender
25
+ @action_behavior.call sender
26
+ end
27
+
28
+ setAction('perform_action:')
29
+ end
30
+
31
+ ##
32
+ # @todo testing
33
+ #
34
+ # @yieldparam [id] sender
35
+ def on_action &behavior
36
+ self.on_action = behavior
37
+ self
38
+ end
39
+ end
@@ -1,27 +1,96 @@
1
1
  require 'fileutils'
2
- require 'rbconfig'
3
2
 
4
- module HotCocoa
5
- class Template
6
-
7
- def self.source_directory
8
- File.expand_path(File.join(File.dirname(__FILE__), "..", ".."))
3
+ ##
4
+ # This class creates new projects given the directory to put them
5
+ # into and the name for the new project.
6
+ class HotCocoa::Template
7
+ include FileUtils
8
+
9
+ ##
10
+ # Directories that need to be created for a new app project.
11
+ #
12
+ # @return [Array<String>]
13
+ DIRECTORIES = [ 'lib', 'resources' ]
14
+
15
+ ##
16
+ # Project files which can be directly copied to a new project.
17
+ #
18
+ # @return [Array<String>]
19
+ COPIED_FILES = [
20
+ 'lib/menu.rb',
21
+ 'resources/HotCocoa.icns'
22
+ ]
23
+
24
+ ##
25
+ # Project files which contain tokens that need to be substituted
26
+ # before being copied to a new project.
27
+ #
28
+ # @return [Array<String>]
29
+ FILTERED_FILES = [
30
+ 'Rakefile',
31
+ '__APPLICATION_NAME__.appspec',
32
+ 'lib/application.rb',
33
+ ]
34
+
35
+ ##
36
+ # The path to the HotCocoa app template in the HotCocoa source files
37
+ #
38
+ # @return [String]
39
+ def self.template_directory
40
+ file = $LOADED_FEATURES.find { |file| file.match /hotcocoa\/template\.rbo?$/ }
41
+ File.expand_path(File.join(File.dirname(file), '../../template'))
42
+ end
43
+
44
+ # @param [String] dir where to put the project
45
+ # @param [String] name name for the project
46
+ def initialize dir, name
47
+ @directory = dir
48
+ @app_name = name
49
+ end
50
+
51
+ ##
52
+ # Create the project!
53
+ def copy!
54
+ @template = self.class.template_directory
55
+ make_directories
56
+ copy_resources
57
+ copy_sources
58
+ end
59
+
60
+
61
+ private
62
+
63
+ ##
64
+ # Create the directory structure for a new app.
65
+ def make_directories
66
+ mkdir_p @directory
67
+ DIRECTORIES.each do |dir|
68
+ mkdir_p File.join(@directory, dir)
69
+ end
70
+ end
71
+
72
+ ##
73
+ # Copy all files that do not need to have tokens substituted.
74
+ def copy_resources
75
+ COPIED_FILES.each do |file|
76
+ cp File.join(@template, file), File.join(@directory, file)
9
77
  end
10
-
11
- def self.copy_to(directory, app_name)
12
- FileUtils.mkdir_p(directory)
13
- Dir.glob(File.join(source_directory, "template", "**/*")).each do |file|
14
- short_name = file[(source_directory.length+10)..-1]
15
- if File.directory?(file)
16
- FileUtils.mkdir_p File.join(directory, short_name)
17
- else
18
- File.open(File.join(directory, short_name), "w") do |out|
19
- input = File.read(file)
20
- input.gsub!(/__APPLICATION_NAME__/, app_name)
21
- out.write input
22
- end
23
- end
24
- end
78
+ end
79
+
80
+ ##
81
+ # Copy all files that need to have tokens substituted.
82
+ def copy_sources
83
+ company_name = ENV['USER'] || 'yourcompany'
84
+
85
+ FILTERED_FILES.each do |file|
86
+ input = File.read File.join(@template, file)
87
+ input.gsub! /__APPLICATION_NAME__/, @app_name
88
+ input.gsub! /__COMPANY_NAME__/, company_name
89
+
90
+ outname = file.gsub /__APPLICATION_NAME__/, @app_name
91
+ outname = File.join(@directory, outname)
92
+ File.open(outname, 'w') { |out| out.write input }
25
93
  end
26
94
  end
95
+
27
96
  end
@@ -0,0 +1,3 @@
1
+ module HotCocoa
2
+ VERSION = '0.6.0pre'
3
+ end