Sha256: 2db4fe965ff3ce4115306e70f6ec6e6920f8f4e8a5cf03554bfd40547acbb547

Contents?: true

Size: 1.31 KB

Versions: 12

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'
require 'media_types/serialization/migrations_command'

module MediaTypes
  module Serialization
    module MigrationsSupport
      extend ActiveSupport::Concern

      included do
        # This is the same as doing matrr_accessor but have it isolated to the class. Subclass changes to change these
        # values, but this allows for definition as a concern.

        class << self
          attr_accessor :migrations
        end

        delegate :migrations,
                 :migrations=,
                 to: :class
      end

      class_methods do
        def migrator(serializer)
          return nil unless migrations
          migrations.call(serializer)
        end

        protected

        def backward_migrations(&block)
          self.migrations = lambda do |serializer|
            MigrationsCommand.new(serializer).tap do |callable|
              callable.instance_exec(&block)
            end
          end
        end
      end

      def migrate(result = nil, media_type = current_media_type, view = current_view)
        result ||= yield

        migrator = self.class.migrator(self)
        return result unless migrator
        migrator.call(result, media_type, view)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
media_types-serialization-0.8.1 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.8.0 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.7.0 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.7.0.beta1 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.6.2 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.6.1 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.6.0 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.5.1 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.5.0 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.4.0 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.3.2 lib/media_types/serialization/migrations_support.rb
media_types-serialization-0.3.1 lib/media_types/serialization/migrations_support.rb