Skip to content

Commit

Permalink
Add optional plist serialization format to Rabl
Browse files Browse the repository at this point in the history
Details
* Fixes #90 (nesquena/rabl issues tracker)
* Plist: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.htmlrubgems
* Defaults to using the Ruby plist gem: http://rubygems.org/gems/plist
* Adds Rabl configuration option include_plist_root which has same semantics as include_json_root.
* Adds Rabl configuration option plist_engine which has similar semantics as json_engine.
* Adds tests
* Updated README.md to reflect the plist changes.
  • Loading branch information
alzeih committed Feb 15, 2012
1 parent a6dcc13 commit 68518de
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ Rabl.configure do |config|
# config.json_engine = nil # Any multi\_json engines
# config.msgpack_engine = nil # Defaults to ::MessagePack
# config.bson_engine = nil # Defaults to ::BSON
# config.plist_engine = nil # Defaults to ::Plist::Emit
# config.include_json_root = true
# config.include_msgpack_root = true
# config.include_bson_root = true
# config.include_plist_root = true
# config.include_xml_root = false
# config.enable_json_callbacks = false
# config.xml_options = { :dasherize => true, :skip_types => false }
Expand Down Expand Up @@ -191,6 +193,32 @@ end
*NOTE*: Attempting to render the bson format without either including the bson gem or
setting a `bson_engine` will cause an exception to be raised.

### Plist ###

Rabl also includes optional support for [Plist](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html]) serialization format using the [plist gem](http://plist.rubyforge.org/).
To enable, include the plist gem in your project's Gemfile. Then use Rabl as normal with the `plist` format (akin to other formats).

```ruby
# Gemfile
gem 'plist'
```

There is also an option for a custom Plist implementation by setting the Rabl `plist_engine` configuration attribute.

```ruby
class CustomEncodeEngine
def self.dump string
# Custom Encoding by your own engine.
end
end

Rabl.configure do |config|
config.plist_engine = CustomEncodeEngine
end
```

*NOTE*: Attempting to render the plist format without either including the plist gem or setting a `plist_engine` will cause an exception to be raised.

## Usage ##

### Object Assignment ###
Expand Down Expand Up @@ -482,6 +510,7 @@ Thanks to [Miso](http://gomiso.com) for allowing me to create this for our appli
* [Matthew Schulkind](https://github.com/mschulkind) - Cleanup of configuration and tests
* [Luke van der Hoeven](https://github.com/plukevdh) - Support non-ORM objects in templates
* [Andrey Voronkov](https://github.com/Antiarchitect) - Added BSON format support
* [Alli Witheford](https://github.com/alzeih) - Added Plist format support

and many more contributors listed in the [CHANGELOG](https://github.com/nesquena/rabl/blob/master/CHANGELOG.md).

Expand Down
15 changes: 15 additions & 0 deletions lib/rabl/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
rescue LoadError
end

# We load the plist library if it is available.
begin
require 'plist'
rescue LoadError
end

# Load MultiJSON
require 'multi_json'

Expand All @@ -18,13 +24,15 @@ module Rabl
class Configuration
attr_accessor :include_json_root
attr_accessor :include_msgpack_root
attr_accessor :include_plist_root
attr_accessor :include_xml_root
attr_accessor :include_bson_root
attr_accessor :enable_json_callbacks
attr_accessor :bson_check_keys
attr_accessor :bson_move_id
attr_writer :msgpack_engine
attr_writer :bson_engine
attr_writer :plist_engine
attr_writer :xml_options
attr_accessor :cache_sources

Expand All @@ -33,6 +41,7 @@ class Configuration
def initialize
@include_json_root = true
@include_msgpack_root = true
@include_plist_root = true
@include_xml_root = false
@include_bson_root = true
@enable_json_callbacks = false
Expand All @@ -41,6 +50,7 @@ def initialize
@json_engine = nil
@msgpack_engine = nil
@bson_engine = nil
@plist_engine = nil
@xml_options = {}
@cache_sources = false
end
Expand All @@ -67,6 +77,11 @@ def msgpack_engine
# @return the Bson encoder/engine to use.
def bson_engine
@bson_engine || ::BSON

##
# @return the Plist encoder/engine to use.
def plist_engine
@plist_engine || ::Plist::Emit
end

# Allows config options to be read like a hash
Expand Down
9 changes: 9 additions & 0 deletions lib/rabl/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ def to_msgpack(options={})
end
alias_method :to_mpac, :to_msgpack

# Returns a plist representation of the data object
# to_plist(:root => true)
def to_plist(options={})
include_root = Rabl.configuration.include_plist_root
options = options.reverse_merge(:root => include_root, :child_root => include_root)
result = defined?(@_collection_name) ? { @_collection_name => to_hash(options) } : to_hash(options)
Rabl.configuration.plist_engine.dump(result)
end

# Returns an xml representation of the data object
# to_xml(:root => true)
def to_xml(options={})
Expand Down
1 change: 1 addition & 0 deletions rabl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'yajl-ruby'
s.add_development_dependency 'msgpack', '~> 0.4.5'
s.add_development_dependency 'bson', '~> 1.5.2'
s.add_development_dependency 'plist'
end
Loading

0 comments on commit 68518de

Please sign in to comment.