Class: ONIX::SubsetDSL
Overview
DSL
Direct Known Subclasses
Addressee, AncillaryContent, Audience, AudienceRange, Barcode, BaseDate, CitedContent, CollateralDetail, Collection, CollectionSequence, ComparisonProductPrice, Complexity, ContentDetail, ContentItem, Contributor, ContributorPlace, CopyrightStatement, DescriptiveDetail, Discount, DiscountCoded, Entity, EpubLicense, EpubLicenseExpression, EpubUsageConstraint, EpubUsageLimit, Extent, Header, Identifier, Imprint, Language, Market, MarketPublishingDetail, Measure, ONIX21::SubsetDSL, ONIXMessage, PageRun, Price, Prize, Product, ProductClassification, ProductContact, ProductFormFeature, ProductPart, ProductSupply, ProfessionalAffiliation, PublisherRepresentative, PublishingDetail, RelatedMaterial, RelatedProduct, RelatedWork, ResourceFeature, ResourceVersion, ResourceVersionFeature, ReturnsConditions, ReviewRating, Root, SalesOutlet, SalesRestriction, SalesRights, Sender, Subject, SupplierOwnCoding, SupplyContact, SupplyDetail, SupportingResource, Tax, Territory, TextContent, TextItem, TitleDetail, TitleElement, Website
Instance Attribute Summary
Attributes included from Attributes
Class Method Summary collapse
-
.element(name, type, options = {}) ⇒ void
define unique element.
-
.elements(name, type, options = {}) ⇒ void
define multiple elements shortcut for element :array=>true.
- .get_class(name) ⇒ Object
- .inherited(sublass) ⇒ Object
-
.n ⇒ Object
infinity constant for cardinality.
-
.ref_to_short(name) ⇒ String
convert reference name notation to short.
- .register_elements(elements) ⇒ Object
- .register_scopes(scopes) ⇒ Object
-
.registered_elements ⇒ Hash
registered elements for this subset.
- .registered_scopes ⇒ Object
-
.scope(name, lambda) ⇒ void
define a scope.
-
.short_to_ref(name) ⇒ String
convert short name notation to reference.
Instance Method Summary collapse
- #get_class(name) ⇒ Object
-
#initialize ⇒ SubsetDSL
constructor
A new instance of SubsetDSL.
- #parse(n) ⇒ void
- #register_subset_array(e) ⇒ Object
- #registered_elements ⇒ Object
Methods inherited from Subset
parse, #tag_match, #unsupported
Methods included from Attributes
attribute_class, #parse_attributes, #serialized_attributes
Constructor Details
#initialize ⇒ SubsetDSL
Returns a new instance of SubsetDSL.
391 392 393 394 395 396 397 398 |
# File 'lib/onix/subset.rb', line 391 def initialize # initialize plural as Array self.registered_elements.values.each do |e| if e.is_array? register_subset_array(e) end end end |
Class Method Details
.element(name, type, options = {}) ⇒ void
This method returns an undefined value.
define unique element
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/onix/subset.rb', line 324 def element(name, type, = {}) @elements ||= {} @elements[name] = ElementParser.new(name, type, ) short_name = self.ref_to_short(name) if short_name @elements[short_name] = @elements[name].dup @elements[short_name].short = true end attr_accessor @elements[name].to_sym alias_method "#{@elements[name].underscore_name}_with_attributes".to_sym, @elements[name].to_sym current_element = @elements[name] define_method current_element.to_sym do |args = nil| val = instance_variable_get(current_element.to_instance) if val.respond_to?(:__getobj__) val.__getobj__ else if val.is_a?(SubsetArray) and val.first and val.first.is_a?(TextWithAttributes) val.map { |v| v.respond_to?(:__getobj__) ? v.__getobj__ : v } else val end end end if @elements[name].shortcut current_element = @elements[name] alias_method "#{current_element.shortcut.to_s}_with_attributes".to_sym, "#{@elements[name].underscore_name}_with_attributes".to_sym alias_method current_element.shortcut, @elements[name].to_sym end @elements[name] end |
.elements(name, type, options = {}) ⇒ void
This method returns an undefined value.
define multiple elements shortcut for element :array=>true
366 367 368 |
# File 'lib/onix/subset.rb', line 366 def elements(name, type, = {}) self.element(name, type, .merge(:array => true)) end |
.get_class(name) ⇒ Object
381 382 383 |
# File 'lib/onix/subset.rb', line 381 def get_class(name) ONIX.const_get(name) if ONIX.const_defined?(name) end |
.inherited(sublass) ⇒ Object
385 386 387 388 |
# File 'lib/onix/subset.rb', line 385 def inherited(sublass) sublass.register_scopes(self.registered_scopes) sublass.register_elements(self.registered_elements) end |
.n ⇒ Object
infinity constant for cardinality
297 298 299 |
# File 'lib/onix/subset.rb', line 297 def n Float::INFINITY end |
.ref_to_short(name) ⇒ String
convert reference name notation to short
292 293 294 |
# File 'lib/onix/subset.rb', line 292 def ref_to_short(name) RefToShort.names[name] end |
.register_elements(elements) ⇒ Object
376 377 378 379 |
# File 'lib/onix/subset.rb', line 376 def register_elements(elements) @elements ||= {} @elements.merge!(elements) end |
.register_scopes(scopes) ⇒ Object
314 315 316 317 |
# File 'lib/onix/subset.rb', line 314 def register_scopes(scopes) @scopes ||= {} @scopes = scopes.merge(@scopes) end |
.registered_elements ⇒ Hash
registered elements for this subset
372 373 374 |
# File 'lib/onix/subset.rb', line 372 def registered_elements @elements || {} end |
.registered_scopes ⇒ Object
310 311 312 |
# File 'lib/onix/subset.rb', line 310 def registered_scopes @scopes || {} end |
.scope(name, lambda) ⇒ void
This method returns an undefined value.
define a scope
305 306 307 308 |
# File 'lib/onix/subset.rb', line 305 def scope(name, lambda) @scopes ||= {} @scopes[name] = lambda end |
.short_to_ref(name) ⇒ String
convert short name notation to reference
285 286 287 |
# File 'lib/onix/subset.rb', line 285 def short_to_ref(name) ShortToRef.names[name] end |
Instance Method Details
#get_class(name) ⇒ Object
420 421 422 |
# File 'lib/onix/subset.rb', line 420 def get_class(name) self.class.get_class(name) end |
#parse(n) ⇒ void
This method returns an undefined value.
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/onix/subset.rb', line 426 def parse(n) parse_attributes(n.attributes) n.elements.each do |t| e = self.registered_elements[t.name] if e primitive = true case e.type when :subset klass = self.get_class(e.class_name) unless klass raise UnknownElement, e.class_name end val = klass.parse(t) primitive = false when :text val = t.text when :integer val = t.text.to_i when :float val = t.text.to_f when :bool val = true when :date fmt = t["dateformat"] || "00" begin val = ONIX::Helper.to_date(fmt, t.text) rescue val = t.text end when :datestamp tm = t.text datestamp = DateStamp.new datestamp.parse(tm) val = datestamp when :ignore val = nil else val = t.text end if val if primitive && t.attributes.length > 0 if t.attributes["textformat"] && t.attributes["textformat"].to_s == "05" # content is XHTML xhtml = CGI.unescapeHTML(t.children.map { |x| x.to_s }.join.strip) if Nokogiri::XML.parse(xhtml).root # check if val is really XHTML val = xhtml else xhtml = CGI.unescapeHTML(val) if Nokogiri::XML.parse(xhtml).root val = xhtml end end end val = TextWithAttributes.new(val) val.parse(t.attributes) end if e.is_array? instance_variable_get(e.to_instance).send(:push, val) else instance_variable_set(e.to_instance, e.parse_lambda(val)) end end else unsupported(t) end end end |
#register_subset_array(e) ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/onix/subset.rb', line 400 def register_subset_array(e) # register a contextual SubsetArray object subset_array = SubsetArray.new subset_klass = self.get_class(e.class_name) if subset_klass.respond_to?(:registered_scopes) subset_klass.registered_scopes.each do |n, l| unless subset_array.respond_to?(n.to_s) subset_array.define_singleton_method(n.to_s) do instance_exec(&l) end end end end instance_variable_set(e.to_instance, subset_array) end |
#registered_elements ⇒ Object
416 417 418 |
# File 'lib/onix/subset.rb', line 416 def registered_elements self.class.registered_elements end |