Sha256: 3327f82e7c9abe6e0bf7e553feac9d035568e7652b6b56671ed1f22dd614d5f9

Contents?: true

Size: 1.4 KB

Versions: 7

Compression:

Stored size: 1.4 KB

Contents

module ActiveData
  module Model
    module Associations
      module Reflections
        class EmbedsAny < Base
          class << self
            def build(target, generated_methods, name, options = {}, &block)
              if block
                options[:class] = proc do |reflection|
                  superclass = reflection.options[:class_name].to_s.presence.try(:constantize)
                  klass = build_class(superclass)
                  target.const_set(name.to_s.classify, klass)
                  klass.class_eval(&block)
                  klass
                end
              end
              super
            end

            private def build_class(superclass)
              Class.new(superclass || ActiveData.base_class) do
                include ActiveData::Model
                include ActiveData::Model::Associations
                include ActiveData::Model::Lifecycle
                include ActiveData::Model::Primary
                include ActiveData.base_concern if ActiveData.base_concern
              end
            end
          end

          def klass
            @klass ||= if options[:class]
              options[:class].call(self)
            else
              super
            end
          end

          def inspect
            "#{self.class.name.demodulize}(#{klass})"
          end

          def embedded?
            true
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_data-1.2.0 lib/active_data/model/associations/reflections/embeds_any.rb
active_data-1.1.7 lib/active_data/model/associations/reflections/embeds_any.rb
active_data-1.1.6 lib/active_data/model/associations/reflections/embeds_any.rb
active_data-1.1.5 lib/active_data/model/associations/reflections/embeds_any.rb
active_data-1.1.4 lib/active_data/model/associations/reflections/embeds_any.rb
active_data-1.1.3 lib/active_data/model/associations/reflections/embeds_any.rb
active_data-1.1.2 lib/active_data/model/associations/reflections/embeds_any.rb