Sha256: ca327dbfaefa178b598ed66708892027ea10676f4f4d6765edce176e60e261a0

Contents?: true

Size: 963 Bytes

Versions: 29

Compression:

Stored size: 963 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_model_callbacks :validation
      define_callbacks :validate, scope: :name
    end
    
    module ClassMethods
      def create!(attributes = {})
        new(attributes).tap do |object|
          object.save!
        end
      end
    end

    def valid?
      run_callbacks :validation do
        super
      end
    end

    def save(options={})
      perform_validations(options) ?  super : false
    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

29 entries across 29 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.8.4 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.8.3 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.8.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.8.1 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.8.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.12 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.11 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.10 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.9 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.8 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.7 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.6 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.5 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.4 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.3 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.1 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.7.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.6.4 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.6.3 lib/cassandra_object/validations.rb