Code Monkey home page Code Monkey logo

Comments (4)

ream88 avatar ream88 commented on August 27, 2024

Btw this could be more of an ActiveJob problem, just tell me if this belongs to the Rails repository.

from globalid.

rafaelfranca avatar rafaelfranca commented on August 27, 2024

I don't know if it is a good idea to include this behavior by default. Right now I think you can implement your own to_global_id implementation.

from globalid.

kaspth avatar kaspth commented on August 27, 2024

Yeah, I don't see this in Global ID directly. I'd probably create some sort of wrapper model that takes a method or class and implements find. Good luck ❤️

from globalid.

ream88 avatar ream88 commented on August 27, 2024

Just for the record, and if anybody else needs something similar:
Solved it for now using this dirty (overwriting private methods 👎) module:

# class_and_method_arguments.rb
require 'active_job/arguments'


module ActiveJob
  module ClassAndMethodArguments
    def serialize_argument(argument)
      case argument
      when Class, Module
        URI::GID.build(app: GlobalID.app, model_name: argument.name, model_id: :itself).to_s
      when Method
        URI::GID.build(app: GlobalID.app, model_name: argument.receiver.name, model_id: argument.name).to_s
      else
        super
      end
    end


    def deserialize_argument(argument)
      case argument
      when String
        gid = URI::GID.parse(argument)

        # GID probably references an Model instance,
        # when model_id starts with numeric characters.
        case gid.model_id
        when /^\d+/
          super
        when 'itself'
          gid.model_name.constantize
        else
          gid.model_name.constantize.method(gid.model_id)
        end
      else
        super
      end
    rescue URI::BadURIError
      super
    end
  end


  Arguments.send :extend, ClassAndMethodArguments
end
# class_and_method_arguments_test.rb
require_relative '../../test_helper'


module ActiveJob
  class ClassAndMethodArgumentsTest < Minitest::Test
    def test_classes
      argument = [ActiveJob::Base]

      assert_equal argument, Arguments.deserialize(Arguments.serialize(argument))
    end


    def test_modules
      argument = [ActiveJob]

      assert_equal argument, Arguments.deserialize(Arguments.serialize(argument))
    end


    def test_methods
      argument = [ActiveJob::Base.method(:new)]

      assert_equal argument, Arguments.deserialize(Arguments.serialize(argument))
    end
  end
end

from globalid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.