Code Monkey home page Code Monkey logo

mapricot's Introduction

Mapricot

XML to object mapper with an interface similar to ActiveRecord associations.

Install

sudo gem install mapricot

Example Usage

Super Simple

require 'mapricot'

simple_xml = %(
  <user>
    <id>1</name>
    <name>Bob</name>
    <pet>cat</pet>
    <pet>dog</pet>
  </user>
)

class User < Mapricot::Base
  has_one   :id,    :integer
  has_one   :name,  :string
  has_many  :pets,  :string
end

user = User.new(simple_xml)
puts user.id                # => 1
puts user.name              # => "Bob"
puts user.pets              # => ["cat", "dog"]

A little more realistic

require 'mapricot'

xml = %(
  <user>
    <id>2</name>
    <name>Sally</name>
    <location code="ny123">
      <city>New York</city>
      <state>NY</state>
    </location>
    <hobby>
      <description>Skiing</description>
      <number_of_years>2</number_of_years>
    </hobby>
    <hobby>
      <description>Hiking</description>
      <number_of_years>3</number_of_years>
    </hobby>
  </user>
)

class User < Mapricot::Base
  has_one   :id,        :integer
  has_one   :name       # Tag type will default to :string
  has_many  :pets
  has_one   :location,  :xml
  has_many  :hobbies,   :xml
end

class Location < Mapricot::Base
  has_attribute :code
  has_one       :city
  has_one       :state
end

class Hobby < Mapricot::Base
  has_one :description,     :string
  has_one :number_of_years, :integer
end

user = User.new(xml)
puts user.name            # => "Sally"
puts user.pets.inspect    # => []
puts user.location.class  # => Location
puts user.location.city   # => "New York"
puts user.location.state  # => "NY"
puts user.location.code   # => "ny123"
puts user.hobbies.class         # => Array
puts user.hobbies.first.class   # => Hobby

user.hobbies.each do |hobby|
  puts "#{hobby.description} for #{hobby.number_of_years} years"
end
# => Skiing for 2 years
# => Hiking for 3 years

Parsing from a URL

If you want to parse xml from a url, do something like this:

require 'open-uri'

Foo < Mapricot::Base
...
end

url = 'http://www.example.com'
Foo.new(open(url))

Changing the xml library

Mapricot.parser = :hpricot
Mapricot.parser = :nokogiri
Mapricot.parser = :libxml

Other stuff

Your classes should inherit from Mapricot::Base, which provides the class methods

  • has_one(name, type, opts = {})

  • has_many(name, type, opts = {})

  • has_attribute(name)

Copyright © 2010 Lou Zell, released under the MIT license

mapricot's People

Contributors

lzell avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

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.