https://github.com/jasonlittleton24/activerecordmodel-serializers-xml
This gem provides XML serialization for your Active Model objects and Active Record models.
https://github.com/jasonlittleton24/activerecordmodel-serializers-xml
ruby ruby-on-rails
Last synced: 2 months ago
JSON representation
This gem provides XML serialization for your Active Model objects and Active Record models.
- Host: GitHub
- URL: https://github.com/jasonlittleton24/activerecordmodel-serializers-xml
- Owner: jasonlittleton24
- License: mit
- Created: 2023-09-19T03:08:45.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-19T03:11:40.000Z (almost 3 years ago)
- Last Synced: 2025-02-24T11:50:41.662Z (over 1 year ago)
- Topics: ruby, ruby-on-rails
- Language: Ruby
- Homepage:
- Size: 58.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# ActiveModel::Serializers::Xml
This gem provides XML serialization for your Active Model objects and Active Record models.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'activemodel-serializers-xml'
```
And then execute:
```
$ bundle
```
Or install it yourself as:
```
$ gem install activemodel-serializers-xml
```
## Usage
### ActiveModel::Serializers::Xml
To use the `ActiveModel::Serializers::Xml` you only need to change from
`ActiveModel::Serialization` to `ActiveModel::Serializers::Xml`.
```ruby
class Person
include ActiveModel::Serializers::Xml
attr_accessor :name
def attributes
{'name' => nil}
end
end
```
With the `to_xml` you have an XML representing the model.
```ruby
person = Person.new
person.to_xml # => "\n\n \n\n"
person.name = "Bob"
person.to_xml # => "\n\n Bob\n\n"
```
From an XML string you define the attributes of the model.
You need to have the `attributes=` method defined on your class:
```ruby
class Person
include ActiveModel::Serializers::Xml
attr_accessor :name
def attributes=(hash)
hash.each do |key, value|
send("#{key}=", value)
end
end
def attributes
{'name' => nil}
end
end
```
Now it is possible to create an instance of person and set the attributes using `from_xml`.
```ruby
xml = { name: 'Bob' }.to_xml
person = Person.new
person.from_xml(xml) # => #
person.name # => "Bob"
```
### ActiveRecord::XmlSerializer
This gem also provides serialization to XML for Active Record.
Please see ActiveRecord::Serialization#to_xml for more information.
## Contributing to ActiveModel::Serializers::Xml
ActiveModel::Serializers::Xml is work of many contributors. You're encouraged to submit pull requests, propose features and discuss issues.