1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
local M = { _NAME = "uri.urn.isbn" }
local Util = require "uri._util"
local URN = require "uri.urn"
Util.subclass_of(M, URN)
-- This implements the 'isbn' NID defined in RFC 3187, and is consistent
-- with the same NID suggested in RFC 2288.
local function _valid_isbn (isbn)
if not isbn:find("^[-%d]+[%dXx]$") then return nil, "invalid character" end
local ISBN = Util.attempt_require("isbn")
if ISBN then return ISBN:new(isbn) end
return isbn
end
local function _normalize_isbn (isbn)
isbn = isbn:gsub("%-", ""):upper()
local ISBN = Util.attempt_require("isbn")
if ISBN then return tostring(ISBN:new(isbn)) end
return isbn
end
function M.init (self)
local nss = self:nss()
local ok, msg = _valid_isbn(nss)
if not ok then return nil, "invalid ISBN value (" .. msg .. ")" end
self:nss(_normalize_isbn(nss))
return self
end
function M.nss (self, new)
local old = M._SUPER.nss(self)
if new then
local ok, msg = _valid_isbn(new)
if not ok then
error("bad ISBN value '" .. new .. "' (" .. msg .. ")")
end
M._SUPER.nss(self, _normalize_isbn(new))
end
return old
end
function M.isbn_digits (self, new)
local old = self:nss():gsub("%-", "")
if new then
local ok, msg = _valid_isbn(new)
if not ok then
error("bad ISBN value '" .. new .. "' (" .. msg .. ")")
end
self._SUPER.nss(self, _normalize_isbn(new))
end
return old
end
function M.isbn (self, new)
local ISBN = require "isbn"
local old = ISBN:new(self:nss())
if new then self:nss(tostring(new)) end
return old
end
return M
-- vi:ts=4 sw=4 expandtab
|