pubid 2.0.0.pre.alpha.9 → 2.0.0.pre.alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (461) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +33 -11
  3. data/conformance/pending.yaml +4 -0
  4. data/lib/pubid/adobe/identifier.rb +1 -3
  5. data/lib/pubid/adobe/parser.rb +10 -2
  6. data/lib/pubid/adobe.rb +1 -0
  7. data/lib/pubid/amca/builder.rb +2 -2
  8. data/lib/pubid/amca/identifiers/base.rb +49 -27
  9. data/lib/pubid/amca/identifiers/interpretation.rb +17 -9
  10. data/lib/pubid/amca/identifiers/publication.rb +13 -9
  11. data/lib/pubid/amca/identifiers/standard.rb +2 -0
  12. data/lib/pubid/amca/parser.rb +1 -1
  13. data/lib/pubid/amca/renderer.rb +4 -4
  14. data/lib/pubid/amca/urn_generator.rb +2 -2
  15. data/lib/pubid/amca.rb +2 -1
  16. data/lib/pubid/ansi/builder.rb +2 -2
  17. data/lib/pubid/ansi/identifier.rb +18 -0
  18. data/lib/pubid/ansi/parser.rb +1 -1
  19. data/lib/pubid/ansi/renderer.rb +2 -2
  20. data/lib/pubid/ansi.rb +5 -5
  21. data/lib/pubid/api/builder.rb +16 -2
  22. data/lib/pubid/api/identifier.rb +25 -4
  23. data/lib/pubid/api/identifiers/mpms.rb +10 -8
  24. data/lib/pubid/api/parser.rb +1 -1
  25. data/lib/pubid/api/renderer.rb +16 -7
  26. data/lib/pubid/api/single_identifier.rb +5 -12
  27. data/lib/pubid/api.rb +1 -0
  28. data/lib/pubid/ashrae/builder.rb +31 -10
  29. data/lib/pubid/ashrae/identifiers/addenda_package.rb +6 -0
  30. data/lib/pubid/ashrae/identifiers/addendum.rb +8 -0
  31. data/lib/pubid/ashrae/identifiers/base.rb +81 -5
  32. data/lib/pubid/ashrae/identifiers/combined_addenda.rb +7 -0
  33. data/lib/pubid/ashrae/identifiers/errata.rb +9 -0
  34. data/lib/pubid/ashrae/identifiers/guideline.rb +3 -0
  35. data/lib/pubid/ashrae/identifiers/interpretation.rb +16 -0
  36. data/lib/pubid/ashrae/identifiers/standard.rb +3 -0
  37. data/lib/pubid/ashrae/parser.rb +23 -8
  38. data/lib/pubid/ashrae/renderer.rb +6 -6
  39. data/lib/pubid/ashrae/supplement_identifier.rb +17 -2
  40. data/lib/pubid/ashrae/urn_generator.rb +8 -2
  41. data/lib/pubid/ashrae.rb +7 -1
  42. data/lib/pubid/asme/builder.rb +11 -2
  43. data/lib/pubid/asme/identifier.rb +9 -0
  44. data/lib/pubid/asme/identifiers/standard.rb +86 -2
  45. data/lib/pubid/asme/parser.rb +1 -1
  46. data/lib/pubid/asme/renderer.rb +3 -3
  47. data/lib/pubid/asme/single_identifier.rb +8 -1
  48. data/lib/pubid/asme/urn_generator.rb +2 -2
  49. data/lib/pubid/asme.rb +1 -0
  50. data/lib/pubid/astm/builder.rb +10 -3
  51. data/lib/pubid/astm/identifier.rb +9 -0
  52. data/lib/pubid/astm/identifiers/adjunct.rb +16 -1
  53. data/lib/pubid/astm/identifiers/code_number.rb +81 -0
  54. data/lib/pubid/astm/identifiers/data_series.rb +2 -0
  55. data/lib/pubid/astm/identifiers/iso_dual_published.rb +16 -0
  56. data/lib/pubid/astm/identifiers/manual.rb +10 -0
  57. data/lib/pubid/astm/identifiers/monograph.rb +2 -0
  58. data/lib/pubid/astm/identifiers/research_report.rb +10 -0
  59. data/lib/pubid/astm/identifiers/standard.rb +10 -0
  60. data/lib/pubid/astm/identifiers/technical_report.rb +2 -0
  61. data/lib/pubid/astm/identifiers/work_in_progress.rb +2 -0
  62. data/lib/pubid/astm/identifiers.rb +1 -0
  63. data/lib/pubid/astm/parser.rb +1 -1
  64. data/lib/pubid/astm/renderer.rb +1 -1
  65. data/lib/pubid/astm/single_identifier.rb +31 -1
  66. data/lib/pubid/astm/urn_generator.rb +7 -5
  67. data/lib/pubid/astm.rb +1 -0
  68. data/lib/pubid/bipm/builder.rb +66 -10
  69. data/lib/pubid/bipm/identifier.rb +134 -9
  70. data/lib/pubid/bipm/identifiers/committee_document.rb +12 -0
  71. data/lib/pubid/bipm/identifiers/guide.rb +11 -0
  72. data/lib/pubid/bipm/identifiers/meeting.rb +12 -0
  73. data/lib/pubid/bipm/identifiers/mep.rb +12 -0
  74. data/lib/pubid/bipm/identifiers/metrologia_article.rb +24 -0
  75. data/lib/pubid/bipm/identifiers/si_brochure.rb +13 -0
  76. data/lib/pubid/bipm/parser.rb +65 -12
  77. data/lib/pubid/bipm/renderer.rb +8 -0
  78. data/lib/pubid/bipm/urn_parser.rb +11 -3
  79. data/lib/pubid/bipm.rb +7 -1
  80. data/lib/pubid/bsi/builder.rb +48 -38
  81. data/lib/pubid/bsi/components/date.rb +10 -4
  82. data/lib/pubid/bsi/identifiers/adopted_european_norm.rb +36 -7
  83. data/lib/pubid/bsi/identifiers/adopted_international_standard.rb +7 -6
  84. data/lib/pubid/bsi/identifiers/british_industrial_practice.rb +1 -2
  85. data/lib/pubid/bsi/identifiers/bundled_identifier.rb +10 -0
  86. data/lib/pubid/bsi/identifiers/committee_document.rb +10 -1
  87. data/lib/pubid/bsi/identifiers/handbook.rb +1 -3
  88. data/lib/pubid/bsi/identifiers/practice_guide.rb +1 -2
  89. data/lib/pubid/bsi/identifiers/set.rb +11 -0
  90. data/lib/pubid/bsi/identifiers/standalone_amendment.rb +10 -1
  91. data/lib/pubid/bsi/parser.rb +1 -1
  92. data/lib/pubid/bsi/renderer.rb +63 -51
  93. data/lib/pubid/bsi/single_identifier.rb +27 -9
  94. data/lib/pubid/bsi/urn_generator.rb +11 -2
  95. data/lib/pubid/bsi.rb +7 -1
  96. data/lib/pubid/builder/base.rb +18 -8
  97. data/lib/pubid/bundled_identifier.rb +16 -6
  98. data/lib/pubid/calconnect/identifier.rb +9 -5
  99. data/lib/pubid/calconnect/parser.rb +1 -1
  100. data/lib/pubid/calconnect.rb +7 -1
  101. data/lib/pubid/ccsds/identifier.rb +13 -2
  102. data/lib/pubid/ccsds/identifiers/base.rb +4 -2
  103. data/lib/pubid/ccsds/identifiers/corrigendum.rb +2 -2
  104. data/lib/pubid/ccsds/parser.rb +1 -1
  105. data/lib/pubid/ccsds/single_identifier.rb +16 -12
  106. data/lib/pubid/ccsds.rb +1 -0
  107. data/lib/pubid/cen_cenelec/builder.rb +101 -76
  108. data/lib/pubid/cen_cenelec/identifier.rb +115 -3
  109. data/lib/pubid/cen_cenelec/identifiers/adopted_european_norm.rb +27 -16
  110. data/lib/pubid/cen_cenelec/identifiers/amendment.rb +46 -6
  111. data/lib/pubid/cen_cenelec/identifiers/base.rb +8 -9
  112. data/lib/pubid/cen_cenelec/identifiers/cen_report.rb +1 -1
  113. data/lib/pubid/cen_cenelec/identifiers/cen_workshop_agreement.rb +1 -1
  114. data/lib/pubid/cen_cenelec/identifiers/cenelec_harmonization_document.rb +1 -1
  115. data/lib/pubid/cen_cenelec/identifiers/consolidated_identifier.rb +29 -20
  116. data/lib/pubid/cen_cenelec/identifiers/corrigendum.rb +47 -8
  117. data/lib/pubid/cen_cenelec/identifiers/european_prestandard.rb +27 -2
  118. data/lib/pubid/cen_cenelec/identifiers/european_specification.rb +1 -1
  119. data/lib/pubid/cen_cenelec/identifiers/fragment.rb +15 -5
  120. data/lib/pubid/cen_cenelec/identifiers/guide.rb +1 -1
  121. data/lib/pubid/cen_cenelec/identifiers/harmonization_document.rb +1 -1
  122. data/lib/pubid/cen_cenelec/identifiers/technical_report.rb +1 -1
  123. data/lib/pubid/cen_cenelec/identifiers/technical_specification.rb +1 -1
  124. data/lib/pubid/cen_cenelec/parser.rb +1 -1
  125. data/lib/pubid/cen_cenelec/renderer.rb +55 -60
  126. data/lib/pubid/cen_cenelec/single_identifier.rb +20 -2
  127. data/lib/pubid/cen_cenelec/urn_generator.rb +66 -44
  128. data/lib/pubid/cen_cenelec.rb +28 -10
  129. data/lib/pubid/cie/builder.rb +10 -0
  130. data/lib/pubid/cie/identifier.rb +6 -1
  131. data/lib/pubid/cie/identifiers/bundle.rb +4 -2
  132. data/lib/pubid/cie/identifiers/conference.rb +2 -2
  133. data/lib/pubid/cie/identifiers/corrigendum.rb +2 -2
  134. data/lib/pubid/cie/identifiers/dual_published.rb +2 -2
  135. data/lib/pubid/cie/identifiers/identical.rb +2 -2
  136. data/lib/pubid/cie/identifiers/joint_published.rb +2 -2
  137. data/lib/pubid/cie/identifiers/proceedings.rb +8 -6
  138. data/lib/pubid/cie/identifiers/standard.rb +22 -13
  139. data/lib/pubid/cie/identifiers/supplement.rb +2 -2
  140. data/lib/pubid/cie/identifiers/tutorial_bundle.rb +2 -2
  141. data/lib/pubid/cie/parser.rb +1 -1
  142. data/lib/pubid/cie.rb +7 -1
  143. data/lib/pubid/conformance/checks.rb +72 -0
  144. data/lib/pubid/conformance/corpus/case.rb +55 -0
  145. data/lib/pubid/conformance/corpus.rb +36 -0
  146. data/lib/pubid/conformance/generator.rb +212 -0
  147. data/lib/pubid/conformance/pending.rb +63 -0
  148. data/lib/pubid/conformance/runner.rb +153 -0
  149. data/lib/pubid/conformance.rb +55 -0
  150. data/lib/pubid/core.rb +2 -0
  151. data/lib/pubid/csa/builder.rb +47 -47
  152. data/lib/pubid/csa/composite_identifier.rb +14 -19
  153. data/lib/pubid/csa/identifier.rb +143 -81
  154. data/lib/pubid/csa/identifiers/bundled.rb +23 -13
  155. data/lib/pubid/csa/identifiers/canadian_adopted.rb +13 -13
  156. data/lib/pubid/csa/identifiers/cec.rb +44 -4
  157. data/lib/pubid/csa/identifiers/combined.rb +95 -73
  158. data/lib/pubid/csa/identifiers/csa_adopted.rb +4 -4
  159. data/lib/pubid/csa/identifiers/package.rb +2 -2
  160. data/lib/pubid/csa/parser.rb +6 -2
  161. data/lib/pubid/csa/renderer.rb +13 -5
  162. data/lib/pubid/csa/single_identifier.rb +93 -4
  163. data/lib/pubid/csa/urn_generator.rb +9 -2
  164. data/lib/pubid/csa/wrapper_identifier.rb +39 -23
  165. data/lib/pubid/csa.rb +7 -1
  166. data/lib/pubid/doi/identifier.rb +6 -3
  167. data/lib/pubid/doi/parser.rb +1 -1
  168. data/lib/pubid/doi.rb +1 -0
  169. data/lib/pubid/easc/identifier.rb +0 -2
  170. data/lib/pubid/easc/parser.rb +10 -2
  171. data/lib/pubid/easc.rb +1 -0
  172. data/lib/pubid/ecma/builder.rb +23 -9
  173. data/lib/pubid/ecma/identifier.rb +81 -10
  174. data/lib/pubid/ecma/parser.rb +39 -4
  175. data/lib/pubid/ecma/renderer.rb +29 -8
  176. data/lib/pubid/ecma/urn_generator.rb +34 -13
  177. data/lib/pubid/ecma/urn_parser.rb +30 -14
  178. data/lib/pubid/ecma.rb +1 -0
  179. data/lib/pubid/errors.rb +80 -0
  180. data/lib/pubid/etsi/builder.rb +6 -1
  181. data/lib/pubid/etsi/identifiers/base.rb +34 -20
  182. data/lib/pubid/etsi/identifiers/etsi_standard.rb +120 -41
  183. data/lib/pubid/etsi/identifiers/supplement_identifier.rb +10 -0
  184. data/lib/pubid/etsi/parser.rb +1 -1
  185. data/lib/pubid/etsi.rb +1 -0
  186. data/lib/pubid/evs/builder.rb +26 -0
  187. data/lib/pubid/evs/identifier.rb +37 -0
  188. data/lib/pubid/evs/identifiers/national_adoption.rb +22 -0
  189. data/lib/pubid/evs/identifiers.rb +9 -0
  190. data/lib/pubid/evs/parser.rb +27 -0
  191. data/lib/pubid/evs/renderer.rb +30 -0
  192. data/lib/pubid/evs/urn_generator.rb +30 -0
  193. data/lib/pubid/evs/urn_parser.rb +80 -0
  194. data/lib/pubid/evs.rb +90 -0
  195. data/lib/pubid/export.rb +1 -0
  196. data/lib/pubid/format_detector.rb +1 -0
  197. data/lib/pubid/gb/builder.rb +14 -9
  198. data/lib/pubid/gb/identifier.rb +29 -7
  199. data/lib/pubid/gb/parser.rb +5 -1
  200. data/lib/pubid/gb/renderer.rb +4 -3
  201. data/lib/pubid/gb.rb +2 -0
  202. data/lib/pubid/gost/builder.rb +46 -9
  203. data/lib/pubid/gost/identifier.rb +0 -2
  204. data/lib/pubid/gost/identifiers/foreign_reference.rb +1 -1
  205. data/lib/pubid/gost/parser.rb +10 -2
  206. data/lib/pubid/gost.rb +1 -0
  207. data/lib/pubid/iala/identifier.rb +9 -6
  208. data/lib/pubid/iala/parser.rb +10 -2
  209. data/lib/pubid/iala/urn_parser.rb +6 -2
  210. data/lib/pubid/iala.rb +1 -0
  211. data/lib/pubid/iana/builder.rb +3 -1
  212. data/lib/pubid/iana/identifier.rb +74 -6
  213. data/lib/pubid/iana/identifiers/registry.rb +36 -0
  214. data/lib/pubid/iana/parser.rb +1 -1
  215. data/lib/pubid/iana/renderer.rb +3 -0
  216. data/lib/pubid/iana/urn_generator.rb +5 -1
  217. data/lib/pubid/iana.rb +1 -0
  218. data/lib/pubid/identifier.rb +605 -13
  219. data/lib/pubid/identifier_metadata.rb +1 -0
  220. data/lib/pubid/idf/builder.rb +3 -3
  221. data/lib/pubid/idf/identifier.rb +25 -9
  222. data/lib/pubid/idf/identifiers/amendment.rb +1 -1
  223. data/lib/pubid/idf/identifiers/corrigendum.rb +1 -1
  224. data/lib/pubid/idf/identifiers/international_standard.rb +1 -1
  225. data/lib/pubid/idf/identifiers/reviewed_method.rb +1 -1
  226. data/lib/pubid/idf/parser.rb +1 -1
  227. data/lib/pubid/idf/renderer.rb +4 -4
  228. data/lib/pubid/idf/single_identifier.rb +1 -1
  229. data/lib/pubid/idf/urn_generator.rb +1 -1
  230. data/lib/pubid/idf.rb +7 -1
  231. data/lib/pubid/iec/builder.rb +14 -7
  232. data/lib/pubid/iec/components.rb +0 -1
  233. data/lib/pubid/iec/identifier.rb +73 -83
  234. data/lib/pubid/iec/identifiers/amendment.rb +6 -2
  235. data/lib/pubid/iec/identifiers/component_specification.rb +0 -10
  236. data/lib/pubid/iec/identifiers/conformity_assessment.rb +0 -10
  237. data/lib/pubid/iec/identifiers/corrigendum.rb +2 -2
  238. data/lib/pubid/iec/identifiers/fragment_identifier.rb +2 -4
  239. data/lib/pubid/iec/identifiers/guide.rb +0 -16
  240. data/lib/pubid/iec/identifiers/international_standard.rb +15 -2
  241. data/lib/pubid/iec/identifiers/operational_document.rb +0 -10
  242. data/lib/pubid/iec/identifiers/publicly_available_specification.rb +0 -12
  243. data/lib/pubid/iec/identifiers/societal_technology_trend_report.rb +0 -10
  244. data/lib/pubid/iec/identifiers/systems_reference_document.rb +0 -10
  245. data/lib/pubid/iec/identifiers/technical_report.rb +0 -17
  246. data/lib/pubid/iec/identifiers/technical_specification.rb +0 -17
  247. data/lib/pubid/iec/identifiers/technology_report.rb +0 -10
  248. data/lib/pubid/iec/identifiers/test_report_form.rb +0 -11
  249. data/lib/pubid/iec/identifiers/white_paper.rb +0 -10
  250. data/lib/pubid/iec/identifiers/working_document.rb +24 -10
  251. data/lib/pubid/iec/parser.rb +2 -18
  252. data/lib/pubid/iec/renderer.rb +13 -10
  253. data/lib/pubid/iec/single_identifier.rb +33 -10
  254. data/lib/pubid/iec/urn_generator.rb +227 -61
  255. data/lib/pubid/iec/urn_parser.rb +42 -4
  256. data/lib/pubid/iec.rb +1 -0
  257. data/lib/pubid/ieee/aiee/identifier.rb +2 -2
  258. data/lib/pubid/ieee/aiee/parser.rb +1 -1
  259. data/lib/pubid/ieee/builder.rb +87 -3
  260. data/lib/pubid/ieee/identifiers/adopted_standard.rb +32 -0
  261. data/lib/pubid/ieee/identifiers/base.rb +42 -5
  262. data/lib/pubid/ieee/identifiers/csa_dual_published.rb +21 -0
  263. data/lib/pubid/ieee/identifiers/dual_published.rb +55 -0
  264. data/lib/pubid/ieee/identifiers/interpretation_identifier.rb +23 -0
  265. data/lib/pubid/ieee/identifiers/joint_development.rb +11 -7
  266. data/lib/pubid/ieee/identifiers/multi_numbered_identifier.rb +57 -5
  267. data/lib/pubid/ieee/identifiers/nesc/base.rb +2 -2
  268. data/lib/pubid/ieee/identifiers/nesc/draft.rb +2 -2
  269. data/lib/pubid/ieee/identifiers/nesc/handbook.rb +2 -2
  270. data/lib/pubid/ieee/identifiers/nesc/redline.rb +2 -2
  271. data/lib/pubid/ieee/identifiers/nesc/standard.rb +2 -2
  272. data/lib/pubid/ieee/identifiers/si_standard.rb +5 -2
  273. data/lib/pubid/ieee/ire/identifier.rb +2 -2
  274. data/lib/pubid/ieee/ire/parser.rb +20 -2
  275. data/lib/pubid/ieee/nesc/parser.rb +1 -1
  276. data/lib/pubid/ieee/parser.rb +208 -33
  277. data/lib/pubid/ieee/project_renderer.rb +46 -0
  278. data/lib/pubid/ieee/renderer.rb +11 -8
  279. data/lib/pubid/ieee/urn_generator.rb +32 -3
  280. data/lib/pubid/ieee.rb +9 -1
  281. data/lib/pubid/ieee_debug.rb +1 -0
  282. data/lib/pubid/ietf/builder.rb +24 -8
  283. data/lib/pubid/ietf/identifiers/base.rb +25 -28
  284. data/lib/pubid/ietf/identifiers/bcp.rb +11 -0
  285. data/lib/pubid/ietf/identifiers/fyi.rb +11 -0
  286. data/lib/pubid/ietf/identifiers/internet_draft.rb +18 -2
  287. data/lib/pubid/ietf/identifiers/rfc.rb +2 -0
  288. data/lib/pubid/ietf/identifiers/serialization.rb +51 -0
  289. data/lib/pubid/ietf/identifiers/std.rb +11 -0
  290. data/lib/pubid/ietf/identifiers.rb +1 -0
  291. data/lib/pubid/ietf/parser.rb +27 -9
  292. data/lib/pubid/ietf/renderer.rb +44 -6
  293. data/lib/pubid/ietf/urn_generator.rb +20 -6
  294. data/lib/pubid/ietf/urn_parser.rb +10 -2
  295. data/lib/pubid/ietf.rb +12 -2
  296. data/lib/pubid/iho/identifiers/base.rb +10 -3
  297. data/lib/pubid/iho/parser.rb +1 -1
  298. data/lib/pubid/iho.rb +1 -0
  299. data/lib/pubid/isbn/identifier.rb +25 -7
  300. data/lib/pubid/isbn/parser.rb +1 -1
  301. data/lib/pubid/isbn.rb +1 -0
  302. data/lib/pubid/iso/builder.rb +10 -11
  303. data/lib/pubid/iso/bundled_identifier.rb +5 -3
  304. data/lib/pubid/iso/combined_identifier.rb +5 -3
  305. data/lib/pubid/iso/components.rb +0 -1
  306. data/lib/pubid/iso/identifier.rb +23 -11
  307. data/lib/pubid/iso/identifiers/directives.rb +7 -4
  308. data/lib/pubid/iso/identifiers/directives_supplement.rb +4 -4
  309. data/lib/pubid/iso/identifiers/tc_document.rb +31 -46
  310. data/lib/pubid/iso/normalizer.rb +1 -1
  311. data/lib/pubid/iso/parser.rb +1 -1
  312. data/lib/pubid/iso/urn_generator.rb +10 -10
  313. data/lib/pubid/iso.rb +12 -5
  314. data/lib/pubid/itu/builder.rb +10 -0
  315. data/lib/pubid/itu/identifiers/addendum.rb +2 -2
  316. data/lib/pubid/itu/identifiers/amendment.rb +2 -2
  317. data/lib/pubid/itu/identifiers/base.rb +29 -5
  318. data/lib/pubid/itu/identifiers/contribution.rb +31 -0
  319. data/lib/pubid/itu/identifiers/corrigendum.rb +2 -2
  320. data/lib/pubid/itu/identifiers/errata.rb +2 -2
  321. data/lib/pubid/itu/identifiers/supplement.rb +2 -2
  322. data/lib/pubid/itu/identifiers.rb +1 -0
  323. data/lib/pubid/itu/parser.rb +20 -3
  324. data/lib/pubid/itu.rb +1 -0
  325. data/lib/pubid/jcgm/builder.rb +1 -1
  326. data/lib/pubid/jcgm/identifier.rb +10 -0
  327. data/lib/pubid/jcgm/identifiers/meeting.rb +1 -1
  328. data/lib/pubid/jcgm/parser.rb +10 -3
  329. data/lib/pubid/jcgm/renderer.rb +10 -3
  330. data/lib/pubid/jcgm/single_identifier.rb +24 -15
  331. data/lib/pubid/jcgm/urn_generator.rb +1 -1
  332. data/lib/pubid/jcgm/urn_parser.rb +156 -14
  333. data/lib/pubid/jcgm.rb +10 -0
  334. data/lib/pubid/jis/identifier.rb +10 -3
  335. data/lib/pubid/jis/parser.rb +1 -1
  336. data/lib/pubid/jis.rb +1 -0
  337. data/lib/pubid/nist/builder.rb +12 -12
  338. data/lib/pubid/nist/components/stage.rb +2 -2
  339. data/lib/pubid/nist/configuration.rb +6 -8
  340. data/lib/pubid/nist/identifiers/base.rb +22 -5
  341. data/lib/pubid/nist/identifiers/circular.rb +9 -2
  342. data/lib/pubid/nist/identifiers/circular_supplement.rb +14 -5
  343. data/lib/pubid/nist/identifiers/commercial_standard_emergency.rb +1 -1
  344. data/lib/pubid/nist/identifiers/commercial_standards_monthly.rb +7 -1
  345. data/lib/pubid/nist/identifiers/crpl_report.rb +12 -7
  346. data/lib/pubid/nist/identifiers/federal_information_processing_standards.rb +2 -2
  347. data/lib/pubid/nist/identifiers/handbook.rb +9 -2
  348. data/lib/pubid/nist/identifiers/internal_report.rb +12 -5
  349. data/lib/pubid/nist/identifiers/miscellaneous_publication.rb +16 -8
  350. data/lib/pubid/nist/identifiers/monograph.rb +14 -7
  351. data/lib/pubid/nist/identifiers/report.rb +14 -6
  352. data/lib/pubid/nist/parser.rb +1 -1
  353. data/lib/pubid/nist/series/ir.rb +3 -7
  354. data/lib/pubid/nist/supplement_identifier.rb +16 -1
  355. data/lib/pubid/nist.rb +7 -1
  356. data/lib/pubid/oasis/builder.rb +12 -10
  357. data/lib/pubid/oasis/identifier.rb +89 -11
  358. data/lib/pubid/oasis/parser.rb +1 -1
  359. data/lib/pubid/oasis.rb +7 -1
  360. data/lib/pubid/ogc/identifier.rb +9 -5
  361. data/lib/pubid/ogc/parser.rb +1 -1
  362. data/lib/pubid/ogc.rb +1 -0
  363. data/lib/pubid/oiml/builder.rb +7 -3
  364. data/lib/pubid/oiml/identifiers/basic_publication.rb +2 -0
  365. data/lib/pubid/oiml/identifiers/bulletin.rb +40 -10
  366. data/lib/pubid/oiml/identifiers/code_number.rb +90 -0
  367. data/lib/pubid/oiml/identifiers/document.rb +2 -0
  368. data/lib/pubid/oiml/identifiers/expert_report.rb +2 -0
  369. data/lib/pubid/oiml/identifiers/guide.rb +2 -0
  370. data/lib/pubid/oiml/identifiers/recommendation.rb +2 -0
  371. data/lib/pubid/oiml/identifiers/seminar_report.rb +2 -0
  372. data/lib/pubid/oiml/identifiers/vocabulary.rb +2 -0
  373. data/lib/pubid/oiml/identifiers.rb +1 -0
  374. data/lib/pubid/oiml/parser.rb +8 -3
  375. data/lib/pubid/oiml/renderer.rb +3 -3
  376. data/lib/pubid/oiml/single_identifier.rb +26 -42
  377. data/lib/pubid/oiml/supplement_identifier.rb +27 -0
  378. data/lib/pubid/oiml/urn_generator.rb +4 -4
  379. data/lib/pubid/oiml.rb +7 -1
  380. data/lib/pubid/omg/builder.rb +1 -0
  381. data/lib/pubid/omg/identifier.rb +20 -3
  382. data/lib/pubid/omg/parser.rb +64 -11
  383. data/lib/pubid/omg/renderer.rb +13 -1
  384. data/lib/pubid/omg.rb +2 -1
  385. data/lib/pubid/parser/grammar.rb +74 -0
  386. data/lib/pubid/parser.rb +2 -0
  387. data/lib/pubid/parsers/mr_string.rb +19 -0
  388. data/lib/pubid/plateau/parser.rb +1 -1
  389. data/lib/pubid/plateau.rb +10 -0
  390. data/lib/pubid/prefixes_support.rb +1 -0
  391. data/lib/pubid/renderers/annotator.rb +233 -0
  392. data/lib/pubid/renderers/base.rb +13 -0
  393. data/lib/pubid/renderers/directives_renderer.rb +6 -4
  394. data/lib/pubid/renderers/human_readable.rb +3 -3
  395. data/lib/pubid/renderers/mr_string.rb +15 -9
  396. data/lib/pubid/renderers.rb +1 -0
  397. data/lib/pubid/rendering/numbering.rb +25 -7
  398. data/lib/pubid/rendering.rb +1 -0
  399. data/lib/pubid/sae/builder.rb +1 -1
  400. data/lib/pubid/sae/identifiers/base.rb +13 -3
  401. data/lib/pubid/sae/parser.rb +1 -1
  402. data/lib/pubid/sae/urn_generator.rb +1 -1
  403. data/lib/pubid/sae.rb +1 -0
  404. data/lib/pubid/schema/declaration.rb +33 -0
  405. data/lib/pubid/schema/error.rb +14 -0
  406. data/lib/pubid/schema/identifier_type.rb +23 -0
  407. data/lib/pubid/schema/loader.rb +117 -0
  408. data/lib/pubid/schema/typed_stage.rb +24 -0
  409. data/lib/pubid/schema.rb +25 -0
  410. data/lib/pubid/tgpp/builder.rb +1 -1
  411. data/lib/pubid/tgpp/identifier.rb +21 -5
  412. data/lib/pubid/tgpp/identifiers/technical_report.rb +2 -2
  413. data/lib/pubid/tgpp/identifiers/technical_specification.rb +2 -2
  414. data/lib/pubid/tgpp/parser.rb +22 -6
  415. data/lib/pubid/tgpp/renderer.rb +21 -5
  416. data/lib/pubid/tgpp/urn_generator.rb +27 -8
  417. data/lib/pubid/tgpp/urn_parser.rb +8 -6
  418. data/lib/pubid/tgpp.rb +7 -1
  419. data/lib/pubid/type_resolver.rb +16 -1
  420. data/lib/pubid/un/identifier.rb +6 -3
  421. data/lib/pubid/un/parser.rb +1 -1
  422. data/lib/pubid/un.rb +1 -0
  423. data/lib/pubid/urn_parser/errors.rb +8 -1
  424. data/lib/pubid/urn_parser.rb +1 -0
  425. data/lib/pubid/utils.rb +1 -0
  426. data/lib/pubid/version.rb +1 -1
  427. data/lib/pubid/w3c/builder.rb +5 -5
  428. data/lib/pubid/w3c/identifier.rb +38 -9
  429. data/lib/pubid/w3c/parser.rb +1 -1
  430. data/lib/pubid/w3c/renderer.rb +1 -1
  431. data/lib/pubid/w3c/urn_generator.rb +2 -2
  432. data/lib/pubid/w3c.rb +1 -0
  433. data/lib/pubid/xsf/identifier.rb +9 -5
  434. data/lib/pubid/xsf/parser.rb +17 -4
  435. data/lib/pubid/xsf.rb +7 -1
  436. data/lib/pubid.rb +399 -21
  437. data/lib/tasks/conformance.rake +34 -0
  438. data/lib/tasks/docs.rake +16 -14
  439. data/lib/tasks/schema.rake +88 -0
  440. data/schema/core/joint_prefixes.yaml +24 -0
  441. data/schema/iec.yaml +1017 -0
  442. data/schema/iso.yaml +1586 -0
  443. data/schema/schema.schema.yaml +96 -0
  444. metadata +40 -19
  445. data/archived-gems/pubid-ccsds/update_codes.yaml +0 -1
  446. data/archived-gems/pubid-iec/stages.yaml +0 -129
  447. data/archived-gems/pubid-iec/update_codes.yaml +0 -67
  448. data/archived-gems/pubid-ieee/update_codes.yaml +0 -104
  449. data/archived-gems/pubid-iso/stages.yaml +0 -106
  450. data/archived-gems/pubid-iso/update_codes.yaml +0 -4
  451. data/archived-gems/pubid-itu/i18n.yaml +0 -13
  452. data/archived-gems/pubid-itu/series.yaml +0 -42
  453. data/archived-gems/pubid-nist/publishers.yaml +0 -6
  454. data/archived-gems/pubid-nist/series.yaml +0 -121
  455. data/archived-gems/pubid-nist/update_codes.yaml +0 -93
  456. data/archived-gems/pubid-plateau/update_codes.yaml +0 -6
  457. data/lib/pubid/ccsds/identifiers/base_BASE_88929.rb +0 -70
  458. data/lib/pubid/cen_cenelec/supplement_identifier.rb +0 -48
  459. data/lib/pubid/iec/components/code.rb +0 -36
  460. data/lib/pubid/iso/components/code.rb +0 -24
  461. /data/{archived-gems/pubid-nist → data/nist}/stages.yaml +0 -0
@@ -2,6 +2,74 @@
2
2
 
3
3
  module Pubid
4
4
  class Identifier < Lutaml::Model::Serializable
5
+ # Components that serialize as a bare scalar when they carry only their
6
+ # single significant field: attribute name => the key the scalar is emitted
7
+ # under. `date` is RENAMED to `year`, matching the flat shape ISO and IEC
8
+ # already emit through their own converters.
9
+ #
10
+ # `stage_iteration` is a Components::Iteration, which holds one `:string`
11
+ # field and whose `to_s`/`render` return it — degenerate by construction.
12
+ #
13
+ # Every flavor INHERITS the attribute, so the reach of this entry is not the
14
+ # set of flavors that map the key but the set that ever POPULATES it. That
15
+ # was measured over every flavor's whole pass-fixture corpus (~99k ids):
16
+ # only ISO does, 91 times, and ISO emits the bare scalar through its own
17
+ # converter — which the inflation guard below defers to — so its serialized
18
+ # shape is unchanged (verified byte-identical over all 7,573 ISO ids). IEC
19
+ # is the flavor this entry is for; it dropped its converter pair for a
20
+ # plain map. For every other flavor the `hash.key?` guard never fires.
21
+ FLAT_SCALAR_COMPONENTS = {
22
+ edition: "edition",
23
+ date: "year",
24
+ stage_iteration: "stage_iteration",
25
+ }.freeze
26
+
27
+ # The one field each of those components degenerates to.
28
+ FLAT_SCALAR_FIELDS = {
29
+ edition: :number,
30
+ date: :year,
31
+ stage_iteration: :number,
32
+ }.freeze
33
+
34
+ # The one field each degenerate component collapses to, keyed by the
35
+ # component CLASS rather than the attribute name — so a collection
36
+ # attribute (`languages`, `copublishers`) and a flavor subclass
37
+ # (Pubid::Iec::Components::Publisher) are both covered by one entry.
38
+ #
39
+ # `Components::Code` is deliberately absent. A String left in `number`
40
+ # renders and serializes correctly today, and coercing it would turn
41
+ # `to_hash` from {"number" => "1000"} into {"number" => {"value" => "1000"}}
42
+ # on the five flavors that still declare a Code (iso, nist, csa, sae,
43
+ # ccsds) — an index wire-format change, and one the `number` retype tranches
44
+ # sequence to land last. The two entries here are the components that RAISE
45
+ # when a String reaches them, so coercing them can only repair.
46
+ #
47
+ # IEC used to be a sixth, and its removal shows the cost of the split the
48
+ # other way round: because `parse` gave a Code and `new` left a String, a
49
+ # hand-built IEC identifier was never `==` the parsed one. Retyping the
50
+ # attribute — not coercing the value — is what reconciles the two paths.
51
+ DEGENERATE_COMPONENT_FIELDS = {
52
+ Components::Language => :code,
53
+ Components::Publisher => :body,
54
+ }.freeze
55
+
56
+ # Keys lutaml reads out of the attribute hash itself
57
+ # (Serializable#extract_register_id), so they are reserved rather than
58
+ # unknown and must survive the unknown-key check.
59
+ RESERVED_INIT_KEYS = %i[lutaml_register].freeze
60
+
61
+ # Scalar aliases accepted by the constructor for a component-valued
62
+ # attribute: alias => [target attribute, the component field it fills].
63
+ # `year` is the one the issue names — it is a READER over `date`, never an
64
+ # attribute, on every date-based flavor, so `new(number: "1000", year: 2023)`
65
+ # used to build an identifier with no year and say nothing.
66
+ SCALAR_ATTRIBUTE_ALIASES = {
67
+ year: [:date, :year],
68
+ month: [:date, :month],
69
+ day: [:date, :day],
70
+ }.freeze
71
+
72
+
5
73
  class << self
6
74
  def format_registry
7
75
  @format_registry || superclass&.format_registry
@@ -25,7 +93,289 @@ module Pubid
25
93
  def from_hash(data, options = {})
26
94
  klass = concrete_class_for(data)
27
95
  return klass.from_hash(data, options) if klass && klass != self
28
- super
96
+
97
+ super(inflate_scalar_components(data), options)
98
+ end
99
+
100
+ # Accept the flat scalar form that {#to_hash} now emits, and the nested
101
+ # form every stored row was written with. lutaml casts a Hash into a
102
+ # component but passes a String straight through (Attribute#cast_element),
103
+ # so the flat form has to be re-inflated here or it would be assigned raw.
104
+ #
105
+ # Reading BOTH shapes is what keeps published relaton-data indexes
106
+ # deserializing across this change.
107
+ def inflate_scalar_components(data)
108
+ return data unless data.is_a?(::Hash)
109
+
110
+ flat_scalar_components.each_with_object(data.dup) do |(attr, key), acc|
111
+ inflate_scalar_component(acc, attr, key)
112
+ end
113
+ end
114
+
115
+ # The flat-scalar table of this class: FLAT_SCALAR_COMPONENTS, plus the
116
+ # entries a flavor adds for its own classes. CEN/CENELEC adds
117
+ # `publisher` and `copublishers`; a table shared by every flavor would
118
+ # change the wire format of flavors with a published index (IEEE).
119
+ def flat_scalar_components
120
+ FLAT_SCALAR_COMPONENTS
121
+ end
122
+
123
+ # The one field each entry of #flat_scalar_components degenerates to.
124
+ def flat_scalar_fields
125
+ FLAT_SCALAR_FIELDS
126
+ end
127
+
128
+ # Hook: rewrite +hash+, the canonical serialization of +model+ (an
129
+ # instance of this class), into a shorter flavor-specific form. The
130
+ # canonicalizer calls it for every identifier, nested ones included, so
131
+ # it also reaches a `base` or an adopted document. A class that
132
+ # overrides it must also override #inflate_scalar_components to read the
133
+ # short form back. The default changes nothing.
134
+ def compact_hash(_model, _hash); end
135
+
136
+ # @api private
137
+ def inflate_scalar_component(data, attr_name, flat_key)
138
+ return unless component_attribute?(attr_name)
139
+ # A flavor with its OWN converter for the flat key (IEC's
140
+ # year_from_kv) already knows how to read it, and inflating would fight
141
+ # it. A plain `map "edition", to: :edition` is not such a converter —
142
+ # it hands the value to lutaml, which cannot cast a String into a
143
+ # component — so that one still needs inflating.
144
+ return if converted_hash_keys.include?(flat_key)
145
+ # Only when the flat key RENAMES the attribute (`date` -> `year`) can
146
+ # it collide with a different attribute. ~20 flavors model the edition
147
+ # as a real `year` attribute of their own (ASHRAE, ASME, CIE, CSA, JIS,
148
+ # OGC, IEEE …), and folding that into a `date` component silently drops
149
+ # the year on every round trip — the same collision
150
+ # `fold_scalar_aliases` guards against on the constructor path.
151
+ # `edition` keeps its own name, so it is never a collision.
152
+ return if flat_key != attr_name.to_s && declared_attribute?(flat_key)
153
+
154
+ key = data.key?(flat_key) ? flat_key : flat_key.to_sym
155
+ value = data[key]
156
+ return if value.nil? || value.is_a?(::Hash)
157
+
158
+ field = flat_scalar_fields.fetch(attr_name).to_s
159
+ if value.is_a?(::Array)
160
+ # A collection of components (`copublishers`) flattens to a list of
161
+ # scalars; anything else in a list is not the flat form.
162
+ return unless collection_attribute?(attr_name)
163
+ return unless value.all? { |v| scalar_value?(v) }
164
+
165
+ data.delete(key)
166
+ data[attr_name.to_s] = value.map { |v| { field => v.to_s } }
167
+ return
168
+ end
169
+
170
+ data.delete(key)
171
+ data[attr_name.to_s] = { field => value.to_s }
172
+ end
173
+
174
+ # True when +name+ is declared as a collection on this class.
175
+ def collection_attribute?(name)
176
+ attr = attributes[name] || attributes[name.to_s]
177
+ attr ? attr.collection? : false
178
+ end
179
+
180
+ # @api private
181
+ def scalar_value?(value)
182
+ !(value.nil? || value.is_a?(::Hash) || value.is_a?(::Array))
183
+ end
184
+
185
+ # True when +name+ is a declared attribute on this class, under either a
186
+ # Symbol or a String key.
187
+ def declared_attribute?(name)
188
+ attributes.key?(name.to_sym) || attributes.key?(name.to_s)
189
+ end
190
+
191
+ # True when +name+ is declared on this class as a component (a nested
192
+ # Serializable), rather than a plain scalar. The coercions are
193
+ # type-aware: ~16 classes declare `edition` as a plain :string and must
194
+ # keep the scalar they were given.
195
+ def component_attribute?(name)
196
+ attr = attributes[name] || attributes[name.to_s]
197
+ return false unless attr
198
+
199
+ type = attr.type
200
+ type.is_a?(Class) && type <= Lutaml::Model::Serializable
201
+ rescue StandardError
202
+ false
203
+ end
204
+
205
+ # Serialization keys this class reads through a converter of its own
206
+ # (a `key_value` rule declared with `with: {to:, from:}`). Those keys are
207
+ # already handled and must not be rewritten underneath the flavor.
208
+ #
209
+ # Empty for a flavor with no `key_value` block, which serializes
210
+ # attribute names directly.
211
+ def converted_hash_keys
212
+ @converted_hash_keys ||=
213
+ begin
214
+ rules = mappings[:hash]&.mappings || []
215
+ rules.reject { |rule| rule.custom_methods.nil? || rule.custom_methods.empty? }
216
+ .map { |rule| rule.name.to_s }
217
+ rescue StandardError
218
+ []
219
+ end
220
+ end
221
+
222
+ # The blessed constructor path (pubid#360 item 3).
223
+ #
224
+ # Two jobs, both of which lutaml declines to do:
225
+ #
226
+ # * coerce a scalar into the component the attribute declares, so
227
+ # `edition: 2` and `year: 2023` work as written;
228
+ # * refuse a key this class does not know, instead of dropping it.
229
+ #
230
+ # The refusal is the important half. lutaml reads only declared attribute
231
+ # names out of the hash (Serialize#initialize_attributes) and reports
232
+ # nothing about the rest, so `new(number: "1000", year: 2023)` returned an
233
+ # identifier with no year at all and no diagnostic — which is exactly how
234
+ # the issue was filed. A dropped key is a silent wrong answer; a raise is
235
+ # a loud one.
236
+ #
237
+ # Safe to apply here because neither internal rebuild path routes
238
+ # attribute keys through the constructor: `from_hash` calls `new` with no
239
+ # attributes and assigns through setters afterwards, and `#exclude`
240
+ # rebuilds from `self.class.attributes` — only declared names, by
241
+ # construction.
242
+ #
243
+ # @param attrs [Hash] caller-supplied attributes
244
+ # @return [Hash] the same attributes, coerced
245
+ # @raise [ArgumentError] naming every key this class does not declare
246
+ def normalize_init_attributes(attrs)
247
+ return attrs.dup unless attrs.is_a?(::Hash)
248
+
249
+ normalized = attrs.dup
250
+ fold_scalar_aliases(normalized)
251
+ coerce_component_scalars(normalized)
252
+ reject_unknown_keys(normalized)
253
+ normalized
254
+ end
255
+
256
+ # Fold `year:` / `month:` / `day:` into the `date` component. Only when
257
+ # the alias is not itself a declared attribute — ~20 flavors model the
258
+ # edition as a plain `year` attribute, and folding there would destroy it.
259
+ def fold_scalar_aliases(attrs)
260
+ SCALAR_ATTRIBUTE_ALIASES.each do |alias_name, (target, field)|
261
+ key = attrs.key?(alias_name) ? alias_name : alias_name.to_s
262
+ next unless attrs.key?(key)
263
+ next if attributes.key?(alias_name) || attributes.key?(alias_name.to_s)
264
+ next unless component_attribute?(target)
265
+
266
+ value = attrs.delete(key)
267
+ next if value.nil?
268
+
269
+ existing = attrs[target] || attrs[target.to_s] || {}
270
+ existing = component_to_hash(existing)
271
+ attrs.delete(target.to_s)
272
+ attrs[target] = existing.merge(field => value.to_s)
273
+ end
274
+ end
275
+
276
+ # Wrap a scalar in the component its attribute declares. A flavor that
277
+ # declares the attribute as a plain :string keeps the scalar untouched.
278
+ def coerce_component_scalars(attrs)
279
+ FLAT_SCALAR_FIELDS.each do |attr_name, field|
280
+ key = attrs.key?(attr_name) ? attr_name : attr_name.to_s
281
+ next unless attrs.key?(key)
282
+ next unless component_attribute?(attr_name)
283
+
284
+ value = attrs[key]
285
+ next if value.nil? || value.is_a?(::Hash) ||
286
+ value.is_a?(Lutaml::Model::Serialize)
287
+
288
+ attrs[key] = { field => value.to_s }
289
+ end
290
+
291
+ coerce_degenerate_components(attrs)
292
+ end
293
+
294
+ # Wrap a scalar in a degenerate component wherever the attribute declares
295
+ # one, INCLUDING a collection — the residue of pubid#360 item 3.
296
+ #
297
+ # lutaml casts a Hash element into the component but passes a String
298
+ # through untouched, so `languages: ["en"]` stored raw Strings and then
299
+ # raised in the renderer (`undefined method 'code'`) and in the URN
300
+ # generator. The Hash form `languages: [{code: "en"}]` already worked, so
301
+ # this is a coercion gap, not a lutaml limit.
302
+ def coerce_degenerate_components(attrs)
303
+ attrs.keys.each do |key|
304
+ next unless key.is_a?(::Symbol) || key.is_a?(::String)
305
+
306
+ field = degenerate_field_for(key)
307
+ next unless field
308
+
309
+ attrs[key] = wrap_degenerate(attrs[key], field)
310
+ end
311
+ end
312
+
313
+ # The degenerate field of the component +key+ declares, or nil when it
314
+ # declares none. A collection attribute reports its ELEMENT class, so one
315
+ # lookup covers both shapes; the `<=` test is what admits a flavor
316
+ # subclass such as Pubid::Iec::Components::Publisher.
317
+ def degenerate_field_for(key)
318
+ type = declared_component_type(key)
319
+ return nil unless type
320
+
321
+ DEGENERATE_COMPONENT_FIELDS.find { |klass, _| type <= klass }&.last
322
+ end
323
+
324
+ # The component class +key+ declares, or nil when the attribute is
325
+ # absent or declared as a plain scalar.
326
+ def declared_component_type(key)
327
+ type = (attributes[key.to_sym] || attributes[key.to_s])&.type
328
+ type.is_a?(::Class) ? type : nil
329
+ rescue StandardError
330
+ nil
331
+ end
332
+
333
+ # A scalar becomes `{field => scalar}` and an Array is mapped
334
+ # element-wise. nil, a Hash and an already-built component pass through
335
+ # untouched, so every shape that worked before still works.
336
+ def wrap_degenerate(value, field)
337
+ if value.is_a?(::Array)
338
+ return value.map { |v| wrap_degenerate(v, field) }
339
+ end
340
+ return value if value.nil? || value.is_a?(::Hash) ||
341
+ value.is_a?(Lutaml::Model::Serialize)
342
+
343
+ { field => value.to_s }
344
+ end
345
+
346
+ def reject_unknown_keys(attrs)
347
+ allowed = RESERVED_INIT_KEYS + extra_init_keys
348
+ unknown = attrs.keys.reject do |key|
349
+ # A key that is neither a Symbol nor a String cannot name an
350
+ # attribute, so it is unknown by definition — and `to_sym` on it
351
+ # would raise NoMethodError instead of reporting that.
352
+ next false unless key.is_a?(::Symbol) || key.is_a?(::String)
353
+
354
+ allowed.include?(key.to_sym) || declared_attribute?(key)
355
+ end
356
+ return if unknown.empty?
357
+
358
+ raise ArgumentError,
359
+ "unknown attribute#{'s' if unknown.size > 1} for " \
360
+ "#{name || self}: #{unknown.map(&:to_s).sort.join(', ')}"
361
+ end
362
+
363
+ # Constructor parameters a flavor accepts that are not lutaml attributes.
364
+ # IEEE takes `code:` and `draft:` this way — runtime-only values its
365
+ # `initialize` turns into component objects. Override to widen.
366
+ #
367
+ # @return [Array<Symbol>]
368
+ def extra_init_keys
369
+ []
370
+ end
371
+
372
+ # @api private
373
+ def component_to_hash(value)
374
+ case value
375
+ when ::Hash then value.transform_keys(&:to_sym)
376
+ when Lutaml::Model::Serialize then value.to_hash.transform_keys(&:to_sym)
377
+ else {}
378
+ end
29
379
  end
30
380
 
31
381
  # lutaml's nested polymorphic cast (Attribute#cast → apply_mappings)
@@ -91,7 +441,22 @@ module Pubid
91
441
  def identifier_registry_classes
92
442
  (scanned_identifier_classes +
93
443
  registered_identifier_classes +
94
- additional_identifier_classes).uniq
444
+ additional_identifier_classes +
445
+ own_base_class).uniq
446
+ end
447
+
448
+ # The flavor's own Identifier base, when a builder instantiates it
449
+ # directly (BSI does, for the members of a bundled identifier). Such an
450
+ # object serializes with `_type: "pubid:<flavor>:identifier"`, and
451
+ # without an entry here that type resolves to nothing — so a nested
452
+ # polymorphic attribute typed ::Pubid::Identifier silently deserialized
453
+ # the member as the abstract root instead of the flavor class. Added
454
+ # LAST, so a concrete leaf that somehow claimed the same name still wins.
455
+ def own_base_class
456
+ return [] if self == ::Pubid::Identifier
457
+ return [] unless name && polymorphic_name
458
+
459
+ [self]
95
460
  end
96
461
 
97
462
  # Concrete identifier classes found directly in the flavor's `Identifiers`
@@ -133,9 +498,17 @@ module Pubid
133
498
  end
134
499
 
135
500
  attribute :_type, :string, polymorphic_class: true
136
- attribute :number, Components::Code
137
- attribute :part, Components::Code
138
- attribute :subpart, Components::Code
501
+ # Plain strings, for every flavor. A Components::Code here held nothing
502
+ # but its `value` on the identifier path, and the disagreement between
503
+ # this declaration and the leaves that redeclared a scalar was the
504
+ # determinism landmine: a redeclaration on an inherited-from class
505
+ # resolves nondeterministically under multi-flavor load. With the base
506
+ # declaring the scalar, no flavor redeclares and the bug class is gone.
507
+ # A flavor that needs structure keeps its own attributes (ISO's committee
508
+ # codes, NIST's series/subseries, ITU's code), never a boxed `number`.
509
+ attribute :number, :string
510
+ attribute :part, :string
511
+ attribute :subpart, :string
139
512
  attribute :stage_iteration, Components::Iteration
140
513
  attribute :date, Components::Date
141
514
  attribute :edition, Components::Edition
@@ -194,6 +567,137 @@ module Pubid
194
567
  model.class.attributes(register).each do |name, attr|
195
568
  canonicalize_attr(model, hash, name, attr) unless name == :_type
196
569
  end
570
+ flatten_scalar_components(model, hash)
571
+ # Nested components are not identifiers and have no such hook.
572
+ if model.class.respond_to?(:compact_hash)
573
+ model.class.compact_hash(model, hash)
574
+ end
575
+ end
576
+
577
+ # A component that carries only ONE meaningful value serializes as that
578
+ # value: `edition: "2"`, not `edition: {number: "2"}`; `year: "2024"`, not
579
+ # `date: {year: "2024"}`. That is the shape an index row wants to be read
580
+ # in, and the shape a caller can write without knowing pubid's component
581
+ # classes — the complaint in pubid#360 item 3.
582
+ #
583
+ # The test is per INSTANCE, not per flavor, because one flavor's corpus
584
+ # holds both shapes. Of 9,730 dated IEC identifiers, 9,728 carry a year
585
+ # alone but 10 carry a month (`IEC CA 01:2025-10`) and 2 are undated
586
+ # (`IEC 60050:--`); a flavor-wide switch would have discarded those.
587
+ #
588
+ # Flavors that already emit the flat form are untouched for free: IEC and
589
+ # ISO serialize the date as top-level year/month/day, so no "date" key ever
590
+ # reaches this method.
591
+ def flatten_scalar_components(model, hash)
592
+ flat_scalar_table(model).each do |attr_name, flat_key|
593
+ flatten_scalar_component(model, hash, attr_name, flat_key)
594
+ end
595
+ end
596
+
597
+ # The flat-scalar table of +model+'s class. The canonicalizer also runs on
598
+ # nested components, which are not identifiers and have only the shared
599
+ # tables.
600
+ def flat_scalar_table(model, fields: false)
601
+ klass = model.class
602
+ if klass.respond_to?(:flat_scalar_components)
603
+ fields ? klass.flat_scalar_fields : klass.flat_scalar_components
604
+ else
605
+ fields ? FLAT_SCALAR_FIELDS : FLAT_SCALAR_COMPONENTS
606
+ end
607
+ end
608
+
609
+ # Replace +hash+'s serialization of +attr_name+ with a bare scalar, when
610
+ # the component holds nothing but its single significant field.
611
+ def flatten_scalar_component(model, hash, attr_name, flat_key)
612
+ key = hash.key?(attr_name.to_s) ? attr_name.to_s : attr_name
613
+ return unless hash.key?(key)
614
+ return unless model.respond_to?(attr_name)
615
+
616
+ field = flat_scalar_table(model, fields: true).fetch(attr_name)
617
+ if hash[key].is_a?(::Array)
618
+ return flatten_scalar_collection(model, hash, attr_name, key, field)
619
+ end
620
+ return unless hash[key].is_a?(::Hash)
621
+
622
+ component = model.public_send(attr_name)
623
+ return unless component.is_a?(Lutaml::Model::Serialize)
624
+
625
+ scalar = degenerate_scalar(component, field)
626
+ return if scalar.nil?
627
+ # Never overwrite a key the flavor already emits under the flat name,
628
+ # and never claim a name the flavor declares as a real attribute of its
629
+ # own — ~20 flavors have a `year` attribute, and renaming `date` onto it
630
+ # would make the two indistinguishable on the way back in.
631
+ if flat_key != key.to_s
632
+ return if hash.key?(flat_key) || model.class.declared_attribute?(flat_key)
633
+ end
634
+
635
+ replace_key_in_place(hash, key, flat_key, scalar)
636
+ end
637
+
638
+ # A collection of components (`copublishers`) serializes as a list of
639
+ # scalars, but only when EVERY element is degenerate; a list that mixes
640
+ # the two shapes could not be read back. The key keeps its name.
641
+ def flatten_scalar_collection(model, hash, attr_name, key, field)
642
+ components = model.public_send(attr_name)
643
+ return unless components.is_a?(::Array) &&
644
+ components.size == hash[key].size &&
645
+ components.all?(Lutaml::Model::Serialize)
646
+
647
+ scalars = components.map { |c| degenerate_scalar(c, field) }
648
+ return if scalars.any?(&:nil?)
649
+
650
+ hash[key] = scalars
651
+ end
652
+
653
+ # Replace +old_key+ with +new_key+ WITHOUT moving it to the end.
654
+ #
655
+ # A plain delete-then-assign appends, which would reorder every key after
656
+ # the one being flattened — cosmetic for `==`, but it rewrites the byte
657
+ # order of every generated relaton-data YAML row for no reason.
658
+ def replace_key_in_place(hash, old_key, new_key, value)
659
+ if old_key.to_s == new_key
660
+ hash[old_key] = value
661
+ return
662
+ end
663
+
664
+ rebuilt = hash.each_with_object({}) do |(k, v), acc|
665
+ if k == old_key
666
+ acc[new_key] = value
667
+ else
668
+ acc[k] = v
669
+ end
670
+ end
671
+
672
+ hash.replace(rebuilt)
673
+ end
674
+
675
+ # The scalar +component+ degenerates to, or nil when it carries anything
676
+ # besides +field+.
677
+ #
678
+ # A +field+ holding another component (ISO's edition number is a
679
+ # Components::Code) is deliberately NOT flattened: the flat form would come
680
+ # back as a plain String, so `from_hash(to_hash) == parse` would break —
681
+ # the silent inequality that CLAUDE.md records as the costliest failure
682
+ # mode in this codebase, because `#matches?` is built on `==`.
683
+ def degenerate_scalar(component, field)
684
+ register = component.lutaml_register
685
+ found = nil
686
+
687
+ component.class.attributes(register).each do |name, attr|
688
+ value = component.public_send(name)
689
+ # `Utils.empty?(nil)` is false, so nil needs its own test — an
690
+ # unpopulated attribute is exactly what makes a component degenerate.
691
+ next if value.nil? || Lutaml::Model::Utils.empty?(value)
692
+ next if attr.default_set?(register, component) &&
693
+ value == attr.default(register, component)
694
+ return nil unless name == field
695
+ return nil if value.is_a?(Lutaml::Model::Serialize)
696
+
697
+ found = value
698
+ end
699
+
700
+ found&.to_s
197
701
  end
198
702
 
199
703
  # Canonicalize a single attribute against its serialized value in +hash+:
@@ -240,10 +744,13 @@ module Pubid
240
744
  end
241
745
  end
242
746
  private :canonicalize_hash, :canonicalize_attr, :default_valued?,
243
- :canonicalize_nested, :canonicalize_collection
747
+ :canonicalize_nested, :canonicalize_collection,
748
+ :flatten_scalar_components, :flatten_scalar_component,
749
+ :flatten_scalar_collection, :flat_scalar_table,
750
+ :degenerate_scalar, :replace_key_in_place
244
751
 
245
752
  def initialize(attrs = {}, options = {})
246
- attrs = attrs.dup
753
+ attrs = self.class.normalize_init_attributes(attrs)
247
754
  attrs[:_type] ||= self.class.polymorphic_name
248
755
  super
249
756
  end
@@ -278,13 +785,75 @@ module Pubid
278
785
  raise ArgumentError, "No renderer registered for format: #{format}"
279
786
  end
280
787
 
281
- # `:trademark` is a render-time flag consumed by the renderer, not the
282
- # rendering context — and some flavors override build_rendering_context
283
- # with a strict signature, so strip it here.
284
- ctx_opts = opts.except(:trademark)
788
+ # `:trademark` (IEEE) and `:with_volume` (ECMA) are render-time flags
789
+ # consumed by the renderer, not the rendering context — and some flavors
790
+ # override build_rendering_context with a strict signature, so strip them
791
+ # here rather than widening that signature.
792
+ ctx_opts = opts.except(:trademark, :with_volume)
285
793
  context = build_rendering_context(renderer, format:, **ctx_opts)
286
- render_opts = opts.slice(:with_edition, :trademark)
287
- renderer.new(self).render(context:, **render_opts)
794
+ render_opts = opts.slice(:with_edition, :trademark, :with_volume)
795
+ result = renderer.new(self).render(context:, **render_opts)
796
+
797
+ annotate_rendered(result, format, ctx_opts[:annotated], context)
798
+ end
799
+
800
+ # Apply semantic spans when the renderer did not.
801
+ #
802
+ # `context.annotated` reaches all 39 flavor renderers and all 39 ignore it,
803
+ # so annotation was a v1 -> v2 regression for every flavor but ISO. This is
804
+ # the single choke point every renderer already passes through, which is
805
+ # why the fallback lives here rather than in each renderer.
806
+ #
807
+ # The `<span` test is what keeps ISO's exact, render-time placement: a
808
+ # renderer that annotated itself is left alone.
809
+ def annotate_rendered(result, format, annotated, context)
810
+ return result unless annotated && format == :human
811
+ return result unless result.is_a?(String)
812
+ return result if result.include?("<span")
813
+
814
+ Renderers::Annotator.new(self, context).annotate(result)
815
+ end
816
+ private :annotate_rendered
817
+
818
+ # The same fallback, for a `to_s` that composes its string by hand instead
819
+ # of going through {#render} — ITU, CCSDS, CIE and CSA wrappers all do.
820
+ # Those never reach the choke point above, so they apply it on the way out.
821
+ #
822
+ # Every such `to_s` must also ACCEPT `annotated:`. Before they did, 13 type
823
+ # families raised on the flag (`wrong number of arguments (given 1,
824
+ # expected 0)` for the ones declaring no parameters at all), which is worse
825
+ # than a missing span: a wrapper is exactly the shape a consumer renders.
826
+ #
827
+ # This is deliberately a per-class opt-in rather than a module prepended to
828
+ # every identifier class. That was tried: prepending to ~500 ancestor
829
+ # chains made a GOST identical adoption render its adopted ISO Technical
830
+ # Report as `ISO TR …` instead of `ISO/TR …`, but only under full-suite
831
+ # load — the multi-flavor nondeterminism this file warns about elsewhere.
832
+ # An explicit call in the handful of classes that need it changes no
833
+ # ancestor chain and cannot have that effect.
834
+ #
835
+ # @param rendered [String] the plain rendering this method produced
836
+ # @param opts [Hash] the render options it was called with
837
+ # @return [String] annotated when `annotated:` was asked for, else as-is
838
+ def annotate_plain_render(rendered, **opts)
839
+ annotate_rendered(rendered, :human, opts[:annotated], nil)
840
+ end
841
+ protected :annotate_plain_render
842
+
843
+ # The tokens {Renderers::Annotator} looks for in this identifier's rendered
844
+ # string, as [attribute_name, css_class] pairs.
845
+ #
846
+ # The shared table names the components a printed identifier usually
847
+ # carries — publisher, type, number, part, year. A flavor that keeps its
848
+ # identity somewhere else answers with its own names appended: IEC's
849
+ # working documents are `technical_committee`/`wd_number`/`wd_stage`, and
850
+ # CSA's electrical code prints `cec_part`/`no_number` rather than the
851
+ # `number` it stores. Without this they carried no annotatable token at all.
852
+ #
853
+ # Appending rather than replacing keeps whatever the shared table does
854
+ # find; a token whose value is not in the string is skipped anyway.
855
+ def annotation_tokens
856
+ Renderers::Annotator::TOKENS
288
857
  end
289
858
 
290
859
  def to_s(**opts)
@@ -343,6 +912,19 @@ module Pubid
343
912
  segments.compact.join("-").downcase
344
913
  end
345
914
 
915
+ # Join MR segments and return nil rather than "" when nothing survives.
916
+ #
917
+ # Renderers::MrString does `parts.compact.join(".")`, and `compact` removes
918
+ # nil but NOT an empty string — so a hook that returns "" contributes a
919
+ # blank segment and the slug gains a double dot. The base
920
+ # #mr_number_with_part guards this with its own `return nil unless num`;
921
+ # flavor overrides that append their own marker to `super` need the same
922
+ # guard, and this is it.
923
+ def mr_join(*segments)
924
+ joined = segments.compact.reject { |s| s.to_s.empty? }.join("-")
925
+ joined.empty? ? nil : joined
926
+ end
927
+
346
928
  def mr_number
347
929
  number&.to_s&.downcase
348
930
  end
@@ -372,6 +954,16 @@ module Pubid
372
954
  "ed#{edition.number}"
373
955
  end
374
956
 
957
+ # Hook for flavors whose identity includes a volume discriminator — ECMA-269
958
+ # ed3 vol1..vol4 are four distinct documents sharing one docidentifier.
959
+ # Literally nil here, NOT `volume&.to_s`: Pubid::Nist::Identifiers::Base
960
+ # declares its own `volume` attribute with different MR semantics, so a
961
+ # generic reader would change NIST's slug. A flavor that needs the segment
962
+ # overrides this (see Pubid::Ecma::Identifier#mr_volume).
963
+ def mr_volume
964
+ nil
965
+ end
966
+
375
967
  def mr_languages
376
968
  return nil unless languages&.any?
377
969
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "pubid"
3
4
  module Pubid
4
5
  # Machine-readable metadata for identifier classes
5
6
  # Provides a standardized way to describe identifier types for: