Sha256: 57d6d2ecb8e4c6b2eea1e4c42d46e4d3f508773a1dd85a9208f664e8de02d742

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

require 'refinements/string_refinements'

module Dolly
  module DocumentType
    using StringRefinements

    def namespace_keys(keys)
      keys.map { |key| namespace_key key }
    end

    def namespace_key(key)
      return "#{key}" if "#{key}" =~ %r{^#{name_paramitized}#{type_sep}}
      "#{name_paramitized}#{type_sep}#{key}"
    end

    def base_id
      self.id.sub(%r{^#{name_paramitized}#{type_sep}}, '')
    end

    def name_paramitized
      class_name.split("::").last.underscore
    end

    def class_name
      is_a?(Class) ? name : self.class.name
    end

    def typed?
      respond_to?(:type)
    end

    def set_type
      return unless typed?
      write_attribute(:type, name_paramitized)
    end

    def type_sep
      return ':' if partitioned?
      '/'
    end

    def partitioned?
      false
    end

    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def absolute_id(id)
        id.sub(%r{^[^/:]+(/|:)}, '')
      end

      def typed_model
        property :type, class_name: String
      end

      def partitioned!
        check_db_partitioned!
        define_method(:partitioned?) { true }
      end

      def check_db_partitioned!
        !!connection.get('').
          dig(:props, :partitioned) ||
          raise(Dolly::PartitionedDataBaseExpectedError)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dolly-3.1.5 lib/dolly/document_type.rb
dolly-3.1.4 lib/dolly/document_type.rb
dolly-3.1.3 lib/dolly/document_type.rb