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,128 @@
1
+ ##
2
+ # Similar to a Ruby Hash, except specialized for dealing with a hash
3
+ # of attributes for NSAttributedString objects.
4
+ class HotCocoa::NSRangedProxyAttributeHash
5
+
6
+ # @return [Hash{Symbol=>StandardAttributedStringAttributes}]
7
+ ATTRIBUTE_KEYS = {
8
+ font: NSFontAttributeName,
9
+ paragraph_style: NSParagraphStyleAttributeName,
10
+ color: NSForegroundColorAttributeName,
11
+ colour: NSForegroundColorAttributeName,
12
+ underline_style: NSUnderlineStyleAttributeName,
13
+ superscript: NSSuperscriptAttributeName,
14
+ background_color: NSBackgroundColorAttributeName,
15
+ background_colour: NSBackgroundColorAttributeName,
16
+ attachment: NSAttachmentAttributeName,
17
+ ligature: NSLigatureAttributeName,
18
+ baseline_offset: NSBaselineOffsetAttributeName,
19
+ kerning: NSKernAttributeName,
20
+ link: NSLinkAttributeName,
21
+ stroke_width: NSStrokeWidthAttributeName,
22
+ stroke_color: NSStrokeColorAttributeName,
23
+ underline_color: NSUnderlineColorAttributeName,
24
+ strikethrough_style: NSStrikethroughStyleAttributeName,
25
+ strikethrough_color: NSStrikethroughColorAttributeName,
26
+ strikethrough_colour: NSStrikethroughColorAttributeName,
27
+ shadow: NSShadowAttributeName,
28
+ obliqueness: NSObliquenessAttributeName,
29
+ expansion_factor: NSExpansionAttributeName,
30
+ cursor: NSCursorAttributeName,
31
+ tool_tip: NSToolTipAttributeName,
32
+ character_shape: NSCharacterShapeAttributeName,
33
+ glyph_info: NSGlyphInfoAttributeName,
34
+ marked_clause_segment: NSMarkedClauseSegmentAttributeName,
35
+ spelling_state: NSSpellingStateAttributeName
36
+ }
37
+
38
+ # @param [HotCocoa::NSRangedProxyAttributedString]
39
+ def initialize proxy
40
+ @proxy = proxy
41
+ end
42
+
43
+ def [] k
44
+ k = attribute_for_key k
45
+ @proxy.string.attribute k, atIndex: @proxy.range.first, effectiveRange: nil
46
+ end
47
+
48
+ def []= k, v
49
+ k = attribute_for_key k
50
+ @proxy.string.removeAttribute k,
51
+ range: @proxy.range.to_NSRange(@proxy.string.length)
52
+ @proxy.string.addAttribute k,
53
+ value: v,
54
+ range: @proxy.range.to_NSRange(@proxy.string.length)
55
+ end
56
+
57
+ ##
58
+ # Append new attributes to the string
59
+ #
60
+ # @param [Hash] attributes
61
+ def << attributes
62
+ attributes.each_pair { |k, v| self[k] = v }
63
+ self
64
+ end
65
+ alias_method :merge!, :<<
66
+
67
+ ##
68
+ # Return a hash of the attributes, but without transforming constant
69
+ # names.
70
+ #
71
+ # @return [NSMutableDictionary]
72
+ def to_hash
73
+ dict = @proxy.string.attributesAtIndex @proxy.range.first,
74
+ effectiveRange: nil
75
+ NSMutableDictionary.dictionaryWithDictionary dict
76
+ end
77
+
78
+ # @return [String]
79
+ def inspect
80
+ to_hash.inspect
81
+ end
82
+
83
+
84
+ private
85
+
86
+ def key_for_attribute attribute
87
+ ATTRIBUTE_KEYS.key(attribute) || attribute
88
+ end
89
+
90
+ def attribute_for_key key
91
+ ATTRIBUTE_KEYS[key] || key
92
+ end
93
+ end
94
+
95
+
96
+ ##
97
+ # Proxies the range of an attributed string.
98
+ class HotCocoa::NSRangedProxyAttributedString
99
+
100
+ ##
101
+ # The string this proxy belongs to.
102
+ #
103
+ # @return [NSMutableAttributedString]
104
+ attr_reader :string
105
+
106
+ ##
107
+ # Range in the string this proxy represents.
108
+ #
109
+ # @return [Range]
110
+ attr_reader :range
111
+
112
+ # @param [NSMutableAttributedString] string
113
+ # @param [Range] range
114
+ def initialize string, range
115
+ @string = string
116
+ @range = range
117
+ end
118
+
119
+ ##
120
+ # This method is useful because it allows an attributed string to
121
+ # return a hash of attributes that use Rubyish names instead of
122
+ # Objective-C ones.
123
+ #
124
+ # @return [HotCooca::NSRangedProxyAttributeHash]
125
+ def attributes
126
+ HotCocoa::NSRangedProxyAttributeHash.new self
127
+ end
128
+ end
@@ -1,7 +1,40 @@
1
- module HotCocoa
2
- module Behaviors
3
- def Behaviors.included(klass)
4
- Mappings::Mapper.map_class(klass)
5
- end
1
+ ##
2
+ # A mixin module for classes that subclass Cocoa classes and want to also
3
+ # inherit HotCocoa customizations (custom methods, constants, etc.)
4
+ module HotCocoa::Behaviors
5
+
6
+ ##
7
+ # Implements the callback `Module#included`; this method declares that
8
+ # when `HotCocoa::Behaviors` is mixed in to a class that the class mixing
9
+ # in `HotCocoa::Behaviors` will gain all the HotCocoa customizations made
10
+ # to its superclasses.
11
+ #
12
+ # @example
13
+ #
14
+ # # Without including, you cannot call HotCocoa custom methods
15
+ # class CustomView < NSView
16
+ # def begin
17
+ # enter_full_screen # => NoMethodError
18
+ # end
19
+ # end
20
+ #
21
+ # # HotCocoa custom methods work when you inculde HotCocoa::Behaviors
22
+ # class OtherCustomView < NSView
23
+ # include HotCocoa::Behaviors
24
+ #
25
+ # def begin
26
+ # enter_full_screen # => goes full screen
27
+ # end
28
+ # end
29
+ #
30
+ def self.included klass
31
+ HotCocoa::Mappings::Mapper.map_class klass
6
32
  end
7
- end
33
+
34
+ end
35
+
36
+ ##
37
+ # Alias for HotCocoa::Behaviors
38
+ #
39
+ # @return [Module]
40
+ HotCocoa::Behaviours = HotCocoa::Behaviors
@@ -0,0 +1,67 @@
1
+ ##
2
+ # HotCocoa extensions for the Kernel module
3
+ module Kernel
4
+
5
+ ##
6
+ # Like $LOADED_FEATURES, but for frameworks.
7
+ #
8
+ # @return [Array<String>]
9
+ $LOADED_FRAMEWORKS = []
10
+
11
+ alias_method :default_framework, :framework
12
+ ##
13
+ # Override MacRuby's built-in #framework method in order to support lazy
14
+ # loading frameworks inside of HotCocoa.
15
+ def framework name
16
+ if default_framework name
17
+ $LOADED_FRAMEWORKS << name
18
+ HotCocoa::Mappings.load name
19
+ true
20
+ else
21
+ false
22
+ end
23
+ end
24
+
25
+ # Populate the list with everything that has already been loaded
26
+ $LOADED_FRAMEWORKS.concat NSBundle.allFrameworks.map { |bundle|
27
+ bundle.bundlePath.split('/').last
28
+ }.select { |framework|
29
+ framework.split('.')[1] == 'framework'
30
+ }.map! { |framework|
31
+ framework.split('.')[0]
32
+ }
33
+ $LOADED_FRAMEWORKS.uniq!
34
+
35
+ ##
36
+ # A mapping, lol
37
+ #
38
+ # @return [Hash]
39
+ PLIST_FORMATS = {
40
+ xml: NSPropertyListXMLFormat_v1_0,
41
+ binary: NSPropertyListBinaryFormat_v1_0
42
+ }
43
+
44
+ ##
45
+ # @todo encoding format can be pushed upstream?
46
+ #
47
+ # Override MacRuby's built-in {Kernel#to_plist} method to support
48
+ # specifying an output format. See {PLIST_FORMATS} for the available
49
+ # formats.
50
+ #
51
+ # @example Encoding a plist in the binary format
52
+ # { key: 'value' }.to_plist(:binary)
53
+ #
54
+ # @return [String] returns a string with the caller's contents
55
+ # serialized as a plist and encoded using `format`
56
+ def to_plist format = :xml
57
+ format_const = PLIST_FORMATS[format]
58
+ raise ArgumentError, "invalid format `#{format}'" unless format_const
59
+
60
+ error = Pointer.new :id
61
+ data = NSPropertyListSerialization.dataFromPropertyList self,
62
+ format: format_const,
63
+ errorDescription: error
64
+ error[0] ? raise(Exception, error[0]) : data.to_str
65
+ end
66
+
67
+ end
@@ -0,0 +1,15 @@
1
+ ##
2
+ # HotCocoa extensions for the NSArray class
3
+ class NSArray
4
+ ##
5
+ # Equivalent to `#[1]`, borrowed from Active Support.
6
+ def second
7
+ at(1)
8
+ end
9
+
10
+ ##
11
+ # Equivalent to `#[2]`, borrowed from Active Support.
12
+ def third
13
+ at(2)
14
+ end
15
+ end
@@ -0,0 +1,74 @@
1
+ ##
2
+ # HotCocoa extensions to the NSMutableAttributeString class.
3
+ class NSMutableAttributedString
4
+
5
+ ##
6
+ # @todo Should this be declared on NSAttributedString instead
7
+ #
8
+ # Make a new attributed string with the string of the receiver and
9
+ # the attributes passed as arguments.
10
+ #
11
+ # @param [Hash]
12
+ # @return [NSMutableAttributedString]
13
+ def with_attributes attributes = {}
14
+ string.with_attributes attributes
15
+ end
16
+
17
+ ##
18
+ # Just like `String#<<`
19
+ def << s
20
+ case s
21
+ when String
22
+ mutableString.appendString s
23
+ else
24
+ appendAttributedString s
25
+ end
26
+ end
27
+
28
+ ##
29
+ # @todo Should this be declared on NSAttributedString instead
30
+ #
31
+ # Just like `String#+`
32
+ def + s
33
+ attributed_string = mutableCopy
34
+ attributed_string << s
35
+ attributed_string
36
+ end
37
+
38
+ ##
39
+ # @todo Should this be declared on NSAttributedString instead
40
+ #
41
+ # Hash of attributes used in the attributed string.
42
+ #
43
+ # @return [HotCocoa::NSRangedProxyAttributeHash]
44
+ def attributes
45
+ HotCocoa::NSRangedProxyAttributedString.new(self, 0..-1).attributes
46
+ end
47
+
48
+ ##
49
+ # @deprecated Who uses this? Removing this in HotCocoa 0.7 unless
50
+ # there are reasonable objections
51
+ #
52
+ # Create a range proxy for an arbitrary portion of the receiver.
53
+ #
54
+ # @param [Range] r
55
+ # @return [HotCocoa::NSRangedProxyAttributedString]
56
+ def [] r
57
+ HotCocoa::NSRangedProxyAttributedString.new self, r
58
+ end
59
+
60
+ ##
61
+ # Replace an arbitrary range of an attributed string with another string.
62
+ #
63
+ # @param [Range] r
64
+ # @param [NSAttributedString,String] s
65
+ def []= r, s
66
+ case s
67
+ when String
68
+ replaceCharactersInRange r.to_NSRange(length), withString: s
69
+ else
70
+ replaceCharactersInRange r.to_NSRange(length), withAttributedString: s
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,36 @@
1
+ ##
2
+ # HotCocoa extensions for the NSString class.
3
+ class NSString
4
+
5
+ ##
6
+ # Borrowed from Active Support.
7
+ def underscore
8
+ new_string = gsub /::/, '/'
9
+ new_string.gsub! /([A-Z]+)([A-Z][a-z])/, '\1_\2'
10
+ new_string.gsub! /([a-z\d])([A-Z])/, '\1_\2'
11
+ new_string.tr! '-', '_'
12
+ new_string.downcase!
13
+ new_string
14
+ end
15
+
16
+ ##
17
+ # Borrowed from Active Support.
18
+ def camel_case
19
+ to_s.gsub /(?:^|_)(.)/ do $1.upcase end
20
+ end
21
+
22
+ ##
23
+ # Create an attributed string with the given attributes. The list of
24
+ # applicable attributes can be found in the Apple documentation for
25
+ # NSAttributedString class, as well as the ATTRIBUTE_KEYS hash in the
26
+ # {HotCocoa::NSRangedProxyAttributeHash} class.
27
+ #
28
+ # @param [Hash] attributes
29
+ # @return [NSMutableAttributedString]
30
+ def with_attributes attributes = {}
31
+ attributed_string = NSMutableAttributedString.alloc.initWithString self
32
+ attributed_string.attributes << attributes
33
+ attributed_string
34
+ end
35
+
36
+ end
@@ -0,0 +1,6 @@
1
+ ##
2
+ # HotCocoa extensions for the NSURL class
3
+ class NSURL
4
+ alias_method :to_s, :absoluteString
5
+ alias_method :inspect, :absoluteString
6
+ end
@@ -0,0 +1,9 @@
1
+ ##
2
+ # HotCocoa extensions for the Numeric class
3
+ class Numeric
4
+ ##
5
+ # Whether or not a number is negative
6
+ def negative?
7
+ self < 0
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##
4
+ # HotCocoa extensions for the Object class
5
+ class Object
6
+
7
+ ##
8
+ # Object.full_const_get was taken from the ‘extlib’ project:
9
+ # http://github.com/sam/extlib which is released under a MIT License and
10
+ # copyrighted by Sam Smoot (2008).
11
+ #
12
+ # Given a string, return the fully qualified constant name found in the
13
+ # receiver's namespace.
14
+ #
15
+ # @example
16
+ #
17
+ # Object.full_const_get('HotCocoa::Mappings') # => HotCocoa::Mappings
18
+ # HotCocoa.full_const_get('Mappings') # => HotCocoa::Mappings
19
+ #
20
+ # @param [String] name the constant to get
21
+ # @return the constant corresponding to the name, if it exists
22
+ def full_const_get name
23
+ list = name.split('::')
24
+ list.shift if list.first.empty?
25
+ obj = self
26
+
27
+ list.each do |x|
28
+ # This is required because const_get tries to look for constants in the
29
+ # ancestor chain, but we only want constants that are HERE
30
+ obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
31
+ end
32
+
33
+ obj
34
+ end
35
+
36
+ end
@@ -0,0 +1,30 @@
1
+ ##
2
+ # HotCocoa extensions to the Range class
3
+ class Range
4
+ ##
5
+ # Create a Cocoa NSRange from a Ruby Range.
6
+ #
7
+ # Since NSRange does not support negative indexing, you _MUST_ include
8
+ # an argument to this method to indicate the length of the object which
9
+ # the range refers to.
10
+ #
11
+ # @example
12
+ #
13
+ # (0..10).to_NSRange # => #<NSRange location=0 length=11>
14
+ # (1..10).to_NSRange # => #<NSRange location=1 length=10>
15
+ # (2..-1).to_NSRange(11) # => #<NSRange location=2 length=9>
16
+ # (3..-2).to_NSRange(11) # => #<NSRange location=3 length=7>
17
+ # (4...-1).to_NSRange(11) # => #<NSRange location=4 length=6>
18
+ # (-3...-1).to_NSRange(11) # => #<NSRange location=8 length=2>
19
+ # (-5..-1).to_NSRange(11) # => #<NSRange location=6 length=5>
20
+ #
21
+ # @param [Number] length the length of the object which the range represents
22
+ def to_NSRange length = nil
23
+ if (first.negative? or last.negative?) and !length
24
+ raise ArgumentError, 'arg required if range has negative indicies'
25
+ end
26
+ start = (first.negative? ? length + first : first)
27
+ run = (last.negative? ? length + last : last ) - start + (exclude_end? ? 0 : 1)
28
+ NSRange.new start, run
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+ require 'hotcocoa/core_extensions/object'
2
+ require 'hotcocoa/core_extensions/kernel'
3
+ require 'hotcocoa/core_extensions/nsurl'
4
+ require 'hotcocoa/core_extensions/nsarray'
5
+ require 'hotcocoa/core_extensions/numeric'
6
+ require 'hotcocoa/core_extensions/range'
7
+ require 'hotcocoa/core_extensions/nsstring'
8
+ require 'hotcocoa/core_extensions/nsmutable_attributed_string'
@@ -1,44 +1,44 @@
1
- module HotCocoa
2
- class ComboBoxDataSource
3
- attr_reader :data
1
+ class HotCocoa::ComboBoxDataSource
4
2
 
5
- def initialize(data)
6
- @data = data
7
- end
8
-
9
- def comboBox(combo_box, completedString:string)
10
- data.length.times do |index|
11
- value = string_value_of_index(index)
12
- return value if value.start_with?(string)
13
- end
14
- nil
15
- end
16
-
17
- def comboBox(combo_box, indexOfItemWithStringValue:string)
18
- data.length.times do |index|
19
- return index if string_value_of_index(index) == string
20
- end
21
- NSNotFound
22
- end
23
-
24
- def comboBox(combo_box, objectValueForItemAtIndex:index)
25
- string_value_of_index(index)
3
+ attr_reader :data
4
+
5
+ def initialize data
6
+ @data = data
7
+ end
8
+
9
+ def comboBox combo_box, completedString: string
10
+ data.length.times do |index|
11
+ value = string_value_of_index index
12
+ return value if value.start_with? string
26
13
  end
27
-
28
- def numberOfItemsInComboBox(combo_box)
29
- data.length
14
+
15
+ nil
16
+ end
17
+
18
+ def comboBox combo_box, indexOfItemWithStringValue: string
19
+ data.length.times do |index|
20
+ return index if string_value_of_index(index) == string
30
21
  end
31
-
32
- private
33
-
34
- def string_value_of_index(i)
35
- item = data[i]
36
- if item.kind_of?(Hash)
37
- item.values.first
38
- else
39
- item.to_s
40
- end
41
- end
42
22
 
23
+ NSNotFound
24
+ end
25
+
26
+ def comboBox combo_box, objectValueForItemAtIndex: index
27
+ string_value_of_index index
28
+ end
29
+
30
+ def numberOfItemsInComboBox combo_box
31
+ data.length
32
+ end
33
+
34
+ private
35
+
36
+ def string_value_of_index i
37
+ item = data[i]
38
+ if item.kind_of? Hash
39
+ item.values.first
40
+ else
41
+ item.to_s
42
+ end
43
43
  end
44
44
  end
@@ -0,0 +1,33 @@
1
+ class HotCocoa::OutlineViewDataSource
2
+
3
+ attr_reader :data
4
+
5
+ def initialize data
6
+ @data = data
7
+ end
8
+
9
+ def outlineView view, numberOfChildrenOfItem: item
10
+ if item.nil?
11
+ data.length
12
+ else
13
+ item[:childRows].length
14
+ end
15
+ end
16
+
17
+ def outlineView view, isItemExpandable: item
18
+ item.has_key?(:childRows) && item[:childRows].length > 0
19
+ end
20
+
21
+ def outlineView view, child: child, ofItem: item
22
+ if item.nil?
23
+ data[child]
24
+ else
25
+ item[:childRows][child]
26
+ end
27
+ end
28
+
29
+ def outlineView view, objectValueForTableColumn: column, byItem: item
30
+ item[column.identifier.intern]
31
+ end
32
+
33
+ end
@@ -1,18 +1,17 @@
1
- module HotCocoa
2
- class TableDataSource
3
- attr_reader :data
1
+ class HotCocoa::TableDataSource
4
2
 
5
- def initialize(data)
6
- @data = data
7
- end
3
+ attr_reader :data
8
4
 
9
- def numberOfRowsInTableView(tableView)
10
- data.length
11
- end
5
+ def initialize data
6
+ @data = data
7
+ end
12
8
 
13
- def tableView(view, objectValueForTableColumn:column, row:i)
14
- data[i][column.identifier.intern]
15
- end
9
+ def numberOfRowsInTableView tableView
10
+ data.length
11
+ end
16
12
 
13
+ def tableView view, objectValueForTableColumn: column, row: i
14
+ data[i][column.identifier.intern]
17
15
  end
18
- end
16
+
17
+ end
@@ -0,0 +1,3 @@
1
+ require 'hotcocoa/data_sources/table_data_source'
2
+ require 'hotcocoa/data_sources/combo_box_data_source'
3
+ require 'hotcocoa/data_sources/outline_view_data_source'