Class: MemoryModel::Collection::Index

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/memory_model/collection/index.rb,
lib/memory_model/collection/index/multi.rb,
lib/memory_model/collection/index/unique.rb

Direct Known Subclasses

Multi, Unique

Defined Under Namespace

Classes: Multi, Unique

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Index

Returns a new instance of Index.



35
36
37
38
39
# File 'lib/memory_model/collection/index.rb', line 35

def initialize(name, options)
  @name    = name
  @options = options
  @index   = {}
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



30
31
32
# File 'lib/memory_model/collection/index.rb', line 30

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/memory_model/collection/index.rb', line 30

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/memory_model/collection/index.rb', line 30

def options
  @options
end

Instance Method Details

#create(key, item) ⇒ Object

‘create` should implement creating a new record, raising an error if an item with the matching storage id already exists in the index.

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/memory_model/collection/index.rb', line 45

def create(key, item)
  raise NotImplementedError, "#{__method__} has not been implemented for this #{name} index"
end

#delete(key) ⇒ Object

‘delete` should find a record in the collection by its indexed_value, and remove it.

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/memory_model/collection/index.rb', line 60

def delete(key)
  raise NotImplementedError, "#{__method__} has not been implemented for this #{name} index"
end

#exists?(item) ⇒ Boolean

‘exists?` return whether or not an item with the given storage id exists.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/memory_model/collection/index.rb', line 65

def exists?(item)
  raise NotImplementedError, "#{__method__} has not been implemented for this #{name} index"
end

#read(key) ⇒ Object

‘read` should find a record in the collection by its indexed_value, remove it, and add with the new value.

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/memory_model/collection/index.rb', line 55

def read(key)
  raise NotImplementedError, "#{__method__} has not been implemented for this #{name} index"
end

#update(key, item) ⇒ Object

‘update` should find a record in the collection by its storage_id, remove it, and add with the new value.

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/memory_model/collection/index.rb', line 50

def update(key, item)
  raise NotImplementedError, "#{__method__} has not been implemented for this #{name} index"
end

#valuesObject

‘values` should return the values of the index

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/memory_model/collection/index.rb', line 79

def values
  raise NotImplementedError, "#{__method__} has not been implemented for this #{name} index"
end

#where(matcher) ⇒ Object

‘where` should allow me to specify complex arguments and return an array



70
71
72
73
74
75
76
# File 'lib/memory_model/collection/index.rb', line 70

def where(matcher)
  matcher_class = matcher.class.name.underscore
  send("where_using_#{matcher_class}", matcher)
rescue NoMethodError
  respond_to?(:where_using_default, true) ? where_using_default(matcher) :
    raise(InvalidWhereQuery, matcher_class)
end