Class: KeyVortex
- Inherits:
-
Object
- Object
- KeyVortex
- 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
- #adapter ⇒ Adapter readonly
-
#record_class ⇒ Class
readonly
Subclass of Record.
Class Method Summary collapse
-
.register(adapter_class) ⇒ Object
Register an adapter class so that KeyVortex.vortex knows about it.
- .vortex(adapter_symbol, record_class, **options) ⇒ KeyVortex
Instance Method Summary collapse
-
#find(key) ⇒ Record?
Retrieve a record that has had #save called on it.
-
#initialize(adapter, record_class) ⇒ KeyVortex
constructor
While you are able to create the object directly, it’s easier to do so through KeyVortex.vortex.
-
#remove(key) ⇒ Object
Remove the Record with Record#key set to key from the #adapter.
-
#save(record) ⇒ Object
Add the record to the #adapter.
Constructor Details
#initialize(adapter, record_class) ⇒ KeyVortex
While you are able to create the object directly, it’s easier to do so through vortex.
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
#record_class ⇒ Class (readonly)
Returns 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.
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.
28 29 30 31 32 33 |
# File 'lib/key_vortex.rb', line 28 def self.vortex(adapter_symbol, record_class, **) new( @adapter_registry[adapter_symbol].build(**), record_class ) end |
Instance Method Details
#find(key) ⇒ Record?
Retrieve a record that has had #save called on it.
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.
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.
62 63 64 |
# File 'lib/key_vortex.rb', line 62 def save(record) @adapter.save(record) end |