Class: Flock::Operation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flock/operation.rb

Instance Method Summary collapse

Constructor Details

#initialize(count = 20, cursor = Flock::CursorStart, &block) ⇒ Operation

Returns a new instance of Operation.



5
6
7
8
9
# File 'lib/flock/operation.rb', line 5

def initialize(count = 20, cursor = Flock::CursorStart, &block)
  @count, @next_cursor = count, cursor
  @get_results = block
  raise if @get_results.nil?
end

Instance Method Details

#current_pageObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flock/operation.rb', line 16

def current_page
  @current_page ||= begin
    current_page, @next_cursor, @prev_cursor = (memo[@next_cursor] ||= begin
      page = Flock::Page.new
      page.cursor = @next_cursor
      page.count = @count
      get_results(page)
    end)
    current_page.dup
  end
end

#eachObject



61
62
63
64
65
66
67
68
# File 'lib/flock/operation.rb', line 61

def each
  while next?
    yield(self.next)
  end
  self
ensure
  reset
end

#firstObject



57
58
59
# File 'lib/flock/operation.rb', line 57

def first
  current_page.first
end

#get_results(page) ⇒ Object



47
48
49
# File 'lib/flock/operation.rb', line 47

def get_results(page)
  @get_results.call(page)
end

#memoObject



28
29
30
# File 'lib/flock/operation.rb', line 28

def memo
  @memo ||= {}
end

#nextObject



84
85
86
87
88
# File 'lib/flock/operation.rb', line 84

def next
  raise unless next?
  item = current_page.shift
  item || next_page && self.next
end

#next?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/flock/operation.rb', line 76

def next?
  current_page.any? || next_page?
end

#next_pageObject



90
91
92
93
94
95
96
# File 'lib/flock/operation.rb', line 90

def next_page
  raise unless next_page?

  @cursor = @next_cursor
  @current_page = nil
  current_page
end

#next_page?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/flock/operation.rb', line 112

def next_page?
  @next_cursor != Flock::CursorEnd
end

#paginate(count = 20, cursor = Flock::CursorStart) ⇒ Object



11
12
13
14
# File 'lib/flock/operation.rb', line 11

def paginate(count = 20, cursor = Flock::CursorStart)
  cursor ||= Flock::CursorStart
  Flock::Operation.new(count, cursor, &@get_results)
end

#prev?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/flock/operation.rb', line 80

def prev?
  refuse.any? || prev_page?
end

#prev_pageObject Also known as: previous_page



98
99
100
101
102
103
104
# File 'lib/flock/operation.rb', line 98

def prev_page
  raise unless prev_page?

  @cursor = @prev_cursor
  @current_page = nil
  current_page
end

#prev_page?Boolean Also known as: previous_page?

Returns:

  • (Boolean)


107
108
109
# File 'lib/flock/operation.rb', line 107

def prev_page?
  @prev_cursor != Flock::CursorEnd
end

#resetObject



70
71
72
73
74
# File 'lib/flock/operation.rb', line 70

def reset
  @current_page = nil
  @prev_cursor = Flock::CursorEnd
  @next_cursor = Flock::CursorStart
end

#set_current_page(cursor, results) ⇒ Object



32
33
34
35
36
# File 'lib/flock/operation.rb', line 32

def set_current_page(cursor, results)
  current_page, @next_cursor, @prev_cursor = results
  @current_page = current_page.dup
  memo[cursor] = results
end

#take(count) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/flock/operation.rb', line 38

def take(count)
  result = []
  each_with_index do |item, i|
    break if i == count
    result << item
  end
  result
end

#unapplyObject



51
52
53
# File 'lib/flock/operation.rb', line 51

def unapply
  [current_page, @next_cursor, @prev_cursor]
end