Compare commits

..

No commits in common. "master" and "plist-renderer" have entirely different histories.

16 changed files with 24 additions and 122 deletions

View File

@ -1,5 +0,0 @@
language: ruby
rvm:
- 1.9.3
- jruby-19mode
- rbx-19mode

View File

@ -1,16 +1,5 @@
# CHANGELOG
## 0.3.1
* Add full template stack support to `glue` (fnordfish)
* Allow format to be a symbol (lloydmeta)
## 0.3.0
* Travis integration
* Add test for keywords used as variable names
* Add PList renderer
* Remove location header from post responses in responder
* Fix bug with incomplete template prefixing
## 0.2.2
* Add condition blocks

11
Gemfile
View File

@ -2,17 +2,10 @@ source "http://rubygems.org"
gemspec
gem 'oj'
gem 'libxml-ruby'
gem 'plist'
platforms :ruby do
gem 'oj'
gem 'libxml-ruby'
end
platforms :jruby do
gem 'nokogiri'
end
group :test do
gem 'rspec-mocks'
end

View File

@ -4,7 +4,7 @@ RABL (Ruby API Builder Language) is a ruby templating system for rendering resou
rabl-rails is **faster** and uses **less memory** than the standard rabl gem while letting you access the same features. There are some slight changes to do on your templates to get this gem to work but it should't take you more than 5 minutes.
rabl-rails only targets **Rails 3+ application** and is compatible with mri 1.9.3, jRuby and rubinius.
rabl-rails only target **Rails 3+ application**.
## Installation
@ -269,9 +269,9 @@ You can find more informations about other features (caching, custom_responder,
## Performance
Benchmarks have been made using this [application](http://github.com/ccocchi/rabl-benchmark), with rabl 0.7.6 and rabl-rails 0.3.0
Benchmarks have been made using this [application](http://github.com/ccocchi/rabl-benchmark), with rabl 0.6.14 and rabl-rails 0.1.0
Overall, Rabl-rails is **20% faster and use 10% less memory**, even **twice faster** when using extends.
Overall, Rabl-rails is **20% faster and use 10% less memory**, even **twice faster** when rendering collections with extends.
You can see full tests on test application repository.
@ -280,12 +280,11 @@ You can see full tests on test application repository.
* [Christopher Cocchi-Perrier](http://github.com/ccocchi) - Creator of the project
Want to add another format to Rabl-rails ? Checkout [JSON renderer](http://github.com/ccocchi/rabl-rails/blob/master/lib/rabl-rails/renderers/json.rb) for reference
Want to make another change ? Just fork and contribute, any help is very much appreciated. If you found a bug, you can report it via the Github issues.
Want to make another change ? Just fork and contribute, any help is very much appreciated
## Original idea
* [RABL](http://github.com/nesquena/rabl) Standart RABL gem. I used it a lot but I needed to improve my API response time, and
since most of the time was spent in view rendering, I decided to implement a faster rabl gem.
* [RABL](http://github.com/nesquena/rabl) Standart RABL gem. I used it a lot before deciding I wanted faster views
## Copyright

View File

@ -76,12 +76,7 @@ module RablRails
def self.load_default_engines!
self.json_engine = MultiJson.default_engine
self.xml_engine = 'LibXML' if defined?(LibXML)
self.plist_engine = Plist::Emit if defined?(Plist)
if defined?(LibXML)
self.xml_engine = 'LibXML'
elsif defined?(Nokogiri)
self.xml_engine = 'Nokogiri'
end
end
end

View File

@ -67,7 +67,7 @@ module RablRails
name = options[:root] if options.has_key? :root
if options[:partial]
template = Library.instance.compile_template_from_path(options[:partial])
@template[name] = template.merge(:_data => data)
@template[name] = template.merge!(:_data => data)
elsif block_given?
@template[name] = sub_compile(data) { yield }
end
@ -161,4 +161,4 @@ module RablRails
@template = old_template
end
end
end
end

View File

@ -15,7 +15,7 @@ module RablRails
compiled_template = compile_template_from_source(source, path)
format = context.params[:format] || 'json'
Renderers.const_get(format.to_s.upcase!).new(context, locals).render(compiled_template)
Renderers.const_get(format.upcase!).new(context, locals).render(compiled_template)
end
def compile_template_from_source(source, path = nil)
@ -33,4 +33,4 @@ module RablRails
compile_template_from_source(t.source, path)
end
end
end
end

View File

@ -70,7 +70,9 @@ module RablRails
end
if key.to_s.start_with?('_') # glue
output.merge!(render_resource(object, current_value))
current_value.each_pair { |k, v|
output[k] = object.send(v)
}
next output
else # child
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
@ -129,4 +131,4 @@ module RablRails
end
end
end
end
end

View File

@ -36,7 +36,7 @@ module RablRails
options[:prefixes] = controller._prefixes
options[:template] ||= template
controller.default_render options.merge(status: :created)
controller.default_render options.merge(status: :created, location: api_location)
else
head :no_content
end

View File

@ -2,10 +2,10 @@ module RablRails
class CompiledTemplate
attr_accessor :source, :data, :root_name
delegate :[], :[]=, :merge!, :merge, :to => :source
delegate :[], :[]=, :merge!, :to => :source
def initialize
@source = {}
end
end
end
end

View File

@ -1,3 +1,3 @@
module RablRails
VERSION = '0.3.0'
VERSION = '0.2.2'
end

View File

@ -113,7 +113,6 @@ class CompilerTest < ActiveSupport::TestCase
t = @compiler.compile_source(%{child(:user, :partial => 'users/base') })
assert_equal( {:user => { :_data => :user, :id => :id } }, t.source)
assert_equal( {:id => :id}, mock_template.source)
end
test "glue is compiled as a child but with anonymous name" do
@ -133,18 +132,6 @@ class CompilerTest < ActiveSupport::TestCase
}, t.source)
end
test "glue accepts all dsl in its body" do
t = @compiler.compile_source(%{
glue :@user do node(:foo) { |u| u.name } end
})
assert_not_nil(t.source[:_glue0])
s = t.source[:_glue0]
assert_equal(:@user, s[:_data])
assert_instance_of(Proc, s[:foo])
end
test "extends use other template source as itself" do
template = mock('template', :source => { :id => :id })
RablRails::Library.reset_instance
@ -189,4 +176,4 @@ class CompilerTest < ActiveSupport::TestCase
assert_equal [:users, :users], @compiler.send(:extract_data_and_name, :users)
assert_equal [:@users, :authors], @compiler.send(:extract_data_and_name, :@users => :authors)
end
end
end

View File

@ -1,47 +0,0 @@
require 'test_helper'
class KeywordTest < ActiveSupport::TestCase
class Collection
attr_accessor :id, :name
def initialize(id, name)
@id = id
@name = name
end
def cover(size)
"foo_#{size}"
end
end
setup do
RablRails::Library.reset_instance
@context = Context.new
@user = User.new(1, 'Marty')
@collections = [Collection.new(1, 'first'), Collection.new(2, 'last')]
@context.assigns['user'] = @user
@context.assigns['collections'] = @collections
@context.virtual_path = 'user/show'
@context.stub(lookup_context: nil)
end
test "collections model should render correctly" do
source = %{
object :@user
child(:@collections => :collections) do
attributes :id, :name
node(:cover_url) { |c|
c.cover(:medium)
}
end
}
assert_equal(MultiJson.encode(
user: { collections: [{
id: 1, name: 'first', cover_url: "foo_medium"
}, {
id: 2, name: 'last', cover_url: "foo_medium"
}] }
), RablRails::Library.instance.get_rendered_template(source, @context))
end
end

View File

@ -61,11 +61,6 @@ class TestJsonRenderer < ActiveSupport::TestCase
assert_equal %q({"name":"foobar"}), render_json_output
end
test "render glued node" do
@template.source = { :_glue0 => { :_data => :@data, :foo => lambda { |u| u.name } } }
assert_equal(%q({"foo":"foobar"}), render_json_output)
end
test "render collection with attributes" do
@data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')]
@context.assigns['data'] = @data

View File

@ -1,4 +1,7 @@
require 'test_helper'
require 'plist'
RablRails.plist_engine = Plist::Emit
class TestPlistRenderer < ActiveSupport::TestCase
INDENT_REGEXP = /\n(\s)*/

View File

@ -22,15 +22,6 @@ class <<Singleton
end
require 'rabl-rails'
require 'plist'
if RUBY_ENGINE == 'jruby'
require 'nokogiri'
else
require 'libxml'
end
RablRails.load_default_engines!
module ActiveSupport
class TestCase