Class: Norman::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/norman/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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Adapter

Returns a new instance of Adapter.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :name (String)

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



14
15
16
17
18
# File 'lib/norman/adapter.rb', line 14

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

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#db_for(klass) ⇒ Object

Get a hash of all the data for the specified model class.

Parameters:

  • klass (#to_s)

    The model class whose data to return.



22
23
24
# File 'lib/norman/adapter.rb', line 22

def db_for(klass)
  @db[klass.to_s] ||= {}
end

#export_dataObject

Inheriting adapters can overload this method to export the data to a String.



37
38
39
# File 'lib/norman/adapter.rb', line 37

def export_data
  true
end

#import_dataObject

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



43
44
45
# File 'lib/norman/adapter.rb', line 43

def import_data
  true
end

#load_databaseObject

Loads the database. For this adapter, that means simply creating a new hash.



28
29
30
# File 'lib/norman/adapter.rb', line 28

def load_database
  @db = {}
end

#save_databaseObject

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



49
50
51
# File 'lib/norman/adapter.rb', line 49

def save_database
  true
end