Sha256: ed2a022d8c378c413780d815965bbceb01dad64d250071c395cc360cd589775e

Contents?: true

Size: 839 Bytes

Versions: 19

Compression:

Stored size: 839 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={})
      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

19 entries across 19 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.13.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.12.5 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.12.4 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.12.3 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.12.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.12.1 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.12.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.9 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.8 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.7 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.6 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.5 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.4 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.3 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.1 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.11.0 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.10.11 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.10.10 lib/cassandra_object/validations.rb