github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

datamapper / dm-more

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 194
    • 62
  • Source
  • Commits
  • Network (62)
  • Wiki (2)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (3)
    • errors_messages_i18n
    • master ✓
    • next
  • Tags (17)
    • v0.10.2
    • 0.10.1
    • 0.10.0-RC2
    • 0.10.0-RC1
    • 0.10.0
    • 0.9.11
    • 0.9.10
    • 0.9.9
    • 0.9.8
    • 0.9.7
    • 0.9.6
    • 0.9.5
    • 0.9.4
    • 0.9.3
    • 0.9.2
    • 0.9.1
    • 0.9.0.1
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper — Read more

  cancel

http://datamapper.org/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

[dm-validations] Defining Error class used 
namelessjon (author)
Fri Feb 05 05:59:59 -0800 2010
commit  6972de18ea7448508f5b6e90ae88aa865ed1594f
tree    d1c96834ac3ec951bb8c53acbf9f935f33a3fc07
parent  17c0dbe0dfd4f7d8c0d63c349b5f932b62ddd5a7
dm-more / dm-serializer
name age
history
message
..
file LICENSE Mon Nov 23 23:30:02 -0800 2009 [dm-more] Converted to use Jeweler * Removed o... [Dan Kubb]
file README.rdoc Tue Aug 18 17:37:06 -0700 2009 [all] Changed README to .rdoc format [#970 sta... [snusnu]
file Rakefile Wed Jan 27 12:03:06 -0800 2010 [all] Bumped rspec development dependency to '~... [snusnu]
file VERSION Tue Dec 29 16:52:33 -0800 2009 [dm-more] Bumped version to 0.10.3 [Dan Kubb]
directory autotest/ Thu May 29 00:51:51 -0700 2008 Updated to work with latest DataMapper::Collection [Dan Kubb]
directory benchmarks/ Tue Dec 29 16:52:33 -0800 2009 [dm-more] Bumped version to 0.10.3 [Dan Kubb]
file dm-serializer.gemspec Loading commit data...
directory lib/ Fri Dec 11 16:29:12 -0800 2009 [dm-more] Updated runtime and development gem d... [Dan Kubb]
directory spec/ Fri Dec 11 16:29:12 -0800 2009 [dm-more] Updated runtime and development gem d... [Dan Kubb]
directory tasks/ Fri Dec 11 19:51:42 -0800 2009 Updated rake spec task to be consistent with dm... [Dan Kubb]
dm-serializer/README.rdoc

dm-serializer

Overview

dm-serializer allows DataMapper models and collections to be serialized to a variety of formats (currently JSON, XML, YAML and CSV).

How it works

One method is added to each model/collection for each serialization type - to_json, to_xml, to_yaml, and to_csv. With the exception of to_csv, all of these methods share the same interface. to_json will be used for examples. Any method specific behaviour is documented in its own section below.

  require 'dm-serializer'

  class Cow
    include DataMapper::Resource

    property :id,   Integer, :key => true
    property :name, String

    def description
      "A Cow"
    end
  end

  cow = Cow.create(
    :id    => 1,
    :name  => "Berta"
  )

  cow.to_json                      # => { "id": 1, "name": "Berta" }
  cow.to_json(:only    => [:name]) # => { "name": "Berta" }
  cow.to_json(:exclude => [:id])   # => { "name": "Berta" }
  cow.to_json(:methods => [:desc]) # => { "id": 1, "name": "Berta", "desc": "A Cow" }

You can include associations by passing the association accessor the :methods option.

If you want to only load a particular serialization method, that’s cool, you can do that:

  require 'dm-serializer/to_json'

to_xml

to_xml supports some extra options to allow you to override the element names

  cow.to_xml(:element_name => 'bovine') # => <bovine><id>1</id><name>Berta</name></bovine>
  cows.to_xml(:collection_element_name => 'kine')   # => <kine><bovine><id>1</id><name>Berta</name></bovine></kine>

If you would like a nice speed boost (~5x), require libxml or nokogiri before dm-serializer, and that library will be used rather than REXML.

to_csv

to_csv currently doesn’t support any options yet. It will in the future. It will not support serializing child associations.

Arrays, Hashes, and other core classes

dm-serializer only adds serialization methods to DataMapper objects and collections, however some libraries used (json, yaml) add methods to core classes, such as Array. Note that passing dm-serializer options (such as :only) to these methods is *not supported*.

  Cow.all.to_a.to_yaml(:only => 'name') # WILL NOT WORK

Beware

If you go spelunking through the code you will find other undocumented options. Use at your own risk, I plan on removing or changing these in the near future.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server