Class: ISBNdb::AccessKeySet

Inherits:
Object
  • Object
show all
Defined in:
lib/isbndb/access_key_set.rb

Overview

The AccessKeySet is a simple class used to manage access keys. It is used primarily by the ruby_isbndb class to automatically advance access keys when necessary.

Instance Method Summary collapse

Constructor Details

#initializeAccessKeySet

Create the @access_keys array and then verify that the keys are valid keys.



7
8
9
# File 'lib/isbndb/access_key_set.rb', line 7

def initialize
  @access_keys ||= YAML::load(File.open('config/isbndb.yml'))['access_keys']
end

Instance Method Details

#current_indexObject



16
17
18
# File 'lib/isbndb/access_key_set.rb', line 16

def current_index
  @current_index ||= 0
end

#current_keyObject

Get the current key. It returns a string of the access key.



21
22
23
# File 'lib/isbndb/access_key_set.rb', line 21

def current_key
  @access_keys[current_index]
end

#next_keyObject

Get the next key.



32
33
34
# File 'lib/isbndb/access_key_set.rb', line 32

def next_key
  @access_keys[current_index+1]
end

#next_key!Object

Move the key pointer forward.



26
27
28
29
# File 'lib/isbndb/access_key_set.rb', line 26

def next_key!
  @current_index = current_index + 1
  current_key
end

#prev_keyObject

Get the previous key.



43
44
45
# File 'lib/isbndb/access_key_set.rb', line 43

def prev_key
  @access_keys[current_index-1]
end

#prev_key!Object

Move the key pointer back.



37
38
39
40
# File 'lib/isbndb/access_key_set.rb', line 37

def prev_key!
  @current_index = current_index - 1
  current_key
end

#remove_key(key) ⇒ Object

Remove the given access key from the AccessKeySet.



55
56
57
# File 'lib/isbndb/access_key_set.rb', line 55

def remove_key(key)
  @access_keys.delete(key)
end

#sizeObject

Returns the total number of access keys in this set.



12
13
14
# File 'lib/isbndb/access_key_set.rb', line 12

def size
  @access_keys.size
end

#to_sObject

Pretty print the AccessKeySet



60
61
62
# File 'lib/isbndb/access_key_set.rb', line 60

def to_s
  "#<AccessKeySet @keys=#{@access_keys.inspect}>"
end

#use_key(key) ⇒ Object

Tell Ruby ISBNdb to use a specified key. If the key does not exist, it is added to the set and set as the current key.



49
50
51
52
# File 'lib/isbndb/access_key_set.rb', line 49

def use_key(key)
  @current_index = @access_keys.index(key) || @access_keys.push(key).index(key)
  current_key
end