Sha256: 9ca18a2f4995d2e661294b141e73f968f002edeee5845ca07d7e46124f8df15b

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module ROM
  module SQL
    module Commands
      ERRORS = [
        Sequel::UniqueConstraintViolation,
        Sequel::NotNullConstraintViolation
      ].freeze

      class Create < ROM::Commands::Create

        def execute(tuples)
          pks = Array([tuples]).flatten.map do |tuple|
            attributes = input[tuple]
            validator.call(attributes)
            relation.insert(attributes.to_h)
          end

          relation.where(relation.model.primary_key => pks)
        rescue *ERRORS => e
          raise ConstraintError, e.message
        end
      end

      class Update < ROM::Commands::Update

        def execute(tuple)
          attributes = input[tuple]
          validator.call(attributes)

          pks = relation.map { |t| t[relation.model.primary_key] }

          relation.update(attributes.to_h)
          relation.unfiltered.where(relation.model.primary_key => pks)
        end

      end

      class Delete < ROM::Commands::Delete

        def execute
          deleted = target.to_a
          target.delete
          deleted
        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-sql-0.3.0 lib/rom/sql/commands.rb