Sha256: ad189f8c898bcc178aba9029035f639c127da11d2ba015852d70ffa51d9cf9ec

Contents?: true

Size: 966 Bytes

Versions: 30

Compression:

Stored size: 966 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

30 entries across 30 versions & 2 rubygems

Version Path
sessionm-cassandra_object-2.2.35 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.34 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.33 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.32 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.31 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.30 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.29 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.28 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.27 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.26 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.24 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.23 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.22 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.20 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.18 lib/cassandra_object/validations.rb
sessionm-cassandra_object-2.2.16 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.3.4 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.3.3 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.3.2 lib/cassandra_object/validations.rb
gotime-cassandra_object-2.3.1 lib/cassandra_object/validations.rb