require "test_helper" # # # 0836217462 # Being a Dog Is a Full-Time Job # # Charles M Schulz # 1922-11-26 # 2000-02-12 # # # Peppermint Patty # 1966-08-22 # bold, brash and tomboyish # # # Snoopy # 1950-10-04 # extroverted beagle # # # Schroeder # 1951-05-30 # brought classical music to the Peanuts strip # # # # Lucy # 1952-03-03 # bossy, crabby and selfish # # # # Theoretically, every property (scalar) needs/should have a namespace. # property :name, namespace: "http://eric.van-der-vlist.com/ns/person" # # This is implicit if contained: # class Person < Decorator # namespace: "http://eric.van-der-vlist.com/ns/person" # property :name #, namespace: "http://eric.van-der-vlist.com/ns/person" # end # class Library # namespace "http://eric.van-der-vlist.com/ns/library" # namespace_def lib: "http://eric.van-der-vlist.com/ns/library" # namespace_def person: "http://eric.van-der-vlist.com/ns/person" # # namespace_def person: Person # property :person, decorator: Person # end class NamespaceXMLTest < Minitest::Spec module Model Library = Struct.new(:book) Book = Struct.new(:id, :isbn) end let (:book) { Model::Book.new(1, 666) } #:simple-class class Library < Representable::Decorator feature Representable::XML feature Representable::XML::Namespace namespace "http://eric.van-der-vlist.com/ns/library" property :book do namespace "http://eric.van-der-vlist.com/ns/library" property :id, attribute: true property :isbn end end #:simple-class end # default namespace for library it "what" do Library.new(Model::Library.new(book)).to_xml.must_xml( #:simple-xml %{ 666 } #:simple-xml end ) end end class Namespace2XMLTest < Minitest::Spec module Model Library = Struct.new(:book) Book = Struct.new(:id, :isbn, :author, :character) Character = Struct.new(:name, :born, :qualification) end let (:book) { Model::Book.new(1, 666, Model::Character.new("Fowler"), [Model::Character.new("Frau Java", 1991, "typed")]) } #:map-class class Library < Representable::Decorator feature Representable::XML feature Representable::XML::Namespace namespace "http://eric.van-der-vlist.com/ns/library" namespace_def lib: "http://eric.van-der-vlist.com/ns/library" namespace_def hr: "http://eric.van-der-vlist.com/ns/person" property :book, class: Model::Book do namespace "http://eric.van-der-vlist.com/ns/library" property :id, attribute: true property :isbn property :author, class: Model::Character do namespace "http://eric.van-der-vlist.com/ns/person" property :name property :born end collection :character, class: Model::Character do namespace "http://eric.van-der-vlist.com/ns/library" property :qualification property :name, namespace: "http://eric.van-der-vlist.com/ns/person" property :born, namespace: "http://eric.van-der-vlist.com/ns/person" end end end #:map-class end it "renders" do Library.new(Model::Library.new(book)).to_xml.must_xml( #:map-xml %{ 666 Fowler typed Frau Java 1991 } #:map-xml end ) end it "parses" do lib = Model::Library.new #:parse-call Library.new(lib).from_xml(%{ 666 Fowler typed Frau Java Mr. Ruby Dr. Elixir 1991 } #:parse-call end ) lib.book.inspect.must_equal %{#, character=[#]>} #:parse-res lib.book.character[0].name #=> "Frau Java" #:parse-res end end end