Class: KeyVortex

Inherits:
Object
  • Object
show all
Defined in:
lib/key_vortex.rb,
lib/key_vortex/field.rb,
lib/key_vortex/record.rb,
lib/key_vortex/adapter.rb,
lib/key_vortex/version.rb,
lib/key_vortex/constraint.rb,
lib/key_vortex/limitation.rb,
lib/key_vortex/adapter/memory.rb,
lib/key_vortex/constraint/base.rb,
lib/key_vortex/constraint/length.rb,
lib/key_vortex/constraint/regexp.rb,
lib/key_vortex/constraint/maximum.rb,
lib/key_vortex/constraint/minimum.rb

Overview

Defines the API you’ll interact with when using this gem. Much of the functionality is delegated to other classes.

Defined Under Namespace

Modules: Constraint Classes: Adapter, Error, Field, Limitation, Record

Constant Summary collapse

VERSION =
"1.1.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, record_class) ⇒ KeyVortex

While you are able to create the object directly, it’s easier to do so through vortex.

Parameters:

  • adapter (Adapter)

    The backend that will be used to save and retrieve records

  • record_class (Class)

    The subclass of Record which will be saved and retreived

Raises:

  • (Error)

    If the constraints of the record_class do not fit within the constraints of the adapter



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/key_vortex.rb', line 45

def initialize(adapter, record_class)
  @adapter = adapter
  @record_class = record_class

  record_class.fields.each do |field|
    next if field.within?(adapter)

    raise KeyVortex::Error,
          "#{adapter.class} can only handle field #{field.name} with these limitations:\n" +
          adapter.limitation_for(field).to_s +
          "\n\nThe following record violates these limitations:\n#{field.limitation}"
  end
end

Instance Attribute Details

#adapterAdapter (readonly)

Returns:



36
37
38
# File 'lib/key_vortex.rb', line 36

def adapter
  @adapter
end

#record_classClass (readonly)

Returns Subclass of Record.

Returns:

  • (Class)

    Subclass of Record



38
39
40
# File 'lib/key_vortex.rb', line 38

def record_class
  @record_class
end

Class Method Details

.register(adapter_class) ⇒ Object

Register an adapter class so that vortex knows about it.

Parameters:

  • adapter_class (Class)


13
14
15
16
# File 'lib/key_vortex.rb', line 13

def self.register(adapter_class)
  @adapter_registry ||= {}
  @adapter_registry[adapter_class.symbol] = adapter_class
end

.vortex(adapter_symbol, record_class, **options) ⇒ KeyVortex

Creates an instance of KeyVortex with an appropriate Adapter. An adapter class must have been associated with the adapter_symbol by calling register, this should have been done by the file which defined that class. So, when you require “key_vortex/adapter/memory” the :memory symbol will be defined for use here.

Parameters:

  • adapter_symbol (Symbol)
  • record_class (Class)

    The class must inherit from Record

  • options (Hash)

    Options that will be passed through to .build on the adapter

Returns:



28
29
30
31
32
33
# File 'lib/key_vortex.rb', line 28

def self.vortex(adapter_symbol, record_class, **options)
  new(
    @adapter_registry[adapter_symbol].build(**options),
    record_class
  )
end

Instance Method Details

#find(key) ⇒ Record?

Retrieve a record that has had #save called on it.

Parameters:

  • key (String)

Returns:



70
71
72
# File 'lib/key_vortex.rb', line 70

def find(key)
  @adapter.find(key)
end

#remove(key) ⇒ Object

Remove the Record with KeyVortex::Record#key set to key from the #adapter.

Parameters:

  • key (String)


77
78
79
# File 'lib/key_vortex.rb', line 77

def remove(key)
  @adapter.remove(key)
end

#save(record) ⇒ Object

Add the record to the #adapter. Once this is done, #find will return this record when passed the KeyVortex::Record#key.

Parameters:



62
63
64
# File 'lib/key_vortex.rb', line 62

def save(record)
  @adapter.save(record)
end