Class: Ygg::Branch

Inherits:
Array
  • Object
show all
Defined in:
lib/ygg/bck/branch.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(x, *args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ygg/bck/branch.rb', line 3

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 = Branch.new
      self.each do |res|
        if res.respond_to? key
          datus = res.send key
          result << res if datus == to_find
        end
      end
      return Branch.new       if result.length == 0
      return result[0]   if result.length == 1
      return result
    
    else 
      self.send(:"sort_by_#{key}")[0] unless empty?
    end
    
    return Branch.new
  end
end