Class: Pod4::Interface

Inherits:
Object
  • Object
show all
Extended by:
Metaxing
Defined in:
lib/pod4/interface.rb

Overview

Abstract class, The parent of all interfaces.

An interface encapsulates whatever method we are using up connect to the data. Its state is therefore that of the connection, not the DB table or whatever entity that the data source uses to group data. It raises only SwingShift errors (wrapping the error it gets inside a SwingShift error).

We would expect a child of Interface for each data access type (sequelInterface, NebulousInterface, etc). These children *will not change* the signatures of the methods below.

The methods below are the required ones. Interfaces will likely implement other, interface-specific, ways of accessing data.

In Normal use, the interface classes may in turn be subclassed as inner classes within each model, in order to customise them for the specific entity that they are drawing data from.

Note that your Interface subclass probably returns an Octothorpe rather than a Hash, q.v.. (But you should be able to treat the former as if it were the latter in most cases.)

Note that if an Interface raises a Pod4::WeakError, then Pod4::Model will catch that and turn it into a Pod4::Alert.

Constant Summary collapse

ACTIONS =
[ :list, :create, :read, :update, :delete ]

Instance Method Summary collapse

Methods included from Metaxing

define_class_method, metaclass

Constructor Details

#initializeInterface

Individual implementations are likely to have very different initialize methods, which will accept whatever SwingShift object is needed to contact the data store, eg. the Sequel DB object.

Raises:



56
57
58
# File 'lib/pod4/interface.rb', line 56

def initialize
  raise NotImplemented, "Interface needs to define an 'initialize' method"
end

Instance Method Details

#_connectionObject

For testing purposes you should expose a _connection method that returns the Connection object the Interface uses

Raises:



121
122
123
# File 'lib/pod4/interface.rb', line 121

def _connection
  raise NotImplemented, "Interface needs to define a '_connection' method"
end

#close_connection(conn) ⇒ Object

Called by a Connection Object to close the connection.

Raises:



113
114
115
# File 'lib/pod4/interface.rb', line 113

def close_connection(conn)
  raise NotImplemented, "Interface needs to define a 'close_connection' method"
end

#create(record) ⇒ Object

Create accepts a record parameter (Hash or OT, but again, the format of this will vary) representing a record, and creates the record. Should return the ID for the new record.

Raises:



76
77
78
# File 'lib/pod4/interface.rb', line 76

def create(record)
  raise NotImplemented, "Interface needs to define a 'create' method"
end

#delete(id) ⇒ Object

delete removes the record with the given ID. returns self.

Raises:



99
100
101
# File 'lib/pod4/interface.rb', line 99

def delete(id)
  raise NotImplemented, "Interface needs to define a 'delete' method"
end

#id_aiObject

true if id_fld autoincrements

Raises:



47
48
49
# File 'lib/pod4/interface.rb', line 47

def id_ai
  raise NotImplemented, "Interface has no 'id_ai' method (use `set_id_fld`?)"
end

#id_fldObject

A field name in the data source, the name of the unique ID field.

Raises:



40
41
42
# File 'lib/pod4/interface.rb', line 40

def id_fld
  raise NotImplemented, "Interface has no 'id_fld' method (use `set_id_fld`?)"
end

#list(selection = nil) ⇒ Object

List accepts a parameter as selection criteria, and returns an array of Octothorpes. Exactly what the selection criteria look like will vary from interface to interface. So will the contents of the return OT, although it must include the ID field. (Ideally each element of the return array should follow the same format as the return value for read(). )

Note that list should ALWAYS return an array; never nil.

Raises:



68
69
70
# File 'lib/pod4/interface.rb', line 68

def list(selection=nil)
  raise NotImplemented, "Interface needs to define a 'list' method"
end

#new_connection(args) ⇒ Object

Called by a Connection object to start a database connection

Raises:



106
107
108
# File 'lib/pod4/interface.rb', line 106

def new_connection(args)
  raise NotImplemented, "Interface needs to define a 'new_connection' method"
end

#read(id) ⇒ Object

Read accepts an ID, and returns an Octothorpe representing the unique record for that ID. If there is no record matching the ID then it returns an empty Octothorpe.

Raises:



84
85
86
# File 'lib/pod4/interface.rb', line 84

def read(id)
  raise NotImplemented, "Interface needs to define a 'read' method"
end

#update(id, record) ⇒ Object

Update accepts an ID and a record parameter. It updates the record on the data source that matches the ID using the record parameter. It returns self.

Raises:



92
93
94
# File 'lib/pod4/interface.rb', line 92

def update(id, record)
  raise NotImplemented, "Interface needs to define a 'update' method"
end