Class: Pod4::NullInterface
- 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},
{one: 2, two: 4} ] )
...
The first column passed is taken to be the ID. We default to autoincrement = true, as standard. Note that ID values for the initial data are not auto-assigned; you need to specify them.
You can switch to autoincrement = false by setting ‘interface.id_ai = false`.
Note: this is quite different from the behaviour before v1.0, where NullInterface effectively only allowed you to create a non-autoincrement interface.
Constant Summary
Constants inherited from Interface
Instance Attribute Summary collapse
-
#id_ai ⇒ Object
Returns the value of attribute id_ai.
-
#id_fld ⇒ Object
readonly
Returns the value of attribute id_fld.
Instance Method Summary collapse
-
#create(record) ⇒ Object
Record is a hash of field: value.
-
#delete(id) ⇒ Object
ID is that first column.
-
#initialize(*cols, data) ⇒ NullInterface
constructor
Initialise the interface by passing it a list of columns and an array of hashes to fill them.
-
#list(selection = nil) ⇒ Object
Selection is a hash, but only the first key/value pair is honoured.
-
#read(id) ⇒ Object
ID is the first column you named in new().
-
#update(id, record) ⇒ Object
ID is the first column you named in new().
Methods inherited from Interface
#_connection, #close_connection, #new_connection
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.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pod4/null_interface.rb', line 36 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 @id_ai = true rescue => e handle_error(e) end |
Instance Attribute Details
#id_ai ⇒ Object
Returns the value of attribute id_ai.
31 32 33 |
# File 'lib/pod4/null_interface.rb', line 31 def id_ai @id_ai end |
#id_fld ⇒ Object (readonly)
Returns the value of attribute id_fld.
30 31 32 |
# File 'lib/pod4/null_interface.rb', line 30 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().
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pod4/null_interface.rb', line 70 def create(record) raise(ArgumentError, "Create requires an ID") if record.nil? || ! record.respond_to?(:to_h) raise(ArgumentError, "Record missing ID field") \ if !@id_ai && record[@id_fld].nil? && record[@id_fld.to_s].nil? datum = record.to_h datum[@id_fld] = (@data.size + 1) if @id_ai @data << record.to_h datum[@id_fld] rescue => e handle_error(e) end |
#delete(id) ⇒ Object
ID is that first column
118 119 120 121 122 123 124 125 126 |
# File 'lib/pod4/null_interface.rb', line 118 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.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pod4/null_interface.rb', line 51 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()
89 90 91 92 93 94 95 96 97 |
# File 'lib/pod4/null_interface.rb', line 89 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.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pod4/null_interface.rb', line 103 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 |