Sha256: 649cf05f60e2e638f63e993250524d03f7152ef36f44688a90f8b77844fba36b

Contents?: true

Size: 1.31 KB

Versions: 28

Compression:

Stored size: 1.31 KB

Contents

module Lookbook
  class EntityCollection
    include Enumerable

    delegate_missing_to :entities

    attr_reader :entities

    def initialize(entities = nil)
      @_cache = {}
      @entities = []
      add(entities)
    end

    def add(to_add = nil)
      Array(to_add).each do |entity|
        unless find_by_path(entity.lookup_path)
          @entities.push(entity)
        end
      end
      clear_cache
    end

    def find_by_id(id)
      id = Utils.id(id)
      @entities.find { |entity| entity.id == id }
    end

    def find_by_path(path)
      @entities.find { |entity| entity.lookup_path.to_s == path.to_s }
    end

    def next(entity)
      index = entities.find_index { |i| i.lookup_path == entity.lookup_path }
      entities[index + 1] unless index.nil?
    end

    def previous(entity)
      index = entities.find_index { |i| i.lookup_path == entity.lookup_path }
      entities[index - 1] if !index.nil? && index > 0
    end

    def each(&block)
      if block
        entities.sort.each { |entity| yield entity }
      else
        to_enum(:each)
      end
    end

    def flat_map(...)
      entities.map(...).map { |e| e.respond_to?(:to_a) ? e.to_a : e }.flatten
    end

    def clear_all
      @entities = []
      clear_cache
    end

    protected

    def clear_cache
      @_cache = {}
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
lookbook-2.3.4 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.3.3 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.3.2 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.3.0 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.2.2 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.2.1 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.2.0 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.1.1 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.1.0 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.5 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.4 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.3 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.2 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.1 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.0 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.0.rc.3 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.0.rc.2 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.0.rc.1 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.0.beta.9 lib/lookbook/entities/collections/entity_collection.rb
lookbook-2.0.0.beta.8 lib/lookbook/entities/collections/entity_collection.rb