How to dynamically alter inheritance in Ruby

后端 未结 7 1360
粉色の甜心
粉色の甜心 2020-12-06 11:30

I would like to dynamically specify the parent class for a class in Ruby. Consider this code:

class Agent
  def self.hook_up(calling_class, desired_parent_c         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-06 12:09

    Look at this

      class MyClass < inherit_orm("Adapter")
      end
    

    And the class selector:

      def inherit_orm(model="Activity", orm=nil)
        orm = Config.orm || orm
        require "orm/#{orm.to_s}"
        "ORM::#{orm.to_s.classify}::#{model}".constantize
      end
    

    So, when instance MyClass it will be inherit from a dynamic class depending of orm and model. Be sure to define both in a module. It work fine in public_activity gem ( selector example ).

    I hope to help.. Bye!

提交回复
热议问题