I need to define some class variables that set namespace and table class variables.
This is the mixin template I'm using:
module MyMixin
module ClassMethods
....
end
module InstanceMethods
....
end
def self.included(receiver)
namespace, table = receiver.name.underscore.pluralize.split('/')
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
end
For the code below, I would like to have class variables of namespace: 'hello' and table: 'worlds'
module Hello
class World
include MyMixin
end
end
For the code below, I would like to have class variables of namespace: 'goodbye' and table: 'friends'
module Goodbye
class Friend
include MyMixin
end
end
I tried using receiver.class_variable_set/get but when I load the Goodbye::Friend code, the class variables of Hello::World .
How can I set and separate both class variables?
I realized that I could use instance_variables to keep the separate "class" variables for my class that was instantiate through a mixin template.
module MyMixin
module ClassMethods
....
end
module InstanceMethods
....
end
def self.included(receiver)
namespace, table = receiver.name.underscore.pluralize.split('/')
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
receiver.instance_variable_set :@namespace, namespace.to_sym
receiver.instance_variable_set :@table, table.to_sym
receiver.instance_variable_set :@properties, {}
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.