Class: DataMapper::Adapters::MnesiaAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/dm-mnesia-adapter.rb

Defined Under Namespace

Classes: ConnectError, ReadError, WriteError

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ MnesiaAdapter

Initialize the adapter with the given options

  1. hostname: location of the rbridge server as an Ip or url, default is localhost

  2. port: the port to comunicate with rbridge, default is 9900

  3. mod: rbridge offers several modes of operation.

  4. ids_table: the table in mnesia that will handle the ids of each record created

  5. id_function: in case an erlang function is wished to be used for record id generations, it can be set here. If the id is a BinaryString, this is required



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dm-mnesia-adapter.rb', line 15

def initialize(name, options)
  super                                       

  @hosname = @options[:hostname] ||= "localhost"         
  @port = @options[:port]        ||= 9900 
  @mod = @options[:mod] 
  @ids_table = @options[:ids_table]
  @id_function = @options[:id_function]
  @con = con
  @table_attributes = nil

  ## Refreshes the spec table so that spec test run properly
  con.erl("mnesia:clear_table(heffalump).") if name == :default
end

Instance Method Details

#con(&blk) ⇒ Object

Initializes the connection to the rbridge server



31
32
33
34
35
36
37
38
39
40
# File 'lib/dm-mnesia-adapter.rb', line 31

def con(&blk)
  # connect to the server
  if @con.nil?
    @con = RBridge.new(@mod,@hostname, @port)
    if @con.erl("1*1.") != 1
       raise "Error opening connection to database: #{@con.erl("1*1.")}"
    end
  end
  @con
end

#create(resources) ⇒ Object

Create function required by the adapter spec



43
44
45
46
47
48
# File 'lib/dm-mnesia-adapter.rb', line 43

def create(resources)
    resources.each do |resource|
      initialize_identity_field(resource,get_key(resource))
      save(con, parse_resource(resource))
  end
end

#delete(collection) ⇒ Object

Delete function required by the adapter spec



73
74
75
76
77
# File 'lib/dm-mnesia-adapter.rb', line 73

def delete(collection)
    collection.each do |resource|
      erl_delete(resource.model,resource.key.first)
    end
end

#read(query) ⇒ Object

Read function required by the adapter spec TODO change query so that the mnesia filters the results and does not bring all records on the table every time



53
54
55
56
57
58
59
60
61
# File 'lib/dm-mnesia-adapter.rb', line 53

def read(query)
    temp_records = erl_read(query)
    records = []
    temp_records.each do |value|
      records << value if value
    end
  result = query.filter_records(records.reverse)
  result
end

#update(attributes, collection) ⇒ Object

Update function required by the adapter spec



64
65
66
67
68
69
70
# File 'lib/dm-mnesia-adapter.rb', line 64

def update(attributes, collection)
  attributes = attributes_as_fields(attributes)
  collection.each do |resource|
      attributes = resource.attributes(:field).merge(attributes)
      save(con, parse_resource(resource))
    end
end