require 'pathname'

module TranspecAnalysis
  @base_path = Dir.pwd

  def self.data
    @data ||= {}
  end

  at_exit do
    # Use JSON rather than Marshal so that:
    # * Unknown third-party class information won't be serialized.
    #   (Such objects are stored as a string.)
    # * Singleton method information won't be serialized.
    #   (With Marshal.load, `singleton can't be dumped (TypeError)` will be raised.)
    require 'json'
    path = File.join(@base_path, '<%= RESULT_FILE %>')
    File.open(path, 'w') do |file|
      JSON.dump(data, file)
    end
  end
end

def <%= ANALYSIS_METHOD %>(object, context, node_id, analysis_codes)
  node_data = {}

  analysis_codes.each do |key, (target_type, code)|
    target = case target_type
             when :object  then object
             when :context then context
             end

    begin
      node_data[key] = target.instance_eval(code)
    rescue Exception
    end
  end

  TranspecAnalysis.data[node_id] = node_data

  object
end

module TranspecAnalysis
  @base_path = Dir.pwd

  def self.data
    @data ||= {}
  end

  at_exit do
    # Use JSON rather than Marshal so that:
    # * Unknown third-party class information won't be serialized.
    #   (Such objects are stored as a string.)
    # * Singleton method information won't be serialized.
    #   (With Marshal.load, `singleton can't be dumped (TypeError)` will be raised.)
    require 'json'
    path = File.join(@base_path, '<%= RESULT_FILE %>')
    File.open(path, 'w') do |file|
      JSON.dump(data, file)
    end
  end
end

def <%= ANALYSIS_METHOD %>(object, context, node_id, analysis_codes)
  node_data = {}

  analysis_codes.each do |key, (target_type, code)|
    target = case target_type
             when :object  then object
             when :context then context
             end

    begin
      node_data[key] = target.instance_eval(code)
    rescue Exception
    end
  end

  TranspecAnalysis.data[node_id] = node_data

  object
end

Remove unneeded require of pathname in dynamic analysis helper
Remove unneeded require of pathname in dynamic analysis helper
