Class: Ambry::Adapter

Inherits:
Object
  • Object
show all
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

Ambry::Adapters::File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Adapter

Returns a new instance of Adapter

Options Hash (options):

  • :name (String)

    The adapter name. Defaults to Ambry::Adapter#Ambry#Ambry.default_adapter_name.


15
16
17
18
19
20
# File 'lib/ambry/adapter.rb', line 15

def initialize(options = {})
  @name      = options[:name] || Ambry.default_adapter_name
  @read_only = false
  load_database
  Ambry.register_adapter(self)
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db


11
12
13
# File 'lib/ambry/adapter.rb', line 11

def db
  @db
end

#nameObject (readonly)

Returns the value of attribute name


10
11
12
# File 'lib/ambry/adapter.rb', line 10

def name
  @name
end

#read_onlyObject

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_dataObject

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_dataObject

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_databaseObject

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_databaseObject

Inheriting adapters can overload this method to persist the data to some kind of storage.

Raises:


57
58
59
60
# File 'lib/ambry/adapter.rb', line 57

def save_database
  raise AmbryError if read_only?
  true
end