Skip to content

Instantly share code, notes, and snippets.

@jeremyevans
Forked from leucos/record.rb
Created March 31, 2012 19:20
Show Gist options
  • Save jeremyevans/2267679 to your computer and use it in GitHub Desktop.
Save jeremyevans/2267679 to your computer and use it in GitHub Desktop.
An attempt with Sequel's composition plugin
# In-memory test database
# The schema has been simplified for the discussion (yes, it should have some domain_id somewhere !)
#
# Values in the content field for a SOA, are, in order :
# ns email serial refresh retry expiry minimum
DB = Sequel.sqlite
DB.run "CREATE TABLE records (name VARCHAR(255) PRIMARY KEY NOT NULL, type VARCHAR(5), content VARCHAR(255))"
DB.run "INSERT INTO records VALUES ('example.com', 'SOA', 'ns.example.com root.example.com 2012333335 28800 86400 3600000 86400')"
# Just an assert function for basic testing
def assert
printf '.'
raise "#{caller} => assertion failed !" unless yield
end
# The record model
class Record < Sequel::Model
plugin :composition
# This is the list of the inner fields, located in the 'content'
# column of the SOA type records
# The fields are listed in order so we can map easily to the
# splited column
SOA_COLUMNS= [ :ns, :email, :serial, :refresh,
:retry, :expiry, :minimum ]
SOA = Struct.new(*SOA_COLUMNS)
SOA_COMPOSER = proc{SOA.new(*content.split)}
SOA_DECOMPOSER = proc{self.content = SOA_COLUMNS.map{|c| send(c)}.join(' ')}
SOA_COLUMNS.each do |c|
define_method(c){soa.send(c)}
e = :"#{c}="
define_method(e){|v| soa.send(e, v)}
end
composition :soa,
:composer => SOA_COMPOSER,
:decomposer => SOA_DECOMPOSER
end
# Tests
# Uncommented tests are passing
# Commented tests are failing
#
print "Testing."
a = Record.first
# Original content
assert { a.content == 'ns.example.com root.example.com 2012333335 28800 86400 3600000 86400' }
a.ns = "newns.example.com"
a.email = "hostmaster.example.com"
a.serial = "00000"
a.refresh = "11111"
a.retry = "22222"
a.expiry = "33333"
a.minimum = "44444"
assert { a.ns == 'newns.example.com' }
assert { a.email == 'hostmaster.example.com' }
assert { a.serial == '00000'}
assert { a.refresh == '11111' }
assert { a.retry == '22222' }
assert { a.expiry == '33333' }
assert { a.minimum == '44444'}
a.save
# Everythings works perfectly below
assert { a.content == 'newns.example.com hostmaster.example.com 00000 11111 22222 33333 44444' }
a.save
a = Record.first
assert { a.content == 'newns.example.com hostmaster.example.com 00000 11111 22222 33333 44444' }
assert { a.ns == 'newns.example.com' }
assert { a.email == 'hostmaster.example.com' }
assert { a.serial == '00000'}
assert { a.refresh == '11111' }
assert { a.retry == '22222' }
assert { a.expiry == '33333' }
assert { a.minimum == '44444'}
puts "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment