Class: Ambry::Adapter
- Inherits:
-
Object
- Object
- Ambry::Adapter
- Defined in:
- lib/ambry/adapter.rb
Overview
Adapters are responsible for persisting the database. This base adapter offers no persistence, all IO operations are just stubs. Adapters must also present the full database as a Hash to the mapper, and provide a ‘key` method that returns an array with all the keys for the specified model class.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#read_only ⇒ Object
Returns the value of attribute read_only.
Instance Method Summary collapse
-
#db_for(klass) ⇒ Object
Get a hash of all the data for the specified model class.
-
#export_data ⇒ Object
Inheriting adapters can overload this method to export the data to a String.
-
#import_data ⇒ Object
Inheriting adapters can overload this method to load the data from some kind of storage.
-
#initialize(options = {}) ⇒ Adapter
constructor
A new instance of Adapter.
-
#load_database ⇒ Object
Loads the database.
-
#read_only? ⇒ Boolean
Is the adapter read only? If so, attempts to write data will raise an AmbryError.
-
#save_database ⇒ Object
Inheriting adapters can overload this method to persist the data to some kind of storage.
Constructor Details
#initialize(options = {}) ⇒ Adapter
Returns a new instance of Adapter.
15 16 17 18 19 20 |
# File 'lib/ambry/adapter.rb', line 15 def initialize( = {}) @name = [:name] || Ambry.default_adapter_name @read_only = false load_database Ambry.register_adapter(self) end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
11 12 13 |
# File 'lib/ambry/adapter.rb', line 11 def db @db end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/ambry/adapter.rb', line 10 def name @name end |
#read_only ⇒ Object
Returns the value of attribute read_only.
12 13 14 |
# File 'lib/ambry/adapter.rb', line 12 def read_only @read_only end |
Instance Method Details
#db_for(klass) ⇒ Object
Get a hash of all the data for the specified model class.
24 25 26 |
# File 'lib/ambry/adapter.rb', line 24 def db_for(klass) @db[klass.to_s] ||= {} end |
#export_data ⇒ Object
Inheriting adapters can overload this method to export the data to a String.
39 40 41 |
# File 'lib/ambry/adapter.rb', line 39 def export_data true end |
#import_data ⇒ Object
Inheriting adapters can overload this method to load the data from some kind of storage.
45 46 47 |
# File 'lib/ambry/adapter.rb', line 45 def import_data true end |
#load_database ⇒ Object
Loads the database. For this adapter, that means simply creating a new hash.
30 31 32 |
# File 'lib/ambry/adapter.rb', line 30 def load_database @db = {} end |
#read_only? ⇒ Boolean
Is the adapter read only? If so, attempts to write data will raise an AmbryError.
51 52 53 |
# File 'lib/ambry/adapter.rb', line 51 def read_only? @read_only end |
#save_database ⇒ Object
Inheriting adapters can overload this method to persist the data to some kind of storage.
57 58 59 60 |
# File 'lib/ambry/adapter.rb', line 57 def save_database raise AmbryError if read_only? true end |