lib/gon.rb in gon-2.1.2 vs lib/gon.rb in gon-2.2.0

- old
+ new

@@ -1,30 +1,26 @@ -if RUBY_VERSION =~ /9/ && defined?(Jbuilder) +if RUBY_VERSION > '1.9' && defined?(Jbuilder) gem 'blankslate' end require 'action_view' require 'action_controller' require 'gon/helpers' require 'gon/escaper' if defined?(Rabl) require 'gon/rabl' end -if RUBY_VERSION =~ /9/ && defined?(Jbuilder) +if RUBY_VERSION > '1.9' && defined?(Jbuilder) require 'gon/jbuilder' end module Gon class << self def all_variables @request_env['gon'] end - def all_variables=(values) - raise "You can't use Gon public methods for storing data" - end - def clear @request_env['gon'] = {} end def request_env=(environment) @@ -46,11 +42,11 @@ @request_id = request_id end def method_missing(m, *args, &block) if ( m.to_s =~ /=$/ ) - if public_methods.include? m.to_s[0..-2].to_sym + if public_methods.include?(RUBY_VERSION > '1.9' ? m.to_s[0..-2].to_sym : m.to_s[0..-2]) raise "You can't use Gon public methods for storing data" end set_variable(m.to_s.delete('='), args[0]) else get_variable(m.to_s) @@ -63,52 +59,66 @@ def set_variable(name, value) @request_env['gon'][name] = value end - # TODO: Remove required argument view_path, and by default use current action - def rabl(view_path, options = {}) - if !defined?(Gon::Rabl) - raise NoMethodError.new('You should define Rabl in your Gemfile') - end + %w(rabl jbuilder).each do |builder_name| + define_method builder_name do |*options| + if options.first.is_a? String + warn "[DEPRECATION] view_path argument is now optional. If you need to specify it please use #{builder}(:template => 'path')" + options = options.extract_options!.merge(:template => options[0]) + elsif options.first.is_a? Hash + options = options ? options.first : { } + else + raise ArgumentError.new("You pass wrong argument type to template generator method: #{options.first.class.to_s}") + end - rabl_data = Gon::Rabl.parse_rabl(view_path, options[:controller] || - @request_env['action_controller.instance'] || - @request_env['action_controller.rescue.response']. - instance_variable_get('@template'). - instance_variable_get('@controller')) - if options[:as] - set_variable(options[:as].to_s, rabl_data) - elsif rabl_data.is_a? Hash - rabl_data.each do |key, value| - set_variable(key, value) + builder_module = get_builder(builder_name) + + data = builder_module.send("parse_#{builder_name}", get_template_path(options, builder_name), get_controller(options)) + + if options[:as] + set_variable(options[:as].to_s, data) + elsif data.is_a? Hash + data.each do |key, value| + set_variable(key, value) + end + else + set_variable(builder_name, data) end - else - set_variable('rabl', rabl_data) end end + alias_method :orig_jbuilder, :jbuilder - def jbuilder(view_path, options = {}) - if RUBY_VERSION !~ /9/ - raise NoMethodError.new('You can use Jbuilder support only in 1.9+') - elsif !defined?(Gon::Jbuilder) - raise NoMethodError.new('You should define Jbuilder in your Gemfile') + def jbuilder(*options) + raise NoMethodError.new('You can use Jbuilder support only in 1.9+') if RUBY_VERSION < '1.9' + orig_jbuilder(*options) + end + + private + + def get_builder(builder_name) + begin + "Gon::#{builder_name.classify}".constantize + rescue + raise NoMethodError.new("You should define #{builder_name.classify} in your Gemfile") end + end - jbuilder_data = Gon::Jbuilder.parse_jbuilder(view_path, options[:controller] || - @request_env['action_controller.instance'] || - @request_env['action_controller.rescue.response']. - instance_variable_get('@template'). - instance_variable_get('@controller')) - if options[:as] - set_variable(options[:as].to_s, jbuilder_data) - elsif jbuilder_data.is_a? Hash - jbuilder_data.each do |key, value| - set_variable(key, value) - end + def get_controller(options) + options[:controller] || + @request_env['action_controller.instance'] || + @request_env['action_controller.rescue.response']. + instance_variable_get('@template'). + instance_variable_get('@controller') + end + + def get_template_path(options, extension) + if options[:template] + File.extname(options[:template]) == ".#{extension}" ? options[:template] : "#{options[:template]}.#{extension}" else - set_variable('jbuilder', jbuilder_data) + "app/views/#{get_controller(options).controller_path}/#{get_controller(options).action_name}.json.#{extension}" end end - end + end end