Sha256: 0ef9d77feba95766272cd83960d785a38a523b6f13b4ba7c3356f55714a94b52

Contents?: true

Size: 1.05 KB

Versions: 26

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

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/entity/src/concerns/primary_key.rb
foobara-0.0.25 projects/entity/src/concerns/primary_key.rb
foobara-0.0.24 projects/entity/src/concerns/primary_key.rb
foobara-0.0.23 projects/entity/src/concerns/primary_key.rb
foobara-0.0.22 projects/entity/src/concerns/primary_key.rb
foobara-0.0.21 projects/entity/src/concerns/primary_key.rb
foobara-0.0.20 projects/entity/src/concerns/primary_key.rb
foobara-0.0.19 projects/entity/src/concerns/primary_key.rb
foobara-0.0.18 projects/entity/src/concerns/primary_key.rb
foobara-0.0.17 projects/entity/src/concerns/primary_key.rb
foobara-0.0.16 projects/entity/src/concerns/primary_key.rb
foobara-0.0.15 projects/entity/src/concerns/primary_key.rb
foobara-0.0.14 projects/entity/src/concerns/primary_key.rb
foobara-0.0.13 projects/entity/src/concerns/primary_key.rb
foobara-0.0.12 projects/entity/src/concerns/primary_key.rb
foobara-0.0.11 projects/entity/src/concerns/primary_key.rb
foobara-0.0.10 projects/entity/src/concerns/primary_key.rb
foobara-0.0.9 projects/entity/src/concerns/primary_key.rb
foobara-0.0.8 projects/entity/src/concerns/primary_key.rb
foobara-0.0.7 projects/entity/src/concerns/primary_key.rb