Add Plist renderer and some tests
This commit is contained in:
parent
286f08eb83
commit
543543b5cf
1
Gemfile
1
Gemfile
@ -4,6 +4,7 @@ gemspec
|
|||||||
|
|
||||||
gem 'oj'
|
gem 'oj'
|
||||||
gem 'libxml-ruby'
|
gem 'libxml-ruby'
|
||||||
|
gem 'plist'
|
||||||
|
|
||||||
group :test do
|
group :test do
|
||||||
gem 'rspec-mocks'
|
gem 'rspec-mocks'
|
||||||
|
@ -33,6 +33,12 @@ module RablRails
|
|||||||
mattr_accessor :responder_default_template
|
mattr_accessor :responder_default_template
|
||||||
@@responder_default_template = 'show'
|
@@responder_default_template = 'show'
|
||||||
|
|
||||||
|
mattr_accessor :plist_engine
|
||||||
|
@@plist_engine = nil
|
||||||
|
|
||||||
|
mattr_accessor :include_plist_root
|
||||||
|
@@include_plist_root = nil
|
||||||
|
|
||||||
def self.configure
|
def self.configure
|
||||||
yield self
|
yield self
|
||||||
|
|
||||||
@ -64,7 +70,8 @@ module RablRails
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.load_default_engines!
|
def self.load_default_engines!
|
||||||
self.json_engine = MultiJson.default_engine
|
self.json_engine = MultiJson.default_engine
|
||||||
self.xml_engine = 'LibXML' if defined?(LibXML)
|
self.xml_engine = 'LibXML' if defined?(LibXML)
|
||||||
|
self.plist_engine = Plist::Emit if defined?(Plist)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
require 'rabl-rails/renderers/base'
|
require 'rabl-rails/renderers/base'
|
||||||
require 'rabl-rails/renderers/json'
|
require 'rabl-rails/renderers/json'
|
||||||
require 'rabl-rails/renderers/xml'
|
require 'rabl-rails/renderers/xml'
|
||||||
|
require 'rabl-rails/renderers/plist'
|
||||||
|
|
||||||
module RablRails
|
module RablRails
|
||||||
module Renderer
|
module Renderer
|
||||||
|
11
lib/rabl-rails/renderers/plist.rb
Normal file
11
lib/rabl-rails/renderers/plist.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
module RablRails
|
||||||
|
module Renderers
|
||||||
|
class PLIST < Base
|
||||||
|
|
||||||
|
def format_output(hash)
|
||||||
|
hash = { _options[:root_name] => hash } if _options[:root_name] && RablRails.include_plist_root
|
||||||
|
RablRails.plist_engine.dump(hash)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
53
test/renderers/plist_renderer_test.rb
Normal file
53
test/renderers/plist_renderer_test.rb
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class TestPlistRenderer < ActiveSupport::TestCase
|
||||||
|
INDENT_REGEXP = /\n(\s)*/
|
||||||
|
HEADER_REGEXP = /<\?[^>]+><![^>]+>/
|
||||||
|
|
||||||
|
setup do
|
||||||
|
@data = User.new(1, 'foobar', 'male')
|
||||||
|
|
||||||
|
@context = Context.new
|
||||||
|
@context.assigns['data'] = @data
|
||||||
|
|
||||||
|
@template = RablRails::CompiledTemplate.new
|
||||||
|
@template.data = :@data
|
||||||
|
@template.root_name = :user
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_plist_output
|
||||||
|
output = RablRails::Renderers::PLIST.new(@context).render(@template).to_s.gsub!(INDENT_REGEXP, '')
|
||||||
|
output.sub!(HEADER_REGEXP, '').gsub!(%r(</?plist[^>]*>), '').sub!(%r(<dict/?>), '').sub(%r(</dict>), '')
|
||||||
|
end
|
||||||
|
|
||||||
|
test "render object wth empty template" do
|
||||||
|
@template.source = {}
|
||||||
|
assert_equal %q(), render_plist_output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "render collection with empty template" do
|
||||||
|
@context.assigns['data'] = [@data]
|
||||||
|
@template.source = {}
|
||||||
|
assert_equal %q(<array></array>), render_plist_output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "render object with local methods (used by decent_exposure)" do
|
||||||
|
@context.stub(:user).and_return(@data)
|
||||||
|
@template.data = :user
|
||||||
|
@template.source = { :id => :id }
|
||||||
|
assert_equal %q(<key>id</key><integer>1</integer>), render_plist_output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "render single object attributes" do
|
||||||
|
@template.source = { :id => :id, :name => :name }
|
||||||
|
assert_equal %q(<key>id</key><integer>1</integer><key>name</key><string>foobar</string>), render_plist_output
|
||||||
|
end
|
||||||
|
|
||||||
|
test "render child with object association" do
|
||||||
|
@data.stub(:address).and_return(mock(:city => 'Paris'))
|
||||||
|
@template.source = { :address => { :_data => :address, :city => :city } }
|
||||||
|
|
||||||
|
|
||||||
|
assert_equal %q(<key>address</key><dict><key>city</key><string>Paris</string></dict>), render_plist_output
|
||||||
|
end
|
||||||
|
end
|
@ -22,6 +22,9 @@ class <<Singleton
|
|||||||
end
|
end
|
||||||
|
|
||||||
require 'rabl-rails'
|
require 'rabl-rails'
|
||||||
|
require 'plist'
|
||||||
|
|
||||||
|
RablRails.plist_engine = Plist::Emit
|
||||||
|
|
||||||
module ActiveSupport
|
module ActiveSupport
|
||||||
class TestCase
|
class TestCase
|
||||||
|
Loading…
Reference in New Issue
Block a user