Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/ygg/base.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(x, *args) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/ygg/base.rb', line 193

def method_missing(x, *args)
  if matches = x.to_s.match(/^sort_by_(.+)$/)
    key = matches[1].to_sym
    self.sort_by { |x| x.send(key) }

  elsif matches = x.to_s.match(/^by_(.+)$/) # Extend with regexps
    key = matches[1].to_sym
    
    if to_find = args.shift
      result = []
      self.each do |res|
        if res.respond_to? key
          datus = res.send key
          result << res if datus == to_find
        end
      end
      return []       if result.length == 0
      return result[0]   if result.length == 1
      return result
    
    else 
      self.send(:"sort_by_#{key}")[0] unless empty?
    end
    
    return []
  end
end