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
@@ -0,0 +1,27 @@
1
+ HotCocoa::Mappings.map bonjour_browser: NSNetServiceBrowser do
2
+
3
+ ##
4
+ # There are no values that need to be set at initialization
5
+ def init_with_options browser, opts
6
+ browser.init
7
+ end
8
+
9
+ custom_methods do
10
+ def search_for_services type, domain = ''
11
+ searchForServicesOfType type, inDomain: domain
12
+ end
13
+
14
+ def search_for_domains
15
+ searchForBrowsableDomains
16
+ end
17
+ end
18
+
19
+ delegating 'netServiceBrowser:didFindDomain:moreComing:', to: 'did_find_domain', parameters: ['didFindDomain', 'moreComing']
20
+ delegating 'netServiceBrowser:didRemoveDomain:moreComing:', to: 'did_remove_domain', parameters: ['didRemoveDomain', 'moreComing']
21
+ delegating 'netServiceBrowser:didFindService:moreComing:', to: 'did_find_service', parameters: ['didFindService', 'moreComing']
22
+ delegating 'netServiceBrowser:didRemoveService:moreComing:', to: 'did_remove_service', parameters: ['didRemoveService', 'moreComing']
23
+ delegating 'netServiceBrowserWillSearch:', to: 'will_search'
24
+ delegating 'netServiceBrowser:didNotSearch:', to: 'did_not_search', parameters: ['didNotSearch']
25
+ delegating 'netServiceBrowserDidStopSearch:', to: 'did_stop_search'
26
+
27
+ end
@@ -0,0 +1,24 @@
1
+ HotCocoa::Mappings.map notification: NSNotification do
2
+
3
+ defaults post: true, distributed: false
4
+
5
+ def alloc_with_options options
6
+ if options.delete :post
7
+ if options.delete :distributed
8
+ NSDistributedNotificationCenter.defaultCenter.postNotificationName options.delete(:name),
9
+ object: options.delete(:object),
10
+ userInfo: options.delete(:info),
11
+ deliverImmediately: (options.delete(:immediately) == true)
12
+ else
13
+ NSNotificationCenter.defaultCenter.postNotificationName options.delete(:name),
14
+ object: options.delete(:object),
15
+ userInfo: options.delete(:info)
16
+ end
17
+ else
18
+ NSNotification.notificationName options.delete(:name),
19
+ object: options.delete(:object),
20
+ userInfo: options.delete(:info)
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,17 @@
1
+ HotCocoa::Mappings.map sort_descriptor: NSSortDescriptor do
2
+
3
+ defaults ascending: true
4
+
5
+ def init_with_options sort_descriptor, opts
6
+ if opts.has_key? :selector
7
+ sort_descriptor.initWithKey opts.delete(:key),
8
+ ascending: opts.delete(:ascending)
9
+
10
+ else
11
+ sort_descriptor.initWithKey opts.delete(:key),
12
+ ascending: opts.delete(:ascending),
13
+ selector: opts.delete(:selector)
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,38 @@
1
+ HotCocoa::Mappings.map timer: NSTimer do
2
+
3
+ defaults scheduled: true, repeats: false
4
+
5
+ def alloc_with_options options
6
+ raise ArgumentError, 'timer requires :interval' unless options.has_key? :interval
7
+
8
+ target = options.delete :target
9
+ selector = options.delete :selector
10
+ unless target && selector
11
+ raise ArgumentError, 'timer requires either :target and :selector or :on_action' unless options.has_key? :on_action
12
+
13
+ target = Object.new
14
+ target.instance_variable_set(:@block, options.delete(:on_action))
15
+
16
+ def target.fire timer
17
+ @block.call(timer)
18
+ end
19
+
20
+ selector = 'fire:'
21
+ end
22
+
23
+ if options.delete :scheduled
24
+ NSTimer.scheduledTimerWithTimeInterval options.delete(:interval),
25
+ target: target,
26
+ selector: selector,
27
+ userInfo: options.delete(:info),
28
+ repeats: options.delete(:repeats)
29
+ else
30
+ NSTimer.timerWithTimeInterval options.delete(:interval),
31
+ target: target,
32
+ selector: selector,
33
+ userInfo: options.delete(:info),
34
+ repeats: options.delete(:repeats)
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,43 @@
1
+ HotCocoa::Mappings.map user_defaults: NSUserDefaults do
2
+
3
+ defaults defaults: {}
4
+
5
+ def alloc_with_options options
6
+ user_defaults = NSUserDefaults.standardUserDefaults
7
+
8
+ unless options[:defaults].nil?
9
+ defs = {}
10
+ # force all keys to be strings, doesn't seem to like symbols
11
+ options.delete(:defaults).each_pair { |key, value| defs[key.to_s] = value }
12
+ user_defaults.registerDefaults(defs)
13
+ end
14
+
15
+ user_defaults
16
+ end
17
+
18
+ custom_methods do
19
+ def []= key, value
20
+ if value.nil?
21
+ delete key.to_s
22
+ else
23
+ setObject value, forKey: key.to_s
24
+ end
25
+ sync
26
+ end
27
+
28
+ def [] key
29
+ objectForKey key.to_s
30
+ end
31
+
32
+ def delete key
33
+ removeObjectForKey key.to_s
34
+ sync
35
+ end
36
+
37
+ # @todo Perhaps we should be making a an alert instead of printing to the log?
38
+ def sync
39
+ puts 'Failed to synchronize' unless synchronize
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,44 @@
1
+ HotCocoa::Mappings.map xml_parser: NSXMLParser do
2
+
3
+ def alloc_with_options options
4
+ if options[:url]
5
+ url = options.delete :url
6
+ url = NSURL.URLWithString url if url.is_a? String
7
+ NSXMLParser.alloc.initWithContentsOfURL url
8
+
9
+ elsif options[:file]
10
+ NSXMLParser.alloc.initWithData NSData.dataWithContentsOfFile options.delete :file
11
+
12
+ elsif options[:data]
13
+ NSXMLParser.alloc.initWithData options.delete :data
14
+
15
+ else
16
+ raise 'Must provide either :url, :file, or :data when constructing an NSXMLParser'
17
+ end
18
+ end
19
+
20
+ delegating 'parserDidStartDocument:', to: :on_start_document
21
+ delegating 'parserDidEndDocument:', to: :on_end_document
22
+ delegating 'parser:didStartElement:namespaceURI:qualifiedName:attributes:', to: :on_start_element, parameters: [:didStartElement, :namespaceURI, :qualifiedName, :attributes]
23
+ delegating 'parser:didEndElement:namespaceURI:qualifiedName:', to: :on_end_element, parameters: [:didEndElement, :namespaceURI, :qualifiedName]
24
+ delegating 'parser:didStartMappingPrefix:toURI:', to: :on_start_mapping_prefix, parameters: [:didStartMappingPrefix, :toURI]
25
+ delegating 'parser:didEndMappingPrefix:', to: :on_end_mapping_prefix, parameters: [:didEndMappingPrefix]
26
+
27
+ delegating 'parser:foundAttributeDeclarationWithName:forElement:type:defaultValue:', to: :on_attribute_declaration, parameters: [:foundAttributeDeclarationWithName, :forElement, :type, :defaultValue]
28
+ delegating 'parser:foundCDATA:', to: :on_cdata, parameters: [:foundCDATA]
29
+ delegating 'parser:foundCharacters:', to: :on_characters, parameters: [:foundCharacters]
30
+ delegating 'parser:foundComment:', to: :on_comment, parameters: [:foundComment]
31
+ delegating 'parser:foundElementDeclarationWithName:model:', to: :on_element_declaration, parameters: [:foundElementDeclarationWithName, :model]
32
+ delegating 'parser:foundExternalEntityDeclarationWithName:publicID:systemID:', to: :on_external_entity_declaration, parameters: [:foundExternalEntityDeclarationWithName, :publicID, :systemID]
33
+ delegating 'parser:foundIgnorableWhitespace:', to: :on_ignorable_whitespace, parameters: [:foundIgnorableWhitespace]
34
+ delegating 'parser:foundInternalEntityDeclarationWithName:value:', to: :on_internal_entity_declaration, parameters: [:foundInternalEntityDeclarationWithName, :value]
35
+ delegating 'parser:foundNotationDeclarationWithName:publicID:systemID:', to: :on_notation_declaration, parameters: [:foundNotationDeclarationWithName, :data]
36
+ delegating 'parser:foundProcessingInstructionWithTarget:data:', to: :on_processing_instruction, parameters: [:foundProcessingInstructionWithTarget, :data]
37
+ delegating 'parser:foundUnparsedEntityDeclarationWithName:publicID:systemID:notationName:', to: :on_unparsed_entity_declaration, parameters: [:foundUnparsedEntityDeclarationWithName, :publicID, :systemID, :notationName]
38
+
39
+ delegating 'parser:resolveExternalEntityName:systemID:', to: :resolve_external_entity_name, parameters: [:resolveExternalEntityName, :systemID]
40
+
41
+ delegating 'parser:parseErrorOccurred:', to: :on_parse_error, parameters: [:parseErrorOccurred]
42
+ delegating 'parser:validationErrorOccurred:', to: :on_validation_error, parameters: [:validationErrorOccurred]
43
+
44
+ end
@@ -0,0 +1,15 @@
1
+ HotCocoa::Mappings.map movie: QTMovie do
2
+
3
+ def alloc_with_options options
4
+ if options.has_key? :file
5
+ QTMovie.movieWithFile options.delete(:file), error:options.delete(:error)
6
+
7
+ elsif options.has_key? :url
8
+ QTMovie.movieWithURL NSURL.URLWithString(options.delete :url), error:options.delete(:error)
9
+
10
+ else
11
+ raise 'Can only allocate a movie from a file or a url'
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,28 @@
1
+ HotCocoa::Mappings.map movie_view: QTMovieView do
2
+
3
+ defaults layout: {}, frame: CGRectZero
4
+
5
+ def init_with_options movie_view, options
6
+ movie_view.initWithFrame(options.delete :frame)
7
+ end
8
+
9
+ custom_methods do
10
+ def controller_visible= value
11
+ setControllerVisible value
12
+ end
13
+
14
+ ##
15
+ # @todo Change this method to use varargs in the future when we
16
+ # start deprecating things
17
+ def controller_buttons= buttons
18
+ setBackButtonVisible buttons.include? :back
19
+ setCustomButtonVisible buttons.include? :custom
20
+ setHotSpotButtonVisible buttons.include? :hot_spot
21
+ setStepButtonsVisible buttons.include? :step
22
+ setTranslateButtonVisible buttons.include? :translate
23
+ setVolumeButtonVisible buttons.include? :volume
24
+ setZoomButtonsVisible buttons.include? :zoom
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,20 @@
1
+ HotCocoa::Mappings.map web_view: WebView do
2
+
3
+ defaults layout: {}, frame: CGRectZero
4
+
5
+ def init_with_options web_view, options
6
+ web_view.initWithFrame options.delete :frame
7
+ end
8
+
9
+ custom_methods do
10
+ def url= url
11
+ url = NSURL.URLWithString(url) if url.kind_of? String
12
+ mainFrame.loadRequest NSURLRequest.requestWithURL url
13
+ end
14
+
15
+ def auto_size
16
+ setAutoresizingMask(NSViewHeightSizable|NSViewWidthSizable)
17
+ end
18
+ end
19
+
20
+ end
@@ -1,115 +1,61 @@
1
- module HotCocoa
2
- module Mappings
3
-
4
- def self.reload
5
- Dir.glob(File.join(File.dirname(__FILE__), "mappings", "*.rb")).each do |mapping|
6
- require mapping
7
- end
8
- end
9
-
10
- DefaultEmptyRect = [0,0,0,0]
11
-
12
- module TargetActionConvenience
13
- def on_action=(behavior)
14
- if target && (
15
- target.instance_variable_get("@action_behavior") ||
16
- target.instance_variable_get("@double_action_behavior"))
17
- object.instance_variable_set("@action_behavior", behavior)
18
- object = target
19
- else
20
- object = Object.new
21
- object.instance_variable_set("@action_behavior", behavior)
22
- setTarget(object)
23
- end
24
- def object.perform_action(sender)
25
- @action_behavior.call(sender)
26
- end
27
- setAction("perform_action:")
28
- end
29
-
30
- def on_action(&behavior)
31
- self.on_action = behavior
32
- self
33
- end
34
- end
35
-
36
- # TODO: Needs docs for all possible invocations and examples!
37
- def self.map(options, &block)
38
- framework = options.delete(:framework)
39
- mapped_name = options.keys.first
40
- mapped_value = options.values.first
41
- args = [mapped_name, mapped_value]
42
-
43
- if mapped_value.kind_of?(Class)
44
- add_mapping(*args, &block)
45
- else
46
- if framework.nil? || loaded_framework?(framework)
47
- add_constant_mapping(*args, &block)
48
- else
49
- on_framework(framework) do
50
- add_constant_mapping(*args, &block)
51
- end
52
- end
53
- end
54
- end
55
-
56
- # Registers +mapped_name+ as a Mapper#builder_method for the given
57
- # +mapped_value+. The +block+ is used as the Mapper#builder_method's body.
58
- def self.add_mapping(mapped_name, mapped_value, &block)
59
- m = Mapper.map_instances_of(mapped_value, mapped_name, &block)
60
- mappings[m.builder_method] = m
61
- end
62
-
63
- # Registers +mapped_name+ as a Mapper#builder_method for the given
64
- # +constant+ string which will be looked up. The +block+ is used as the
65
- # Mapper#builder_method's body.
66
- def self.add_constant_mapping(mapped_name, constant, &block)
67
- add_mapping(mapped_name, Object.full_const_get(constant), &block)
68
- end
69
-
70
- # Returns the Hash of mappings.
71
- def self.mappings
72
- @mappings ||= {}
73
- end
74
-
75
- # Registers a callback for after the specified framework has been loaded.
76
- def self.on_framework(name, &block)
77
- (frameworks[name.to_s] ||= []) << block
78
- end
79
-
80
- # Returns the Hash of mapped frameworks.
81
- def self.frameworks
82
- @frameworks ||= {}
83
- end
84
-
85
- # Returns the Set of loaded frameworks.
86
- def self.loaded_frameworks
87
- @loaded_frameworks ||= Set.new
88
- end
89
-
90
- # Registers a given framework as being loaded.
91
- def self.framework_loaded
92
- frameworks.keys.each do |key|
93
- if loaded_framework?(key)
94
- frameworks[key].each do |mapper|
95
- mapper.call
96
- end
97
- frameworks.delete(key)
98
- end
1
+ module HotCocoa::Mappings
2
+ # needs to be initialized
3
+ @mappings = {}
4
+
5
+ ##
6
+ # This is a hack to work around a MacRuby issue with compiled files.
7
+ #
8
+ # Return the absolute path to `lib/hotcocoa`.
9
+ #
10
+ # @return [String]
11
+ file = $LOADED_FEATURES.find { |file| file.match /hotcocoa\/mappings\.rbo?$/ }
12
+ @path = File.expand_path(File.join(File.dirname(file), 'mappings'))
13
+ end
14
+
15
+ class << HotCocoa::Mappings
16
+
17
+ ##
18
+ # The cache of mappers for available mappings.
19
+ #
20
+ # @return [Hash{Symbol=>HotCocoa::Mappings::Mapper}]
21
+ attr_reader :mappings
22
+
23
+ ##
24
+ # Load mappings for every loaded framework.
25
+ def reload
26
+ $LOADED_FRAMEWORKS.each do |framework| load framework end
27
+ end
28
+
29
+ ##
30
+ # Load mappings for a given framework.
31
+ #
32
+ # @param [String] framework
33
+ def load framework
34
+ framework = framework.downcase
35
+ dir = File.join(@path, framework)
36
+ if Dir.exists? dir
37
+ Dir.glob(File.join(dir, '**/*.rb')).each do |mapping|
38
+ require mapping.chomp! '.rb'
99
39
  end
100
40
  end
101
-
102
- # Returns whether or not the framework has been loaded yet.
103
- def self.loaded_framework?(name)
104
- NSBundle.allFrameworks.map { |bundle|
105
- bundle.bundlePath.split("/").last
106
- }.select { |framework|
107
- framework.split(".")[1] == 'framework'
108
- }.map { |framework|
109
- framework.split(".")[0]
110
- }.include?(name.to_s)
111
- end
112
-
113
41
  end
114
-
42
+
43
+ ##
44
+ # Create a new mapping by registering `mapped_name` as a builder
45
+ # method for `mapped_class`.
46
+ #
47
+ # @example
48
+ #
49
+ # HotCocoa::Mappings.map( window: NSWindow ) do
50
+ # # define your mapping
51
+ # end
52
+ #
53
+ # @param [Symbol] mapped_name
54
+ # @param [Class] mapped_class
55
+ def map name, &block
56
+ mapped_name, mapped_class = name.first
57
+ mappings[mapped_name] =
58
+ HotCocoa::Mappings::Mapper.map_instances_of mapped_class, mapped_name, &block
59
+ end
60
+
115
61
  end