https://github.com/am-kantox/aston
Helper to produce JSON hashes isomorphic to XML
https://github.com/am-kantox/aston
Last synced: 23 days ago
JSON representation
Helper to produce JSON hashes isomorphic to XML
- Host: GitHub
- URL: https://github.com/am-kantox/aston
- Owner: am-kantox
- License: mit
- Created: 2024-02-08T13:13:24.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-19T15:11:58.000Z (about 1 year ago)
- Last Synced: 2025-03-14T19:39:00.768Z (about 1 month ago)
- Language: Ruby
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Aston
The tiny library providing a tooling to deal with _ASTON_, which is like _JSON_, but isomorphic to _XML_.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'aston'
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install aston
## Usage
```ruby
Aston.new(:root).
then { |a| a.put_attribute([:bar, :baz], 'attr', 'value') }.
then { |a| a.put_content([:bar, :baz], 'Hello, world!') }
#⇒ #,
# @content=
# #,
# @content=
# #"value"}>,
# @content=#,
# @name=:baz>]>,
# @name=:bar>]>,
# @name=:root>
```### `#to_s`
```ruby
puts Aston.new(:root).
then { |a| a.put_attribute([:bar, :baz], 'attr', 'value') }.
then { |a| a.put_content([:bar, :baz], "Hello, world!") }.
to_s
``````xml
Hello, world!
```
### `#to_json`
```ruby
puts Aston.new(:root).
then { |a| a.put_attribute([:bar, :baz], 'attr', 'value') }.
then { |a| a.put_content([:bar, :baz], "Hello, world!") }.
to_json
#⇒ {"name":"root","attributes":{},"content":[
# {"name":"bar","attributes":{},"content":[
# {"name":"baz","attributes":{"attr":"value"},"content":["Hello, world!"]}]}]}
```### `Aston#parse_hash`
```ruby
json = Aston.new(:root).
then { |a| a.put_attribute([:bar, :baz], 'attr', 'value') }.
then { |a| a.put_content([:bar, :baz], "Hello, world!") }.
to_json
Aston.parse_hash(JSON.parse(json))
#⇒ #,
# @content=
# #,
# @content=
# #"value"}>,
# @content=#,
# @name="baz">]>,
# @name="bar">]>,
# @name="root">
```### `#update_in`
```ruby
a = Aston.new :aston, attributes: { foo: :bar }
a.put_attribute %i[bar baz], :ok, 42
a.put_content %i[bar baz], 'Hello'
a.update_in %i[bar baz] do |content|
content <<
Aston.new(:seq, attributes: { name: :seq1 }) <<
Aston.new(:seq, attributes: { name: :seq2 }) <<
Aston.new(:seq, attributes: { name: :seq3 })
end#⇒ [#42}>,
# @content=
# #:seq1}>,
# @content=#,
# @name=:seq>,
# #:seq2}>,
# @content=#,
# @name=:seq>,
# #:seq3}>,
# @content=#,
# @name=:seq>]>,
# @name=:baz>]
````#update_in` returns an array of updated elements on the path given. Unlike `#update_in`.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Aston project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/am-kantox/aston/blob/master/CODE_OF_CONDUCT.md).