Class: Pod4::NullInterface

Inherits:
Interface show all
Defined in:
lib/pod4/null_interface.rb

Overview

Pod4 Interface *for testing*. Fakes a table and records.

Example:

class TestModel < Pod4::Model
  attr_columns :one, :two
  set_interface NullInterface.new( :one, :two [ {one: 1, two: 2} ] )
  ...

The first column passed is taken to be the ID. Note that ID is not auto-assigned; you need to specify it in the record.

Constant Summary

Constants inherited from Interface

Interface::ACTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Metaxing

#define_class_method, #metaclass

Constructor Details

#initialize(*cols, data) ⇒ NullInterface

Initialise the interface by passing it a list of columns and an array of hashes to fill them.



30
31
32
33
34
35
36
37
38
39
# File 'lib/pod4/null_interface.rb', line 30

def initialize(*cols, data)
  raise ArgumentError, "no columns"  if cols.nil? || cols == []

  @cols   = cols.dup.map(&:to_sym)
  @data   = Array.new(data.dup).flatten 
  @id_fld = @cols.first

rescue => e
  handle_error(e)
end

Instance Attribute Details

#id_fldObject (readonly)

Returns the value of attribute id_fld.



24
25
26
# File 'lib/pod4/null_interface.rb', line 24

def id_fld
  @id_fld
end

Instance Method Details

#create(record) ⇒ Object

Record is a hash of field: value

Note that we will store any old crap, not just the fields you named in new().



65
66
67
68
69
70
71
72
73
# File 'lib/pod4/null_interface.rb', line 65

def create(record)
  raise(ArgumentError, "Create requires an ID") if record.nil? || ! record.respond_to?(:to_h)

  @data << record.to_h
  record[@id_fld]

rescue => e
  handle_error(e)
end

#delete(id) ⇒ Object

ID is that first column



110
111
112
113
114
115
116
117
118
# File 'lib/pod4/null_interface.rb', line 110

def delete(id)
  raise(ArgumentError, "Delete requires an ID")  if id.nil?
  raise(Pod4::CantContinue, "'No record found with ID '#{id}'") if read(id).empty?

  @data.delete_if {|r| r[@id_fld] == id }
  self
rescue => e
  handle_error(e)
end

#list(selection = nil) ⇒ Object

Selection is a hash, but only the first key/value pair is honoured.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pod4/null_interface.rb', line 45

def list(selection=nil)
  if selection
    key, value = selection.to_a.first
    rows = @data.find_all {|r| r[key.to_sym] == value}
  else
    rows = @data
  end

  rows.map{|x| Octothorpe.new(x) }

rescue => e
  handle_error(e)
end

#read(id) ⇒ Object

ID is the first column you named in new()



79
80
81
82
83
84
85
86
87
# File 'lib/pod4/null_interface.rb', line 79

def read(id)
  raise(ArgumentError, "Read requires an ID") if id.nil?

  rec = @data.find{|x| x[@id_fld] == id }
  Octothorpe.new(rec)

rescue => e
  handle_error(e)
end

#update(id, record) ⇒ Object

ID is the first column you named in new(). Record should be a Hash or Octothorpe. Again, note that we don’t care what columns you send us.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pod4/null_interface.rb', line 94

def update(id, record)
  raise(ArgumentError, "Update requires an ID") if id.nil?

  rec = @data.find{|x| x[@id_fld] == id }
  raise Pod4::CantContinue, "No record found with ID '#{id}'" unless rec

  rec.merge!(record.to_h)
  self
rescue => e
  handle_error(e)
end