Skip to content

take-five/ox-mapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ox::Mapper

Ox::Mapper's intention is to simplify creation of parsers based on ox

Installation

Add this line to your application's Gemfile:

gem 'ox-mapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ox-mapper

Usage

All you need to do is to setup callbacks for elements and attributes in Ruby style

mapper = Ox::Mapper.new

mapper.on(:book) { |book| puts book.attributes.inspect }
mapper.on(:title) { |title| title.parent[:title] = title.text }

# collected attributes should be set up explicitely
mapper.on(:author, :attributes => :name) { |e| e.parent[:author] = e[:name] }

# setup transformation for attribute "value" of "price" element
mapper.on(:price, :attributes => :value) { |e| e.parent[:price] = Float(e[:value]) }

mapper.parse(StringIO.new <<-XML) # => {:title => "Serenity", :author => "John Dow", :price => 1123.0}
  <xml>
    <book>
      <title>Serenity</title>
      <author name="John Dow" age="99" />
      <price value="1123" />
    </book>
  </xml>
XML

This API is unstable and a subject to change.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

simplify creation of parsers based on `ox`

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages