Module: Accessibility::NSArrayCompat

Included in:
NSArray
Defined in:
lib/ax_elements/nsarray_compat.rb

Overview

An old hack on arrays that allows you to map a single method across an array of AX::Element objects more succinctly than Symbol#to_proc.

I've always been on the fence as to whether this was a good idea or not, but at this point there is too much code written that depends on this and so I will just keep it around for backwards compatability.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Note:

Debatably bad idea. Maintained for backwards compatibility.

If the array contains AX::Element objects and the elements respond to the method name then the method will be mapped across the array. In this case, you can artificially pluralize the attribute name and the lookup will singularize the method name for you.

Examples:


rows   = outline.rows      # :rows is already an attribute # edge case
fields = rows.text_fields  # you want the AX::TextField from each row
fields.values              # grab the values

outline.rows.text_fields.values # all at once


42
43
44
45
46
47
48
49
50
# File 'lib/ax_elements/nsarray_compat.rb', line 42

def method_missing method, *args
  smethod = TRANSLATOR.singularize(method.chomp('?'))
  map do |x|
    if    !x.kind_of? AX::Element then super
    elsif  x.respond_to? method   then x.send method,  *args
    else                               x.send smethod, *args
    end
  end
end

Instance Method Details

#secondObject

Equivalent to #at(1)



16
17
18
# File 'lib/ax_elements/nsarray_compat.rb', line 16

def second
  at(1)
end

#thirdObject

Equivalent to #at(2)



22
23
24
# File 'lib/ax_elements/nsarray_compat.rb', line 22

def third
  at(2)
end