Class: Nevermind::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/nevermind/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(first, second) ⇒ Proxy

include Enumerable



5
6
7
8
9
10
11
# File 'lib/nevermind/proxy.rb', line 5

def initialize(first, second)
  @first, @second = first, second

  @order_params = nil
  @limit_params = nil
  @offset_params = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nevermind/proxy.rb', line 57

def method_missing(method, *args, &block)
  if [:all, :where, :order].include? method
    call_relation_method(method, *args, &block)
  else
    if '!' == method.to_s.last
      call_forced_method(method, *args, &block)
    else
      call_regular_method(method, *args, &block)
    end
  end
end

Instance Method Details

#[](index) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/nevermind/proxy.rb', line 22

def [](index)
  #todo: optimize!
  count = 0
  self.each do |obj|
    return obj if count == index
    count+= 1
  end
  return nil
end

#countObject



32
33
34
35
36
37
38
39
40
# File 'lib/nevermind/proxy.rb', line 32

def count
  if @order_params || @limit_params || @offset_params
    count = 0
    get_scoped { |val| count+= 1 }
    return count
  else
    @first.count + @second.count
  end
end

#each(&block) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/nevermind/proxy.rb', line 13

def each(&block)
  if @order_params || @limit_params || @offset_params
    get_scoped &block
  else
    @first.each { |obj| yield obj }
    @second.each { |obj| yield obj }
  end
end

#limit(*args) ⇒ Object



47
48
49
50
# File 'lib/nevermind/proxy.rb', line 47

def limit(*args)
  @limit_params = *args
  return self.clone
end

#offset(*args) ⇒ Object



52
53
54
55
# File 'lib/nevermind/proxy.rb', line 52

def offset(*args)
  @offset_params = *args
  return self.clone
end

#order(*args) ⇒ Object



42
43
44
45
# File 'lib/nevermind/proxy.rb', line 42

def order(*args)
  @order_params = *args
  return self.clone
end