rabl-fast-json => rabl-rails
This commit is contained in:
parent
f9e2f0766b
commit
c12e14700f
2
Gemfile
2
Gemfile
@ -1,6 +1,6 @@
|
|||||||
source "http://rubygems.org"
|
source "http://rubygems.org"
|
||||||
|
|
||||||
# Declare your gem's dependencies in rabl-fast-json.gemspec.
|
# Declare your gem's dependencies in rabl-rails.gemspec.
|
||||||
# Bundler will treat runtime dependencies like base dependencies, and
|
# Bundler will treat runtime dependencies like base dependencies, and
|
||||||
# development dependencies will be added by default to the :development group.
|
# development dependencies will be added by default to the :development group.
|
||||||
gemspec
|
gemspec
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
PATH
|
PATH
|
||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
rabl-fast-json (0.1.0)
|
rabl-rails (0.1.0)
|
||||||
activesupport (~> 3.2.1)
|
activesupport (~> 3.2.1)
|
||||||
|
|
||||||
GEM
|
GEM
|
||||||
@ -61,7 +61,7 @@ PLATFORMS
|
|||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
actionpack (~> 3.2.1)
|
actionpack (~> 3.2.1)
|
||||||
rabl-fast-json!
|
rabl-rails!
|
||||||
railties (~> 3.2.1)
|
railties (~> 3.2.1)
|
||||||
rspec-mocks
|
rspec-mocks
|
||||||
sqlite3
|
sqlite3
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
= RablFastJson
|
= RablRails
|
||||||
|
|
||||||
This project rocks and uses MIT-LICENSE.
|
This project rocks and uses MIT-LICENSE.
|
2
Rakefile
Normal file → Executable file
2
Rakefile
Normal file → Executable file
@ -14,7 +14,7 @@
|
|||||||
#
|
#
|
||||||
# RDoc::Task.new(:rdoc) do |rdoc|
|
# RDoc::Task.new(:rdoc) do |rdoc|
|
||||||
# rdoc.rdoc_dir = 'rdoc'
|
# rdoc.rdoc_dir = 'rdoc'
|
||||||
# rdoc.title = 'RablFastJson'
|
# rdoc.title = 'RablRails'
|
||||||
# rdoc.options << '--line-numbers'
|
# rdoc.options << '--line-numbers'
|
||||||
# rdoc.rdoc_files.include('README.rdoc')
|
# rdoc.rdoc_files.include('README.rdoc')
|
||||||
# rdoc.rdoc_files.include('lib/**/*.rb')
|
# rdoc.rdoc_files.include('lib/**/*.rb')
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
require 'rabl-fast-json/renderers/base'
|
|
||||||
require 'rabl-fast-json/renderers/json'
|
|
@ -4,19 +4,19 @@ require 'active_support'
|
|||||||
require 'active_support/json'
|
require 'active_support/json'
|
||||||
require 'active_support/core_ext/class/attribute_accessors'
|
require 'active_support/core_ext/class/attribute_accessors'
|
||||||
|
|
||||||
require 'rabl-fast-json/version'
|
require 'rabl-rails/version'
|
||||||
require 'rabl-fast-json/template'
|
require 'rabl-rails/template'
|
||||||
require 'rabl-fast-json/compiler'
|
require 'rabl-rails/compiler'
|
||||||
|
|
||||||
require 'rabl-fast-json/renderer'
|
require 'rabl-rails/renderer'
|
||||||
|
|
||||||
require 'rabl-fast-json/library'
|
require 'rabl-rails/library'
|
||||||
require 'rabl-fast-json/handler'
|
require 'rabl-rails/handler'
|
||||||
require 'rabl-fast-json/railtie'
|
require 'rabl-rails/railtie'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module RablFastJson
|
module RablRails
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
mattr_accessor :cache_templates
|
mattr_accessor :cache_templates
|
@ -1,4 +1,4 @@
|
|||||||
module RablFastJson
|
module RablRails
|
||||||
#
|
#
|
||||||
# Class that will compile RABL source code into a hash
|
# Class that will compile RABL source code into a hash
|
||||||
# representing data structure
|
# representing data structure
|
@ -1,4 +1,4 @@
|
|||||||
module RablFastJson
|
module RablRails
|
||||||
module Handlers
|
module Handlers
|
||||||
class Rabl
|
class Rabl
|
||||||
cattr_accessor :default_format
|
cattr_accessor :default_format
|
||||||
@ -6,7 +6,7 @@ module RablFastJson
|
|||||||
|
|
||||||
def self.call(template)
|
def self.call(template)
|
||||||
%{
|
%{
|
||||||
RablFastJson::Library.instance.
|
RablRails::Library.instance.
|
||||||
get_rendered_template(#{template.source.inspect}, self)
|
get_rendered_template(#{template.source.inspect}, self)
|
||||||
}
|
}
|
||||||
end
|
end
|
@ -1,6 +1,6 @@
|
|||||||
require 'singleton'
|
require 'singleton'
|
||||||
|
|
||||||
module RablFastJson
|
module RablRails
|
||||||
class Library
|
class Library
|
||||||
include Singleton
|
include Singleton
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ module RablFastJson
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_compiled_template(path, source)
|
def get_compiled_template(path, source)
|
||||||
if path && RablFastJson.cache_templates?
|
if path && RablRails.cache_templates?
|
||||||
@cached_templates[path] ||= Compiler.new.compile_source(source)
|
@cached_templates[path] ||= Compiler.new.compile_source(source)
|
||||||
@cached_templates[path].dup
|
@cached_templates[path].dup
|
||||||
else
|
else
|
@ -1,8 +1,8 @@
|
|||||||
module RablFastJson
|
module RablRails
|
||||||
class Railtie < Rails::Railtie
|
class Railtie < Rails::Railtie
|
||||||
initializer "rabl.initialize" do |app|
|
initializer "rabl.initialize" do |app|
|
||||||
ActiveSupport.on_load(:action_view) do
|
ActiveSupport.on_load(:action_view) do
|
||||||
ActionView::Template.register_template_handler :rabl, RablFastJson::Handlers::Rabl
|
ActionView::Template.register_template_handler :rabl, RablRails::Handlers::Rabl
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
2
lib/rabl-rails/renderer.rb
Normal file
2
lib/rabl-rails/renderer.rb
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
require 'rabl-rails/renderers/base'
|
||||||
|
require 'rabl-rails/renderers/json'
|
@ -1,4 +1,4 @@
|
|||||||
module RablFastJson
|
module RablRails
|
||||||
module Renderers
|
module Renderers
|
||||||
class PartialError < StandardError; end
|
class PartialError < StandardError; end
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
module RablFastJson
|
module RablRails
|
||||||
module Renderers
|
module Renderers
|
||||||
class JSON < Base
|
class JSON < Base
|
||||||
def format_output(hash)
|
def format_output(hash)
|
@ -1,4 +1,4 @@
|
|||||||
module RablFastJson
|
module RablRails
|
||||||
class CompiledTemplate
|
class CompiledTemplate
|
||||||
attr_accessor :source, :data, :root_name
|
attr_accessor :source, :data, :root_name
|
||||||
|
|
@ -1,3 +1,3 @@
|
|||||||
module RablFastJson
|
module RablRails
|
||||||
VERSION = '0.1.0'
|
VERSION = '0.1.0'
|
||||||
end
|
end
|
@ -1,4 +1,4 @@
|
|||||||
# desc "Explaining what the task does"
|
# desc "Explaining what the task does"
|
||||||
# task :rabl-fast-json do
|
# task :rabl-rails do
|
||||||
# # Task goes here
|
# # Task goes here
|
||||||
# end
|
# end
|
@ -1,17 +1,17 @@
|
|||||||
$:.push File.expand_path("../lib", __FILE__)
|
$:.push File.expand_path("../lib", __FILE__)
|
||||||
|
|
||||||
# Maintain your gem's version:
|
# Maintain your gem's version:
|
||||||
require "rabl-fast-json/version"
|
require "rabl-rails/version"
|
||||||
|
|
||||||
# Describe your gem and declare its dependencies:
|
# Describe your gem and declare its dependencies:
|
||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = "rabl-fast-json"
|
s.name = "rabl-rails"
|
||||||
s.version = RablFastJson::VERSION
|
s.version = RablRails::VERSION
|
||||||
s.authors = ["TODO: Your name"]
|
s.authors = ["TODO: Your name"]
|
||||||
s.email = ["TODO: Your email"]
|
s.email = ["TODO: Your email"]
|
||||||
s.homepage = "TODO"
|
s.homepage = "TODO"
|
||||||
s.summary = "TODO: Summary of RablFastJson."
|
s.summary = "TODO: Summary of RablRails."
|
||||||
s.description = "TODO: Description of RablFastJson."
|
s.description = "TODO: Description of RablRails."
|
||||||
|
|
||||||
s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
|
s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
|
||||||
s.test_files = Dir["test/**/*"]
|
s.test_files = Dir["test/**/*"]
|
@ -3,9 +3,9 @@ require 'test_helper'
|
|||||||
class CacheTemplatesTest < ActiveSupport::TestCase
|
class CacheTemplatesTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
RablFastJson::Library.reset_instance
|
RablRails::Library.reset_instance
|
||||||
@library = RablFastJson::Library.instance
|
@library = RablRails::Library.instance
|
||||||
RablFastJson.cache_templates = true
|
RablRails.cache_templates = true
|
||||||
end
|
end
|
||||||
|
|
||||||
test "cache templates if perform_caching is active and cache_templates is enabled" do
|
test "cache templates if perform_caching is active and cache_templates is enabled" do
|
||||||
|
@ -4,11 +4,11 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
setup do
|
setup do
|
||||||
@user = User.new
|
@user = User.new
|
||||||
@compiler = RablFastJson::Compiler.new
|
@compiler = RablRails::Compiler.new
|
||||||
end
|
end
|
||||||
|
|
||||||
test "compiler return a compiled template" do
|
test "compiler return a compiled template" do
|
||||||
assert_instance_of RablFastJson::CompiledTemplate, @compiler.compile_source("")
|
assert_instance_of RablRails::CompiledTemplate, @compiler.compile_source("")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "object set data for the template" do
|
test "object set data for the template" do
|
||||||
@ -91,10 +91,10 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test "child with succint partial notation" do
|
test "child with succint partial notation" do
|
||||||
mock_template = RablFastJson::CompiledTemplate.new
|
mock_template = RablRails::CompiledTemplate.new
|
||||||
mock_template.source = { :id => :id }
|
mock_template.source = { :id => :id }
|
||||||
RablFastJson::Library.reset_instance
|
RablRails::Library.reset_instance
|
||||||
RablFastJson::Library.instance.stub(:get).with('users/base').and_return(mock_template)
|
RablRails::Library.instance.stub(:get).with('users/base').and_return(mock_template)
|
||||||
|
|
||||||
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)
|
||||||
@ -119,8 +119,8 @@ class CompilerTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
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 })
|
||||||
RablFastJson::Library.reset_instance
|
RablRails::Library.reset_instance
|
||||||
RablFastJson::Library.instance.stub(:get).with('users/base').and_return(template)
|
RablRails::Library.instance.stub(:get).with('users/base').and_return(template)
|
||||||
t = @compiler.compile_source(%{ extends 'users/base' })
|
t = @compiler.compile_source(%{ extends 'users/base' })
|
||||||
assert_equal({ :id => :id }, t.source)
|
assert_equal({ :id => :id }, t.source)
|
||||||
end
|
end
|
||||||
|
@ -15,7 +15,7 @@ class DeepNestingTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
RablFastJson::Library.reset_instance
|
RablRails::Library.reset_instance
|
||||||
@post = Post.new(42, 'I rock !')
|
@post = Post.new(42, 'I rock !')
|
||||||
@user = User.new(1, 'foobar', 'male')
|
@user = User.new(1, 'foobar', 'male')
|
||||||
@user.stub(:posts).and_return([@post])
|
@user.stub(:posts).and_return([@post])
|
||||||
@ -50,7 +50,7 @@ class DeepNestingTest < ActiveSupport::TestCase
|
|||||||
{ :content => 'second' }
|
{ :content => 'second' }
|
||||||
]
|
]
|
||||||
}]
|
}]
|
||||||
}), RablFastJson::Library.instance.get_rendered_template(source, @context))
|
}), RablRails::Library.instance.get_rendered_template(source, @context))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ require 'test_helper'
|
|||||||
|
|
||||||
class NonRestfulResponseTest < ActiveSupport::TestCase
|
class NonRestfulResponseTest < ActiveSupport::TestCase
|
||||||
setup do
|
setup do
|
||||||
RablFastJson::Library.reset_instance
|
RablRails::Library.reset_instance
|
||||||
|
|
||||||
@user = User.new(1, 'foo', 'male')
|
@user = User.new(1, 'foo', 'male')
|
||||||
@user.stub_chain(:posts, :count).and_return(10)
|
@user.stub_chain(:posts, :count).and_return(10)
|
||||||
@ -30,6 +30,6 @@ class NonRestfulResponseTest < ActiveSupport::TestCase
|
|||||||
:id => 1,
|
:id => 1,
|
||||||
:name => 'foo'
|
:name => 'foo'
|
||||||
}
|
}
|
||||||
}), RablFastJson::Library.instance.get_rendered_template(source, @context))
|
}), RablRails::Library.instance.get_rendered_template(source, @context))
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -10,12 +10,12 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|||||||
@context.stub(:instance_variable_get).with(:@data).and_return(@data)
|
@context.stub(:instance_variable_get).with(:@data).and_return(@data)
|
||||||
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
|
@context.stub(:instance_variable_get).with(:@_assigns).and_return({})
|
||||||
|
|
||||||
@template = RablFastJson::CompiledTemplate.new
|
@template = RablRails::CompiledTemplate.new
|
||||||
@template.data = :@data
|
@template.data = :@data
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_json_output
|
def render_json_output
|
||||||
RablFastJson::Renderers::JSON.new(@context).render(@template).to_s
|
RablRails::Renderers::JSON.new(@context).render(@template).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
test "render object wth empty template" do
|
test "render object wth empty template" do
|
||||||
@ -83,10 +83,10 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|||||||
@data.stub(:respond_to?).with(:empty?).and_return(false)
|
@data.stub(:respond_to?).with(:empty?).and_return(false)
|
||||||
|
|
||||||
# Stub Library#get
|
# Stub Library#get
|
||||||
t = RablFastJson::CompiledTemplate.new
|
t = RablRails::CompiledTemplate.new
|
||||||
t.source = { :name => :name }
|
t.source = { :name => :name }
|
||||||
RablFastJson::Library.reset_instance
|
RablRails::Library.reset_instance
|
||||||
RablFastJson::Library.instance.should_receive(:get).with('users/base').and_return(t)
|
RablRails::Library.instance.should_receive(:get).with('users/base').and_return(t)
|
||||||
|
|
||||||
@template.data = false
|
@template.data = false
|
||||||
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
|
@template.source = { :user => ->(s) { partial('users/base', :object => @user) } }
|
||||||
@ -98,7 +98,7 @@ class TestJsonRenderer < ActiveSupport::TestCase
|
|||||||
@template.data = false
|
@template.data = false
|
||||||
@template.source = { :user => ->(s) { partial('users/base') } }
|
@template.source = { :user => ->(s) { partial('users/base') } }
|
||||||
|
|
||||||
assert_raises(RablFastJson::Renderers::PartialError) { render_json_output }
|
assert_raises(RablRails::Renderers::PartialError) { render_json_output }
|
||||||
end
|
end
|
||||||
|
|
||||||
test "partial with empty values should not raise an error" do
|
test "partial with empty values should not raise an error" do
|
||||||
|
@ -21,7 +21,7 @@ class <<Singleton
|
|||||||
alias_method_chain :included, :reset
|
alias_method_chain :included, :reset
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'rabl-fast-json'
|
require 'rabl-rails'
|
||||||
|
|
||||||
module ActiveSupport
|
module ActiveSupport
|
||||||
class TestCase
|
class TestCase
|
||||||
|
Loading…
Reference in New Issue
Block a user