Sha256: 63867bd6779b6f964cbaefbda00e436dd172345fc6ce8bd58f145d4871301c6d

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

module Foobara
  class DetachedEntity < Model
    module Concerns
      module PrimaryKey
        include Concern

        foobara_delegate :primary_key_attribute, to: :class

        module ClassMethods
          def primary_key(attribute_name)
            if primary_key_attribute
              # :nocov:
              raise "Primary key already set to #{primary_key_attribute}"
              # :nocov:
            end

            if attribute_name.nil? || attribute_name.empty?
              # :nocov:
              raise ArgumentError, "Primary key can't be blank"
              # :nocov:
            end

            @primary_key_attribute = attribute_name.to_sym

            set_model_type
          end

          def primary_key_attribute
            return @primary_key_attribute if @primary_key_attribute

            unless superclass == DetachedEntity
              @primary_key_attribute = superclass.primary_key_attribute
            end
          end
        end

        def primary_key
          read_attribute(primary_key_attribute)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
foobara-0.0.38 projects/detached_entity/src/concerns/primary_key.rb
foobara-0.0.37 projects/detached_entity/src/concerns/primary_key.rb
foobara-0.0.36 projects/detached_entity/src/concerns/primary_key.rb
foobara-0.0.35 projects/detached_entity/src/concerns/primary_key.rb
foobara-0.0.34 projects/detached_entity/src/concerns/primary_key.rb
foobara-0.0.33 projects/detached_entity/src/concerns/primary_key.rb
foobara-0.0.32 projects/detached_entity/src/concerns/primary_key.rb
foobara-0.0.31 projects/detached_entity/src/concerns/primary_key.rb