Module: HotCocoa::MappingMethods
- Defined in:
- lib/hotcocoa/mapping_methods.rb
Overview
The set of methods that are available when creating a mapping.
Instance Attribute Summary collapse
-
#constants_map ⇒ Hash{Symbol=>Hash{Symbol=>Constant}}
readonly
A mapping of constant mappings that were created with calls to #constant.
-
#delegate_map ⇒ Hash{Symbol=>Hash{Symbol=>SEL}}
readonly
A mapping of delegate mappings that were created with calls to #delegating.
Class Method Summary collapse
-
.extended(klass) ⇒ Object
A small hack so that we can have #delegate_map and #constants as attributes instead of methods that memoize instance variables.
Instance Method Summary collapse
-
#constant(name, constants) ⇒ Object
Create a mapping of a constant type to an enumeration of constants.
-
#custom_methods { ... } ⇒ Object
Custom methods are modules that are mixed into the class being mapped; they provide idiomatic Ruby methods for the mapped Objective-C class instances.
-
#defaults(defaults = nil) ⇒ Object
You can provide a hash of default options in the definition of your mapping.
-
#delegating(name, options) ⇒ Object
Delegation is a pattern that is used pervasively in Cocoa to facilitate customization of controls; it is a powerful tool, but is a little more complex to setup than custom methods.
Instance Attribute Details
#constants_map ⇒ Hash{Symbol=>Hash{Symbol=>Constant}} (readonly)
A mapping of constant mappings that were created with calls to #constant
51 52 53 |
# File 'lib/hotcocoa/mapping_methods.rb', line 51 def constants_map @constants_map end |
#delegate_map ⇒ Hash{Symbol=>Hash{Symbol=>SEL}} (readonly)
A mapping of delegate mappings that were created with calls to #delegating
100 101 102 |
# File 'lib/hotcocoa/mapping_methods.rb', line 100 def delegate_map @delegate_map end |
Class Method Details
.extended(klass) ⇒ Object
A small hack so that we can have #delegate_map and #constants as attributes instead of methods that memoize instance variables.
105 106 107 108 |
# File 'lib/hotcocoa/mapping_methods.rb', line 105 def self.extended klass klass.instance_variable_set :@constants_map, {} klass.instance_variable_set :@delegate_map, {} end |
Instance Method Details
#constant(name, constants) ⇒ Object
Create a mapping of a constant type to an enumeration of constants.
A constant mapping allows the use of short symbol names to be used in place of long constant names in the scope of the wrapped class.
Details about using this method are in the Mappings tutorial.
42 43 44 |
# File 'lib/hotcocoa/mapping_methods.rb', line 42 def constant name, constants constants_map[name] = constants end |
#custom_methods(do ... end) ⇒ Module #custom_methods ⇒ Module?
Custom methods are modules that are mixed into the class being mapped; they provide idiomatic Ruby methods for the mapped Objective-C class instances.
Custom methods are meant to be used in conjunction with constant mappings or when the custom method provides something much better than what is offered by plain Cocoa. Examples are available in the Mappings tutorial.
71 72 73 74 75 76 77 |
# File 'lib/hotcocoa/mapping_methods.rb', line 71 def custom_methods &block if block @custom_methods = Module.new &block else @custom_methods end end |
#defaults ⇒ Hash? #defaults(key1: value1, key2: value2, ...) ⇒ Hash
You can provide a hash of default options in the definition of your mapping. This is very useful for many Cocoa classes, because there are so many options to set at initialization.
Details about how defaults are used can be found in the Mappings tutorial.
23 24 25 26 27 28 29 |
# File 'lib/hotcocoa/mapping_methods.rb', line 23 def defaults defaults = nil if defaults @defaults = defaults else @defaults end end |
#delegating(name, options) ⇒ Object
Delegation is a pattern that is used pervasively in Cocoa to facilitate customization of controls; it is a powerful tool, but is a little more complex to setup than custom methods.
You should read the Mappings tutorial to get an in depth understanding on how to setup delegates in HotCocoa.
91 92 93 |
# File 'lib/hotcocoa/mapping_methods.rb', line 91 def delegating name, delegate_map[name] = end |