Sha256: 66b126404f618b5fad7a141e95b1652a95fa053b947e2dc98b485fcedec2a277

Contents?: true

Size: 887 Bytes

Versions: 21

Compression:

Stored size: 887 Bytes

Contents

module CassandraObject
  class RecordInvalid < StandardError
    attr_reader :record
    def initialize(record)
      @record = record
      super("Invalid record: #{@record.errors.full_messages.to_sentence}")
    end
  end

  module Validations
    extend ActiveSupport::Concern
    include ActiveModel::Validations
    
    included do
      define_callbacks :validate, scope: :name
    end
    
    module ClassMethods
      def create!(attributes = {})
        new(attributes).tap do |object|
          object.save!
        end
      end
    end

    def save(options={})
      if perform_validations(options)
        super
        true
      else
        false
      end
    end
    
    def save!
      save || raise(RecordInvalid.new(self))
    end

    protected
      def perform_validations(options={})
        (options[:validate] != false) ? valid? : true
      end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
gotime-cassandra_object-4.5.1 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.5.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.4.5 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.4.4 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.4.3 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.4.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.3.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.3.1 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.3.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.2.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.2.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.1.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.0.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.0.1 lib/cassandra_object/validations.rb
gotime-cassandra_object-4.0.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-3.0.5 lib/cassandra_object/validations.rb
gotime-cassandra_object-3.0.4 lib/cassandra_object/validations.rb
gotime-cassandra_object-3.0.3 lib/cassandra_object/validations.rb
gotime-cassandra_object-3.0.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-3.0.1 lib/cassandra_object/validations.rb