Compare commits
16 Commits
plist-rend
...
master
Author | SHA1 | Date |
---|---|---|
John Bintz | cb53352607 | |
ccocchi | 297f8a9145 | |
ccocchi | 8c35a24408 | |
Christopher Cocchi-Perrier | 0761830988 | |
Lloyd | 2d05060311 | |
ccocchi | b1ae9a4c58 | |
ccocchi | 84fa2dd282 | |
ccocchi | fde46000a4 | |
ccocchi | 3ca937246b | |
ccocchi | ff22261ac0 | |
ccocchi | 0e18a4141e | |
ccocchi | 8b1e88d4ff | |
ccocchi | 2c3f835895 | |
ccocchi | ded9e66973 | |
Christopher Cocchi-Perrier | 0039cc7369 | |
ccocchi | fa6239a564 |
|
@ -0,0 +1,5 @@
|
|||
language: ruby
|
||||
rvm:
|
||||
- 1.9.3
|
||||
- jruby-19mode
|
||||
- rbx-19mode
|
11
CHANGELOG.md
11
CHANGELOG.md
|
@ -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
11
Gemfile
|
@ -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
|
||||
|
|
11
README.md
11
README.md
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module RablRails
|
||||
VERSION = '0.2.2'
|
||||
VERSION = '0.3.0'
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
require 'test_helper'
|
||||
require 'plist'
|
||||
|
||||
RablRails.plist_engine = Plist::Emit
|
||||
|
||||
class TestPlistRenderer < ActiveSupport::TestCase
|
||||
INDENT_REGEXP = /\n(\s)*/
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue