Sha256: 6465ef494587fc7cf404f8773ed74bbdae7919c7da09c8bce01e6bab8c7c4af1

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module Daimon
  module Exhibition
    # TODO Depends on `primary_key`, not an `id`.

    class Mylist
      class << self
        def load_from_serialized(serialized)
          new(serialized)
        end
      end

      def initialize(ids = nil)
        @ids = (ids || {}).with_indifferent_access
      end

      def serialize
        @ids.dup
      end

      def ids_by_type(type = nil)
        if type
          # To keep key for type.
          {type => []}.with_indifferent_access.merge(@ids.deep_dup)
        else
          @ids.deep_dup
        end
      end

      def count_of(type)
        @ids[type].count
      end

      def count
        @ids.values.flatten.count
      end

      def has_item?(type = nil)
        (type ? count_of(type) : count) > 0
      end

      def empty?
        !has_item?
      end

      def exhibits_of(type)
        Mapping.detect_exhibits_by(type).where(id: @ids[type])
      end

      def include?(item)
        ids_for(item).include?(item.id)
      end

      def add(item)
        ids_for(item).push(item.id) unless include?(item)
      end

      def delete(item)
        ids_for(item).delete(item.id)
      end

      def clear(type = nil)
        if type
          @ids[type]&.clear
        else
          @ids.clear
        end
      end

      private

      def ids_for(item)
        @ids[type_of(item)] ||= []
      end

      def type_of(item)
        Mapping.class_to_type(item.class)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
daimon-exhibition-0.1.0 app/models/daimon/exhibition/mylist.rb