Class: Lebowski::Foundation::ObjectArray

Inherits:
Object
  • Object
show all
Includes:
Lebowski::Foundation
Defined in:
lib/lebowski/foundation/object_array.rb

Overview

Represents an array of proxied objects. Use this for any proxy object that has a property that is an array of objects within the browser. Provides a set of common functionality to check and access proxied objects within the array.

Many of the methods for the class accept a filter as input. The filter can either be a type of SproutCore object or a hash made of properties with values that must be matched. For instance, let’s say that you want to find all the objects that are of the type ‘SC.Foo’. You would do the following:

found = obj.object_array.find_all('SC.Foo')

Only those objects in the array that are of type SC.Foo will be returned. As another example, we want to find all the objects in the array that have a property called “foo” have a matching value of “bar”. You would do the following:

found = obj.object_array.find_all({ :foo => 'bar' })

Only those objects that have the property foo with value ‘bar’ will be returned. Instead of just a string value, you can also provided a regular expression, like so:

found = obj.object_array.find_all({ :foo => /bar/i })

You are not limited to just one key-value pair; you can have as many as you like:

found = object.object_array.find_all({ :foo => /bar/i, :title => 'hello' })

Only objects that satisfy all of the filter’s conditions will be returned.

Constant Summary

Constants included from Lebowski::Foundation

SC_BRANCH_CLOSED, SC_BRANCH_OPEN, SC_BUTTON1_STATUS, SC_BUTTON2_STATUS, SC_BUTTON3_STATUS, SC_LEAF_NODE, SC_MIXED_STATE, SC_PICKER_FIXED, SC_PICKER_MENU, SC_PICKER_POINTER, SC_T_ARRAY, SC_T_BOOL, SC_T_CLASS, SC_T_ERROR, SC_T_FUNCTION, SC_T_HASH, SC_T_NULL, SC_T_NUMBER, SC_T_OBJECT, SC_T_STRING, SC_T_UNDEFINED

Instance Method Summary collapse

Constructor Details

#initialize(parent, array_rel_path, array_length_property_name = nil, *params) ⇒ ObjectArray

Returns a new instance of ObjectArray.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lebowski/foundation/object_array.rb', line 41

def initialize(parent, array_rel_path, array_length_property_name=nil, *params)
  init_validate_parent parent
  
  @parent = parent
  @array_rel_path = array_rel_path
  @array_length_property_name = array_length_property_name.nil? ? 'length' : array_length_property_name
  @driver = parent.driver
  @prefilter = {}
  
  if not params.empty?
    if params[0].kind_of?(Hash) and params[0].has_key?(:prefilter) 
      @prefilter = params[0][:prefilter] 
    else
      @prefilter = params[0]
    end
  end
  
end

Instance Method Details

#[](index, expected_type = nil) ⇒ Object

Returns an item at the given index of the array



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/lebowski/foundation/object_array.rb', line 160

def [](index, expected_type=nil)
  error = ArgumentError.new "index is out of bounds: #{index}" 
  
  if (not index.kind_of? Integer) or index < 0
    raise error
  end
  
  if @prefilter.empty?
    raise error if (index >= count)
    return create_object(index, expected_type)
  else
    indexes = find_indexes
    raise error if (index >= indexes.length)
    return create_object(indexes[index], expected_type)
  end
end

#all?(filter = nil, &block) ⇒ Boolean

Used to check if all items in the array match the given filter

Returns:

  • (Boolean)


290
291
292
# File 'lib/lebowski/foundation/object_array.rb', line 290

def all?(filter=nil, &block)
  return (count(filter, &block) == count)
end

#any?(filter = nil, &block) ⇒ Boolean Also known as: some?

Used to check if any items in the array match the given filter

Returns:

  • (Boolean)


297
298
299
# File 'lib/lebowski/foundation/object_array.rb', line 297

def any?(filter=nil, &block)
  return (count(filter, &block) > 0)
end

#count(filter = nil, &block) ⇒ Object

Returns the number of items in the array. If a filter is provided then the count will be for those items in the array that match the filter



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/lebowski/foundation/object_array.rb', line 142

def count(filter=nil, &block)
  if @prefilter.empty? and filter.nil? and (not block_given?)
    return unfiltered_count
  elsif not block_given?
    return find_indexes(filter).length
  end
  
  counter = 0
  each filter do |view, index|
    result = yield view, index
    counter = counter.next if (result == true)
  end
  return counter
end

#each(filter = nil, &block) ⇒ Object Also known as: each_with_index

Used to iterative through each item in the array. Can filter what items are iterated over by supply a filter object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/lebowski/foundation/object_array.rb', line 195

def each(filter=nil, &block)
  raise ArgumentError.new "block is required" if (not block_given?)

  indexes = []
  if @prefilter.empty? and filter.nil?
    value = count
    return if (value == 0)
    indexes = (0..(value - 1))        
    
    indexes.each do |index| 
      yield create_object(index), index  
    end
  else
    indexes = find_indexes(filter)
    return if indexes.empty?
    
    (0..(indexes.length - 1)).each do |i|
      yield create_object(indexes[i]), i  
    end
  end
end

#empty?Boolean

Used to check if the array is empty

Returns:

  • (Boolean)


320
321
322
# File 'lib/lebowski/foundation/object_array.rb', line 320

def empty?()
  return (count == 0)
end

#filter(filter) ⇒ Object

Used to create a new filtered object array from this array based on this given filter. The new object array will only reference those items that match the given filter



68
69
70
71
# File 'lib/lebowski/foundation/object_array.rb', line 68

def filter(filter)
  merged_filter = merge_filter_with_prefilter(filter)
  return create_filtered_object_array(@parent, @array_rel_path, @array_length_property_name, merged_filter)
end

#find_all(filter = nil, &block) ⇒ Array

Returns all the items matching a given filter. If the filter is nil then all the items are returns. If no mathing items are found then an empty array is returned.

Returns:

  • (Array)

    basic array containing the matching found items



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/lebowski/foundation/object_array.rb', line 226

def find_all(filter=nil, &block)
  if filter.nil? and (not block_given?)
    raise ArugmentError.new "Must provide at least a filter or a block"
  end
  
  collected = []
  each filter do |view, index|
    if block_given?
      result = yield view, index
      collected << result if not result.nil?
    else
      collected << view
    end
  end
  return collected
end

#find_first(filter, expected_type = nil) ⇒ Object

Returns the first item matching the given filter. If no items match then nil is returned



247
248
249
250
251
252
253
254
255
# File 'lib/lebowski/foundation/object_array.rb', line 247

def find_first(filter, expected_type=nil)
  if filter.nil?
    raise ArgumentError.new "filter can not be nil"
  end
  
  indexes = find_indexes(filter)
  return nil if indexes.empty?
  return create_object(indexes[0], expected_type)
end

#find_indexes(filter = nil) ⇒ Object

Returns an enumerable object of indexes for those items in the array that match the given filter

This method gives you a chance to perform any processing on the given filter by overriding the find_indexes_process_filter method. Once indexes are found you can also process the index before a final result is returned by overriding the find_indexes_process_indexes method.

See Also:

  • #find_indexes_process_filter
  • #find_indexes_process_indexes


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/lebowski/foundation/object_array.rb', line 83

def find_indexes(filter=nil)
  raise ArgumentError.new "No filter was supplied" if (filter.nil? and @prefilter.empty?)
  
  # Do some initial processing of the given filter
  if filter.nil?
    # Filter is nil, so make it an empty hash object
    filter = {}
  elsif filter.kind_of? String
    # Filter is just a string, therefore assume the string is an SproutCore type
    filter = { :sc_type => filter }
  elsif filter.kind_of?(Class) and filter.ancestors.member?(SCObject)
    # Filter is an SCObject, therefore get the SC type as a string
    filter = { :sc_type => filter.represented_sc_class }
  elsif filter.kind_of?(Hash)
    # Filter is a hash object. Just need to check if the hash contains the special
    # key :sc_type. If so then do necessary conversions. 
    if filter.has_key? :sc_type
      type = filter[:sc_type]
      if type.kind_of?(Class) and type.ancestors.member?(SCObject)
        filter[:sc_type] = type.represented_sc_class
      end
    end
  else
    raise ArgumentInvalidTypeError.new "filter", filter, 'class < SCObject', String, Hash
  end
  
  # Merge the given filter with this object's prefilter
  filter = merge_filter_with_prefilter(filter)
  
  # Give a chance for the filter to be processed before finding matching indexes
  processed_filter = find_indexes_process_filter(filter)
  raise StandardError.new "process filter can not be nil" if processed_filter.nil?
  
  if not processed_filter.empty?
    # Filter is not empty therefore actually determine what indexes matches the filter
    sc_path = @parent.abs_path_with(@array_rel_path)
    indexes = @driver.get_sc_object_array_index_lookup(sc_path, processed_filter)
    return indexes if indexes.empty?
  else
    # Filter is empty, so just return a range of indexes for this array
    val = unfiltered_count
    return [] if val <= 0 
    indexes = (0..(val - 1))
  end
  
  # Now give a chance for the matching indexes to be processed before returning the
  # final result
  processed_indexes = find_indexes_process_indexes(indexes)
  raise StandardError.new "process indexes can not be nil" if processed_indexes.nil?
  
  # We're done. Return the final result
  return processed_indexes  
  
end

#find_last(filter, expected_type = nil) ⇒ Object

Returns the last item matching a given filter. If no items match then nil is returned.



261
262
263
264
265
266
267
268
269
# File 'lib/lebowski/foundation/object_array.rb', line 261

def find_last(filter, expected_type=nil)
  if filter.nil?
    raise ArgumentError.new "filter can not be nil"
  end
  
  indexes = find_indexes(filter)
  return nil if indexes.empty?
  return create_object(indexes[indexes.length - 1], expected_type)
end

#first(expected_type = nil) ⇒ Object

Returns the first item in the array



180
181
182
# File 'lib/lebowski/foundation/object_array.rb', line 180

def first(expected_type=nil)
  return self[0, expected_type]
end

#index_of(obj) ⇒ Object

Returns the index of the given object



274
275
276
277
278
# File 'lib/lebowski/foundation/object_array.rb', line 274

def index_of(obj)
  return -1 if (not obj.kind_of? ProxyObject)
  indexes = find_indexes({ :sc_guid => obj.sc_guid })
  return indexes.empty? ? -1 : indexes[0]
end

#last(expected_type = nil) ⇒ Object

Returns the last item in the array



187
188
189
# File 'lib/lebowski/foundation/object_array.rb', line 187

def last(expected_type=nil)
  return self[count - 1, expected_type]
end

#member?(obj) ⇒ Boolean

Used to determine if an object is part of this array

Returns:

  • (Boolean)


283
284
285
# File 'lib/lebowski/foundation/object_array.rb', line 283

def member?(obj)
  return (index_of(obj) >= 0)
end

#none?(filter = nil, &block) ⇒ Boolean

Used to check if no items in the array match the given filter

Returns:

  • (Boolean)


306
307
308
# File 'lib/lebowski/foundation/object_array.rb', line 306

def none?(filter=nil, &block)
  return (count(filter, &block) == 0)
end

#one?(filter = nil, &block) ⇒ Boolean

Used to check if only one item in the array matches the given filter

Returns:

  • (Boolean)


313
314
315
# File 'lib/lebowski/foundation/object_array.rb', line 313

def one?(filter=nil, &block)
  return (count(filter, &block) == 1)
end

#prefilterObject



60
61
62
# File 'lib/lebowski/foundation/object_array.rb', line 60

def prefilter()
  return @prefilter.clone
end