Sha256: ab35263b45498416d573161bacbeb7437ec595e62aa8a6d7db57d8dc913e1c98

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

require 'rom/support/notifications'

require 'rom/changeset/create'
require 'rom/changeset/update'
require 'rom/changeset/delete'

module ROM
  module Plugins
    module Relation
      module Changeset
        TYPES = {
          create: ROM::Changeset::Create,
          update: ROM::Changeset::Update,
          delete: ROM::Changeset::Delete
        }.freeze

        extend Notifications::Listener

        subscribe('configuration.relations.class.ready') do |event|
          event[:relation].include(InstanceMethods)
        end

        module InstanceMethods

          # Create a changeset for a relation
          #
          # @return [Changeset]
          #
          # @api public
          def changeset(type, data = EMPTY_HASH)
            klass = type.is_a?(Symbol) ? TYPES.fetch(type) : type

            unless klass < ROM::Changeset
              raise ArgumentError, "+#{type.name}+ must be a Changeset descendant"
            end

            if klass < ROM::Changeset::Stateful
              klass.new(self, __data__: data)
            else
              klass.new(self)
            end
          rescue KeyError
            raise ArgumentError,
                  "+#{type.inspect}+ is not a valid changeset type. Must be one of: #{TYPES.keys.inspect}"
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rom-changeset-1.0.0.beta2 lib/rom/plugins/relation/changeset.rb
rom-changeset-1.0.0.beta1 lib/rom/plugins/relation/changeset.rb