Sha256: 0ef9d77feba95766272cd83960d785a38a523b6f13b4ba7c3356f55714a94b52

Contents?: true

Size: 1.05 KB

Versions: 27

Compression:

Stored size: 1.05 KB

Contents

module Foobara
  class Entity < 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 == Entity
              @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

27 entries across 27 versions & 1 rubygems

Version Path
foobara-0.0.7 projects/entity/src/concerns/primary_key.rb
foobara-0.0.6 projects/entity/src/concerns/primary_key.rb
foobara-0.0.5 projects/entity/src/concerns/primary_key.rb
foobara-0.0.4 projects/entity/src/concerns/primary_key.rb
foobara-0.0.3 projects/entity/src/concerns/primary_key.rb
foobara-0.0.2 projects/entity/src/concerns/primary_key.rb
foobara-0.0.1 projects/entity/src/concerns/primary_key.rb