Git Product home page Git Product logo

tox's Introduction

Tox

Tox is a high performance XML parser and renderer for Ruby based on Ox. It's best explained using an example:

require 'tox'

xml = %{
  <profile id="20">
    <name>Mike Ross</name>

    <friend title="Name Partner">
      <name>Harvey Specter</name>
      <tags>
        <tag>The Best</tag>
        <tag>Closer</tag>
      </tags>
    </friend>

    <friend title="Paralegal">
      <name>Rachel Zane</name>
      <age>30</age>
    </friend>
  </profile>
}

template = Tox.dsl do
  el(:profile, {
    id: at(:id),
    name: el(:name, text),
    friends: mel(:friend, {
      name: el(:name, text),
      age: el(:age, text),
      title: at(:title),
      tags: el(:tags, mel(:tag, text))
    })
  })
end

v = template.parse(xml)
# {
#   id: "20",
#   name: "Mike Ross",
#   friends: [
#     {
#       title: "Name Partner",
#       name: "Harvey Specter",
#       tags: ["The Best", "Closer"]
#     },
#     {
#       title: "Paralegal",
#       name: "Rachel Zane",
#       age: "30"
#     }
#   ]
# }

template.render(v)
# Outputs input xml

The above template is actually translated to something like:

el(:profile, compose({
  id: at(:id),
  name: el(:name, text),
  friends: collect(el(:friend, compose({
    name: el(:name, text),
    age: el(:age, text),
    title: at(:title),
    tags: el(:tags, collect(el(:tag, text)))
  })))
}))

As you can see, it's a mixture of nodes matching xml elements, and nodes transforming the output structure. If you strip out the transformations, the template should match a subset of the xml. Each XML element can only be specified once in the template. Transformations can hold custom behavior for distributing to and collecting from it's subtree(s). Another example:

template = Tox.dsl do
  el(:names, merge(
    const('array', at(:type)),
    compose(names: collect(el(:name, text)))
  ))
end

template.render(names: ['Mike', 'Harvey'])
# <names type="array">
#   <name>Mike</name>
#   <name>Harvey</name>
# </names>

Read Tox Tests for more examples.

Installation

Add this line to your application's Gemfile:

gem 'tox'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tox

Development

Run tests using rake:

rake

Performance tests can be enabled using:

PERFORMANCE=true rake

License

MIT

tox's People

Contributors

challengee avatar choosen avatar devwout avatar wardva avatar

Stargazers

 avatar  avatar

Watchers

 avatar Piotr Boniecki avatar  avatar Peter De Berdt avatar James Cloos avatar Owen Shepherd avatar Frederik avatar Kyle Alpert avatar Beerend Lauwers avatar Francisco Javier Carretero Garcia avatar  avatar Gert-Jan Bottu avatar Sem avatar Nathalie Oostvogels avatar Scott Williams avatar Konstantin avatar Łukasz Maślej avatar Yevhenii Loskutov avatar Joseph Tseng avatar Iwan avatar Jesse Bartola avatar Jan Matuszewski avatar  avatar  avatar

Forkers

bonias

tox's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.