Compare commits

...

16 Commits

Author SHA1 Message Date
John Bintz cb53352607 ensure that child usage does not manipulate cached template 2013-01-22 14:16:44 -05:00
ccocchi 297f8a9145 Update CHANGELOG 2013-01-19 16:49:44 +01:00
ccocchi 8c35a24408 Add full template stack support. Closes #20 2013-01-19 16:48:56 +01:00
Christopher Cocchi-Perrier 0761830988 Merge pull request #19 from lloydmeta/feature/support_symbol_format
Add support for format as symbol
2013-01-17 07:00:43 -08:00
Lloyd 2d05060311 Add support for format as symbol (e.g. when specified in routes.rb or inside the controller 2013-01-16 12:21:00 +09:00
ccocchi b1ae9a4c58 Bump to 0.3.0 2012-11-14 15:52:17 +01:00
ccocchi 84fa2dd282 Update Changelog 2012-11-14 15:51:08 +01:00
ccocchi fde46000a4 Update README [ci skip] 2012-11-14 15:50:06 +01:00
ccocchi 3ca937246b Use libxml for rbx 2012-11-14 15:19:13 +01:00
ccocchi ff22261ac0 oj is cruby specific 2012-11-14 15:16:22 +01:00
ccocchi 0e18a4141e Use nokogiri on JRuby 2012-11-14 15:13:59 +01:00
ccocchi 8b1e88d4ff Add .travis.yml 2012-11-14 12:45:14 +01:00
ccocchi 2c3f835895 Update CHANGELOG 2012-11-14 12:41:06 +01:00
ccocchi ded9e66973 Add test for keywords used as variable names 2012-11-14 12:40:11 +01:00
Christopher Cocchi-Perrier 0039cc7369 Merge pull request #11 from ccocchi/plist-renderer
Add PList renderer
2012-11-14 03:22:41 -08:00
ccocchi fa6239a564 Remove location from response by default 2012-11-12 17:16:51 +01:00
16 changed files with 122 additions and 24 deletions

5
.travis.yml Normal file
View File

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

View File

@ -1,5 +1,16 @@
# 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,10 +2,17 @@ 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 target **Rails 3+ application**.
rabl-rails only targets **Rails 3+ application** and is compatible with mri 1.9.3, jRuby and rubinius.
## 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.6.14 and rabl-rails 0.1.0
Benchmarks have been made using this [application](http://github.com/ccocchi/rabl-benchmark), with rabl 0.7.6 and rabl-rails 0.3.0
Overall, Rabl-rails is **20% faster and use 10% less memory**, even **twice faster** when rendering collections with extends.
Overall, Rabl-rails is **20% faster and use 10% less memory**, even **twice faster** when using extends.
You can see full tests on test application repository.
@ -280,11 +280,12 @@ 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
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.
## Original idea
* [RABL](http://github.com/nesquena/rabl) Standart RABL gem. I used it a lot before deciding I wanted faster views
* [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.
## Copyright

View File

@ -76,7 +76,12 @@ 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.upcase!).new(context, locals).render(compiled_template)
Renderers.const_get(format.to_s.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,9 +70,7 @@ module RablRails
end
if key.to_s.start_with?('_') # glue
current_value.each_pair { |k, v|
output[k] = object.send(v)
}
output.merge!(render_resource(object, current_value))
next output
else # child
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
@ -131,4 +129,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, location: api_location)
controller.default_render options.merge(status: :created)
else
head :no_content
end

View File

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

View File

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

View File

@ -113,6 +113,7 @@ 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
@ -132,6 +133,18 @@ 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
@ -176,4 +189,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

47
test/keyword_test.rb Normal file
View File

@ -0,0 +1,47 @@
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,6 +61,11 @@ 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,7 +1,4 @@
require 'test_helper'
require 'plist'
RablRails.plist_engine = Plist::Emit
class TestPlistRenderer < ActiveSupport::TestCase
INDENT_REGEXP = /\n(\s)*/

View File

@ -22,6 +22,15 @@ 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