Class: Cassie::Testing::Fake::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cassie/testing/fake/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statement, execution_opts = {}) ⇒ Result

Returns a new instance of Result.



8
9
10
11
12
# File 'lib/cassie/testing/fake/result.rb', line 8

def initialize(statement, execution_opts={})
  @statement = statement
  @opts = execution_opts
  @rows = @data = opts[:rows] || []
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/cassie/testing/fake/result.rb', line 6

def opts
  @opts
end

#statementObject (readonly)

Returns the value of attribute statement.



6
7
8
# File 'lib/cassie/testing/fake/result.rb', line 6

def statement
  @statement
end

Instance Method Details

#current_pageObject



62
63
64
65
# File 'lib/cassie/testing/fake/result.rb', line 62

def current_page
  return 1 unless paging_enabled? && previous_page
  previous_page + 1
end

#eachObject Also known as: rows, each_row



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cassie/testing/fake/result.rb', line 18

def each
  result_rows = if paging_enabled?
    index = current_page - 1
    offset = index * page_size
    @data.slice(offset, page_size) || []
  else
    @data
  end

  if block_given?
    result_rows.each(&block)
    self
  else
    result_rows.each
  end
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/cassie/testing/fake/result.rb', line 37

def empty?
  !!rows.peek
rescue StopIteration
  true
end

#execution_infoObject



14
15
16
# File 'lib/cassie/testing/fake/result.rb', line 14

def execution_info
  ExecutionInfo.new(statement)
end

#last_page?Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/cassie/testing/fake/result.rb', line 67

def last_page?
  return nil unless paging_enabled?
  current_page == pages
end

#page_sizeObject



47
48
49
50
# File 'lib/cassie/testing/fake/result.rb', line 47

def page_size
  return nil unless opts[:page_size]
  opts[:page_size].to_i
end

#pagesObject



52
53
54
55
# File 'lib/cassie/testing/fake/result.rb', line 52

def pages
  return nil unless paging_enabled?
  (@data.count / page_size.to_f).ceil
end

#paging_enabled?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cassie/testing/fake/result.rb', line 43

def paging_enabled?
  !!page_size
end

#paging_stateObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cassie/testing/fake/result.rb', line 77

def paging_state
  return nil unless paging_enabled?
  if previous_paging_state
    bytes = previous_paging_state.bytes
    raise 'Too many pages for Cassie testing harness!' if bytes[-1] >= 256
    bytes[-1] = bytes[-1] + 1
    bytes.pack('c*')
  else
    # use last byte of state string to store pages
    # presume 255 pages is enough for any testing
    "paging #{SecureRandom.hex(6)}:" + [1].pack('c*')
  end
end

#previous_pageObject



57
58
59
60
# File 'lib/cassie/testing/fake/result.rb', line 57

def previous_page
  return nil unless previous_paging_state
  previous_paging_state.bytes[-1]
end

#previous_paging_stateObject



72
73
74
75
# File 'lib/cassie/testing/fake/result.rb', line 72

def previous_paging_state
  return nil unless paging_enabled?
  @opts[:paging_state]
end