lib/gon.rb in gon-2.3.0 vs lib/gon.rb in gon-3.0.0

- old
+ new

@@ -1,10 +1,13 @@ if defined?(Jbuilder) gem 'blankslate' end require 'action_view' require 'action_controller' +require 'gon/base' +require 'gon/global' +require 'gon/request' require 'gon/helpers' require 'gon/escaper' if defined?(Rabl) require 'gon/rabl' end @@ -13,109 +16,79 @@ end module Gon class << self - def all_variables - @request_env['gon'] + def global + Gon::Global end - def clear - @request_env['gon'] = {} - end - - def request_env=(environment) - @request_env = environment - @request_env['gon'] ||= {} - end - - def request_env - if defined?(@request_env) - return @request_env - end - end - - def request - @request_id if defined? @request_id - end - - def request=(request_id) - @request_id = request_id - end - - def method_missing(m, *args, &block) - if ( m.to_s =~ /=$/ ) - if public_methods.include?(RUBY_VERSION > '1.9' ? m.to_s[0..-2].to_sym : m.to_s[0..-2]) + def method_missing(method, *args, &block) + if ( method.to_s =~ /=$/ ) + if public_method_name? method raise "You can't use Gon public methods for storing data" end - set_variable(m.to_s.delete('='), args[0]) + + set_variable(method.to_s.delete('='), args[0]) else - get_variable(m.to_s) + get_variable(method.to_s) end end - def get_variable(name) - @request_env['gon'][name] + def all_variables + Request.gon end - def set_variable(name, value) - @request_env['gon'][name] = value + def clear + Request.clear 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]) - else - options = (options && options.first.is_a?(Hash)) ? options.first : { } - end + def rabl(*args) + data, options = Gon::Rabl.handler(args) - builder_module = get_builder(builder_name) + store_builder_data 'rabl', data, options + end - data = builder_module.send("parse_#{builder_name}", get_template_path(options, builder_name), get_controller(options)) + def jbuilder(*args) + data, options = Gon::Jbuilder.handler(args) - 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 - end + store_builder_data 'jbuilder', data, options end - alias_method :orig_jbuilder, :jbuilder - def jbuilder(*options) - orig_jbuilder(*options) + def inspect + 'Gon' 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 + def get_variable(name) + Request.gon[name] 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') + def set_variable(name, value) + Request.gon[name] = value end - def get_template_path(options, extension) - if options[:template] - File.extname(options[:template]) == ".#{extension}" ? options[:template] : "#{options[:template]}.#{extension}" + def store_builder_data(builder, data, 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 - "app/views/#{get_controller(options).controller_path}/#{get_controller(options).action_name}.json.#{extension}" + set_variable(builder, data) end + end + + def public_method_name?(method) + public_methods.include?( + if RUBY_VERSION > '1.9' + method.to_s[0..-2].to_sym + else + method.to_s[0..-2] + end + ) end end end