Compare commits
No commits in common. "master" and "plist-renderer" have entirely different histories.
master
...
plist-rend
@ -1,5 +0,0 @@
|
|||||||
language: ruby
|
|
||||||
rvm:
|
|
||||||
- 1.9.3
|
|
||||||
- jruby-19mode
|
|
||||||
- rbx-19mode
|
|
11
CHANGELOG.md
11
CHANGELOG.md
@ -1,16 +1,5 @@
|
|||||||
# CHANGELOG
|
# 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
|
## 0.2.2
|
||||||
* Add condition blocks
|
* Add condition blocks
|
||||||
|
|
||||||
|
11
Gemfile
11
Gemfile
@ -2,17 +2,10 @@ source "http://rubygems.org"
|
|||||||
|
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
|
gem 'oj'
|
||||||
|
gem 'libxml-ruby'
|
||||||
gem 'plist'
|
gem 'plist'
|
||||||
|
|
||||||
platforms :ruby do
|
|
||||||
gem 'oj'
|
|
||||||
gem 'libxml-ruby'
|
|
||||||
end
|
|
||||||
|
|
||||||
platforms :jruby do
|
|
||||||
gem 'nokogiri'
|
|
||||||
end
|
|
||||||
|
|
||||||
group :test do
|
group :test do
|
||||||
gem 'rspec-mocks'
|
gem 'rspec-mocks'
|
||||||
end
|
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 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
|
## Installation
|
||||||
|
|
||||||
@ -269,9 +269,9 @@ You can find more informations about other features (caching, custom_responder,
|
|||||||
|
|
||||||
## Performance
|
## 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.
|
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
|
* [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 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
|
## 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
|
* [RABL](http://github.com/nesquena/rabl) Standart RABL gem. I used it a lot before deciding I wanted faster views
|
||||||
since most of the time was spent in view rendering, I decided to implement a faster rabl gem.
|
|
||||||
|
|
||||||
## Copyright
|
## Copyright
|
||||||
|
|
||||||
|
@ -76,12 +76,7 @@ module RablRails
|
|||||||
|
|
||||||
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.plist_engine = Plist::Emit if defined?(Plist)
|
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
|
||||||
end
|
end
|
||||||
|
@ -67,7 +67,7 @@ module RablRails
|
|||||||
name = options[:root] if options.has_key? :root
|
name = options[:root] if options.has_key? :root
|
||||||
if options[:partial]
|
if options[:partial]
|
||||||
template = Library.instance.compile_template_from_path(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?
|
elsif block_given?
|
||||||
@template[name] = sub_compile(data) { yield }
|
@template[name] = sub_compile(data) { yield }
|
||||||
end
|
end
|
||||||
|
@ -15,7 +15,7 @@ module RablRails
|
|||||||
compiled_template = compile_template_from_source(source, path)
|
compiled_template = compile_template_from_source(source, path)
|
||||||
|
|
||||||
format = context.params[:format] || 'json'
|
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
|
end
|
||||||
|
|
||||||
def compile_template_from_source(source, path = nil)
|
def compile_template_from_source(source, path = nil)
|
||||||
|
@ -70,7 +70,9 @@ module RablRails
|
|||||||
end
|
end
|
||||||
|
|
||||||
if key.to_s.start_with?('_') # glue
|
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
|
next output
|
||||||
else # child
|
else # child
|
||||||
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
|
object.respond_to?(:each) ? render_collection(object, current_value) : render_resource(object, current_value)
|
||||||
|
@ -36,7 +36,7 @@ module RablRails
|
|||||||
options[:prefixes] = controller._prefixes
|
options[:prefixes] = controller._prefixes
|
||||||
options[:template] ||= template
|
options[:template] ||= template
|
||||||
|
|
||||||
controller.default_render options.merge(status: :created)
|
controller.default_render options.merge(status: :created, location: api_location)
|
||||||
else
|
else
|
||||||
head :no_content
|
head :no_content
|
||||||
end
|
end
|
||||||
|
@ -2,7 +2,7 @@ module RablRails
|
|||||||
class CompiledTemplate
|
class CompiledTemplate
|
||||||
attr_accessor :source, :data, :root_name
|
attr_accessor :source, :data, :root_name
|
||||||
|
|
||||||
delegate :[], :[]=, :merge!, :merge, :to => :source
|
delegate :[], :[]=, :merge!, :to => :source
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@source = {}
|
@source = {}
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
module RablRails
|
module RablRails
|
||||||
VERSION = '0.3.0'
|
VERSION = '0.2.2'
|
||||||
end
|
end
|
||||||
|
@ -113,7 +113,6 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
t = @compiler.compile_source(%{child(:user, :partial => 'users/base') })
|
t = @compiler.compile_source(%{child(:user, :partial => 'users/base') })
|
||||||
assert_equal( {:user => { :_data => :user, :id => :id } }, t.source)
|
assert_equal( {:user => { :_data => :user, :id => :id } }, t.source)
|
||||||
assert_equal( {:id => :id}, mock_template.source)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "glue is compiled as a child but with anonymous name" do
|
test "glue is compiled as a child but with anonymous name" do
|
||||||
@ -133,18 +132,6 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
}, t.source)
|
}, t.source)
|
||||||
end
|
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
|
test "extends use other template source as itself" do
|
||||||
template = mock('template', :source => { :id => :id })
|
template = mock('template', :source => { :id => :id })
|
||||||
RablRails::Library.reset_instance
|
RablRails::Library.reset_instance
|
||||||
|
@ -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
|
|
@ -61,11 +61,6 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|||||||
assert_equal %q({"name":"foobar"}), render_json_output
|
assert_equal %q({"name":"foobar"}), render_json_output
|
||||||
end
|
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
|
test "render collection with attributes" do
|
||||||
@data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')]
|
@data = [User.new(1, 'foo', 'male'), User.new(2, 'bar', 'female')]
|
||||||
@context.assigns['data'] = @data
|
@context.assigns['data'] = @data
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
require 'plist'
|
||||||
|
|
||||||
|
RablRails.plist_engine = Plist::Emit
|
||||||
|
|
||||||
class TestPlistRenderer < ActiveSupport::TestCase
|
class TestPlistRenderer < ActiveSupport::TestCase
|
||||||
INDENT_REGEXP = /\n(\s)*/
|
INDENT_REGEXP = /\n(\s)*/
|
||||||
|
@ -22,15 +22,6 @@ class <<Singleton
|
|||||||
end
|
end
|
||||||
|
|
||||||
require 'rabl-rails'
|
require 'rabl-rails'
|
||||||
require 'plist'
|
|
||||||
|
|
||||||
if RUBY_ENGINE == 'jruby'
|
|
||||||
require 'nokogiri'
|
|
||||||
else
|
|
||||||
require 'libxml'
|
|
||||||
end
|
|
||||||
|
|
||||||
RablRails.load_default_engines!
|
|
||||||
|
|
||||||
module ActiveSupport
|
module ActiveSupport
|
||||||
class TestCase
|
class TestCase
|
||||||
|
Loading…
Reference in New Issue
Block a user