Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Dec 3, 2024
1 parent e1ed1e1 commit f48e54f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 56 deletions.
16 changes: 1 addition & 15 deletions lib/relaton/bib/formattedref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ module Relaton
module Bib
class Formattedref
# @return [String]
attr_reader :content
attr_accessor :content

def initialize(content: nil)
@content = content
end

def content=(content)
@content = content.is_a?(String) ? content : content.to_xml
end

# @param content [Relaton::M
# def content=(content)
# @content = content.is_a?(String) ? Model::TextElement.from_xml(content) : content
# end

# @param [Nokogiri::XML::Builder]
# def to_xml(builder)
# builder.formattedref { super }
# end

# @param prefix [String]
# @return [String]
def to_asciibib(prefix = "")
Expand Down
30 changes: 2 additions & 28 deletions lib/relaton/model/formattedref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,11 @@ class Formattedref < Lutaml::Model::Serializable

model Bib::Formattedref

attribute :content, TextElement # , collection: true
attribute :content, :string

xml do
root "formattedref"
map_content to: :text, delegate: :content # , with: { to: :content_to_xml, from: :content_from_xml }
map_element "em", to: :em, delegate: :content # , with: { to: :em_to_xml, from: :em_from_xml }
map_element "strong", to: :strong, delegate: :content # , with: { to: :strong_to_xml, from: :strong_from_xml }
end

def content_to_xml(model, parent, doc)
parent << doc.create_element("content", model.content)
end

def content_from_xml(model, value)
model.content << value
end

def em_from_xml(model, value)
model.content << value.to_xml
end

def em_to_xml(model, parent, doc)
parent << doc.create_element("em", model.content)
end

def strong_from_xml(model, value)
model.content << value.to_xml
end

def strong_to_xml(model, parent, doc)
parent << doc.create_element("strong", model.content)
map_all to: :content
end
end
end
Expand Down
14 changes: 1 addition & 13 deletions spec/relaton/model/formattedref_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@
it "parse XML" do
formattedref = described_class.from_xml xml
expect(formattedref).to be_instance_of Relaton::Bib::Formattedref
expect(formattedref.content).to be_instance_of Array
expect(formattedref.content.size).to eq 3
expect(formattedref.content[0]).to be_instance_of Relaton::Model::TextElement
expect(formattedref.content[1]).to be_instance_of Relaton::Model::TextElement
expect(formattedref.content[2]).to be_instance_of Relaton::Model::TextElement
expect(described_class.to_xml(formattedref)).to be_equivalent_to xml
end

it "parse XML" do
formattedref = Relaton::Bib::Formattedref.new
formattedref.content = "<strong>Strong</strong>content<em>Em</em>"
expect(formattedref.content).to be_instance_of Array
expect(formattedref.content.size).to eq 3
expect(formattedref.content).to eq "<strong>Strong</strong>Content<em>Em</em>"
expect(described_class.to_xml(formattedref)).to be_equivalent_to xml
end
end

0 comments on commit f48e54f

Please sign in to comment.