An open API service indexing awesome lists of open source software.

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

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).