Module: DataMapper::Property::Discriminator::Model

Defined in:
lib/dm-core/property/discriminator.rb

Instance Method Summary collapse

Instance Method Details

#inherited(model) ⇒ Object


13
14
15
16
# File 'lib/dm-core/property/discriminator.rb', line 13

def inherited(model)
  super  # setup self.descendants
  set_discriminator_scope_for(model)
end

#new(*args, &block) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dm-core/property/discriminator.rb', line 18

def new(*args, &block)
  if args.size == 1 && args.first.kind_of?(Hash)
    discriminator = properties(repository_name).discriminator

    if discriminator_value = args.first[discriminator.name]
      model = discriminator.typecast_to_primitive(discriminator_value)

      if model.kind_of?(Model) && !model.equal?(self)
        return model.new(*args, &block)
      end
    end
  end

  super
end