-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support collections using custom classes #215
Comments
Another use case needed by @andrew2net at:
|
From the original issue, we should support:
Or |
e.g. class Ceramic < Lutaml::Model::Serialization
attribute :name, :string
attribute :creation_date, :date_time
xml do
root "ceramic"
map_element "name", to: :name
map_attribute "creation-date", to: creation_date
end
end
class CeramicCollection < Lutaml::Model::Collection
attribute :identifier, :string
values Ceramic
xml do
root "ceramic-collection"
map_attribute "identifier", to: :identifier
map_values
end
end <ceramic-collection identifier="my_id">
<ceramic creation-date="2025-01-01">
<name>Picasso collection #3213</name>
</ceramic>
<ceramic creation-date="2025-01-01">
<name>Picasso collection #3223</name>
</ceramic>
</ceramic-collection> |
@andrew2net can you help provide some examples of how RelatonCollection differs from a normal Array? Then we can see how we can implement the functionality here. Thanks. |
@ronaldtse sure. Let's say we have XML data <bibitem>
<title>Title 1</title>
<title>Title 2</title>
</bibitem> We can handle the XML using the classes class Title < Lutaml::Model::Serializable
attribute :content, :string
xml do
root "title"
map_content to: :content
end
end
class BibItem < Lutaml::Model::Serializable
attribute :title, Title, collection: true
xml do
root "bibitem"
map_element "title", to: :title
end
end
> bibitem = Bibitem.from_xml xml
> bibitem.title.class # => Array It's okay until we need a custom collection instead of an array with some extra methods. I think one of the ways is to use a custom collection class as class TitleCollection
...
end
class BibItem < Lutaml::Model::Serializable
attribute :title, Title, collection: TitleCollection
xml do
root "bibitem"
map_element "title", to: :title
end
end
> bibitem = Bibitem.from_xml xml
> bibitem.title.class # => TitleCollection @HassanAkbar Maybe we can use custom serialization methods, but in this case, we have to implement custom methods for each format. class BibItem < Lutaml::Model::Serializable
attribute :title, :string, collection: true
xml do
root "bibitem"
map_element "title", to: :title, with: { from: :title_from_xml }
end
def title_from_xml(mode, value)
model.title = TitleCollection.new value
end
end
...with argument for mapping 'title' requires :to and :from keys (Lutaml::Model::IncorrectMappingArgumentsError) |
Agree. We should be able to use one default method and use one custom method. |
as mentioned here by @ronaldtse -> glossarist/glossarist-ruby#118 (comment)
The text was updated successfully, but these errors were encountered: