Method: Trax::Model::ClassMethods#mixin

Defined in:
lib/trax/model.rb

#mixin(key, options = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/trax/model.rb', line 118

def mixin(key, options = {})
  raise ::Trax::Model::Errors::MixinNotRegistered.new(
    model: self.name,
    mixin: key
  ) unless ::Trax::Model.mixin_registry.key?(key)

  mixin_module = ::Trax::Model.mixin_registry[key]
  self.registered_mixins[key] = mixin_module

  self.class_eval do
    include(mixin_module) unless self.ancestors.include?(mixin_module)

    options = {} if options.is_a?(TrueClass)
    options = { options => true } if options.is_a?(Symbol)
    mixin_module.apply_mixin(self, options) if mixin_module.respond_to?(:apply_mixin)

    if mixin_module.instance_variable_defined?(:@_after_included_block)
      block = mixin_module.instance_variable_get(:@_after_included_block)

      instance_exec(options, &block)
    end
  end
end