lib/json_builder/compiler.rb in json_builder-3.0.1 vs lib/json_builder/compiler.rb in json_builder-3.0.2

- old
+ new

@@ -1,6 +1,6 @@ -require 'blankslate' unless defined? BlankSlate +require 'blankslate' unless defined?(BlankSlate) require 'json_builder/member' module JSONBuilder class Compiler < BlankSlate class << self @@ -22,25 +22,30 @@ @_members = [] @_scope = options[:scope] @_callback = options[:callback] || true @_pretty_print = options[:pretty] || false - copy_instance_variables_from(@_scope, [:@assigns, :@helpers]) + # Only copy instance variables if there is a scope and presence of Rails + copy_instance_variables_from(@_scope) if @_scope end def compile(*args, &block) instance_exec(*args, &block) end def array(items, &block) - @_array = Elements.new(items, &block) + @_array = Elements.new(@_scope, items, &block) end # Need to return a Key instance to allow for arrays to be handled appropriately def method_missing(key, *args, &block) - member = Member.new(key, *args, &block) - @_members << member - member + if @_scope.respond_to?(key) + @_scope.send(key, *args, &block) + else + member = Member.new(key, @_scope, *args, &block) + @_members << member + member + end end alias_method :key, :method_missing # Once all nodes are compiled, build the string def to_s