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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constants_mapHash{Symbol=>Hash{Symbol=>Constant}} (readonly)

A mapping of constant mappings that were created with calls to #constant

Returns:

  • (Hash{Symbol=>Hash{Symbol=>Constant}})


51
52
53
# File 'lib/hotcocoa/mapping_methods.rb', line 51

def constants_map
  @constants_map
end

#delegate_mapHash{Symbol=>Hash{Symbol=>SEL}} (readonly)

A mapping of delegate mappings that were created with calls to #delegating

Returns:

  • (Hash{Symbol=>Hash{Symbol=>SEL}})


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.

Parameters:

  • name (Symbol)
  • constants (Hash{Symbol=>Constant})


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_methodsModule?

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.

Overloads:

  • #custom_methods(do ... end) ⇒ Module

    Create and cache a new module to mix into the mapped class

    Returns:

    • (Module)

      return the module that caches the custom methods

  • #custom_methodsModule?

    Return the Module if it exists, otherwise nil

    Returns:

    • (Module, nil)

      return the Module if it exists, otherwise nil

Yields:

  • A block that will be evaluated in the context of a new module



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

#defaultsHash? #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.

Overloads:

  • #defaultsHash?

    Get the hash of defaults

    Returns:

    • (Hash, nil)
  • #defaults(key1: value1, key2: value2, ...) ⇒ Hash

    Set the hash of defaults

    Parameters:

    • (Hash)

    Returns:

    • (Hash)


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.

Parameters:

  • name (String, Symbol)
  • options (Hash{:to=>:ruby_name, :parameters=>Array<String>})

    the :to key must be included, but :parameters is optional



91
92
93
# File 'lib/hotcocoa/mapping_methods.rb', line 91

def delegating name, options
  delegate_map[name] = options
end