class Orthoses::Trace::Attribute

def build_trace_hook

def build_trace_hook
  TracePoint.new(:call) do |tp1|
    mod_name = Orthoses::Utils.module_name(tp1.self)
    if m = tp1.self.to_s.match(/#<Class:([\w:]+)>/)
      mod_name = m[1]
    end
    next unless mod_name
    next unless target?(mod_name)
    is_singleton = tp1.self.singleton_class?
    prefix = is_singleton ? "self." : ""
    kind = tp1.method_id
    tp1.binding.local_variable_get(:names).each do |name|
      attr_module = Module.new{
        if [:attr_accessor, :attr_reader, :attr].include?(kind)
          define_method(name) do
            super()
          end
        end
        if [:attr_accessor, :attr_writer, :attr].include?(kind)
          define_method("#{name}=") do |object|
            super(object)
          end
        end
      }
      tp1.self.prepend attr_module
      if [:attr_accessor, :attr_reader, :attr].include?(kind)
        TracePoint.new(:return) do |tp2|
          type = Utils.object_to_rbs(tp2.return_value)
          @captured_dict[mod_name][[kind, prefix, name]] << type
        end.enable(target: attr_module.instance_method(name))
      end
      if [:attr_accessor, :attr_writer, :attr].include?(kind)
        TracePoint.new(:call) do |tp2|
          type = Utils.object_to_rbs(tp2.binding.local_variable_get(:object))
          @captured_dict[mod_name][[kind, prefix, name]] << type
        end.enable(target: attr_module.instance_method("#{name}="))
      end
    end
  end
end