Module: Persistence::Adapter::Mock::Cursor::CursorInterface

Included in:
Persistence::Adapter::Mock::Cursor
Defined in:
lib/persistence/adapter/mock/cursor/cursor_interface.rb

Overview

Interface implementation for Mock adapter Cursor class instances.

Instance Method Summary collapse

Instance Method Details

#closeObject

Declare use of cursor complete.



55
56
57
58
59
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 55

def close
  
  # nothing required

end

#currentObject

Return the current object in cursor’s current context.

Returns:

  • (Object)

    Current object in cursor’s current context.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 167

def current

  # current should return the current ID or object hash

  current_id = nil

  if @current_item
    if @index
      current_id = @current_item[ 1 ]
    else
      current_id = @current_item[ 0 ]
    end
  end

  return current_id

end

#current_keyObject

Return the current key in cursor’s current context.

Returns:

  • (Object)

    Current object in cursor’s current context.



194
195
196
197
198
199
200
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 194

def current_key
  current_key = nil
  if @current_item
    current_key = @current_item[ 0 ]
  end
  return current_key
end

#firstObject

Return the first object in cursor’s current context.

Responsible for setting the cursor position.

Returns:

  • (Object)

    First object in cursor’s current context.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 125

def first

  # first should set the cursor position and return the first ID or object hash

  begin
    @current_item = @current_position.rewind.peek
  rescue StopIteration
    @current_item = nil
  end

  return current

end

#first_keyObject

Return the first key in cursor’s current context.

Responsible for setting the cursor position.

Returns:

  • (Object)

    First key in cursor’s current context.



149
150
151
152
153
154
155
156
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 149

def first_key
  begin
    @current_item = @current_position.rewind.peek
  rescue StopIteration
    @current_item = nil
  end
  return current_key
end

#initialize(bucket, index) ⇒ Object

Parameters:

  • bucket

    Cursor will open on bucket instance.

  • index

    Cursor will open on index instance.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 16

def initialize( bucket, index )

  @persistence_bucket = bucket
  @index = index

  @id_source = index  ? index.instance_variable_get( :@keys )
                      : bucket.instance_variable_get( :@objects )
  
  @current_position = ( @id_source || { } ).each

end

#nextObject

Return the next object in cursor’s current context.

Returns:

  • (Object)

    Next object in cursor’s current context.



211
212
213
214
215
216
217
218
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 211

def next
  begin
    @current_item = @current_position.next
  rescue StopIteration
    @current_item = nil
  end
  return current
end

#next_keyObject

Return the next key in cursor’s current context.

Returns:

  • (Object)

    Next key in cursor’s current context.



229
230
231
232
233
234
235
236
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 229

def next_key
  begin
    @current_item = @current_position.next
  rescue StopIteration
    @current_item = nil
  end
  return current_key
end

#persisted?(key, ...) ⇒ true, false

Query whether keys are persisted in cursor’s current context.

Responsible for setting the cursor position.

Parameters:

  • Key (Object)

    to look up.

Returns:

  • (true, false)

    Whether key(s) exist in cursor’s current context.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 75

def persisted?( *args )

  # persisted? is responsible for setting the cursor position

  key = nil
  # we expect 0 or 1 arg
  if args.count > 0
    key = args[ 0 ]
  else
    # if we have no key we are asking if keys exist
    @current_position.rewind

    return @id_source.count > 0 ? true : false
  end

  has_key = false

  # rewind to beginning of bucket
  @current_position.rewind

  # iterate until we find our key
  begin
    
    while @current_item = @current_position.next
      if @current_item[ 0 ] == key
        has_key = true
        break
      end
    end
    
  rescue StopIteration

    @current_item = nil

  end

  return has_key

end

#supports_bucket_order?Boolean

supports_bucket_order? #

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 32

def supports_bucket_order?
  
  return true
  
end

#supports_index_order?Boolean

supports_index_order? #

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/persistence/adapter/mock/cursor/cursor_interface.rb', line 42

def supports_index_order?
  
  return true

end