lib/gon.rb in gon-4.1.1 vs lib/gon.rb in gon-5.0.0

- old
+ new

@@ -19,43 +19,43 @@ def watch Gon::Watch end 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" + if method.to_s =~ /=$/ + if public_method_name?(method) + raise 'You can\'t use Gon public methods for storing data' end set_variable(method.to_s.delete('='), args[0]) else get_variable(method.to_s) end end def get_variable(name) - Request.gon[name] + current_gon.gon[name] end def set_variable(name, value) - Request.gon[name] = value + current_gon.gon[name] = value end def push(data = {}) - raise "Object must have each_pair method" unless data.respond_to? :each_pair + raise 'Object must have each_pair method' unless data.respond_to? :each_pair data.each_pair do |name, value| set_variable(name.to_s, value) end end def all_variables - Request.gon + current_gon.gon end def clear - Request.clear + current_gon.clear end def rabl(*args) data, options = Gon::Rabl.handler(args) store_builder_data 'rabl', data, options @@ -71,16 +71,18 @@ 'Gon' end private + def current_gon + Thread.current['gon'] + end + 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 + data.each { |k, v| set_variable(k, v) } else set_variable(builder, data) end end