Class: Cassie::Testing::Fake::Result
- Includes:
- Enumerable
- Defined in:
- lib/cassie/testing/fake/result.rb
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#statement ⇒ Object
readonly
Returns the value of attribute statement.
Instance Method Summary collapse
- #current_page ⇒ Object
- #each ⇒ Object (also: #rows, #each_row)
- #empty? ⇒ Boolean
- #execution_info ⇒ Object
-
#initialize(statement, execution_opts = {}) ⇒ Result
constructor
A new instance of Result.
- #last_page? ⇒ Boolean
- #page_size ⇒ Object
- #pages ⇒ Object
- #paging_enabled? ⇒ Boolean
- #paging_state ⇒ Object
- #previous_page ⇒ Object
- #previous_paging_state ⇒ Object
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
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
6 7 8 |
# File 'lib/cassie/testing/fake/result.rb', line 6 def opts @opts end |
#statement ⇒ Object (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_page ⇒ Object
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 |
#each ⇒ Object 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
37 38 39 40 41 |
# File 'lib/cassie/testing/fake/result.rb', line 37 def empty? !rows.peek rescue StopIteration true end |
#execution_info ⇒ Object
14 15 16 |
# File 'lib/cassie/testing/fake/result.rb', line 14 def execution_info ExecutionInfo.new(statement) end |
#last_page? ⇒ 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_size ⇒ Object
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 |
#pages ⇒ Object
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
43 44 45 |
# File 'lib/cassie/testing/fake/result.rb', line 43 def paging_enabled? !!page_size end |
#paging_state ⇒ Object
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_page ⇒ Object
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_state ⇒ Object
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 |