Sha256: 2ebeda095e3cb3bfc7c34d7d1ae2f9d415b692244ef62d4a65a06e1c6af66729

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

# frozen_string_literal: true

module Phlex
	module Collection
		def initialize(collection: nil, item: nil)
			unless collection || item
				raise ArgumentError, "You must pass a collection or an item as a keyword argument."
			end

			@collection = collection
			@item = item
		end

		def template
			if @item
				item_template
			else
				collection_template { yield_items }
			end
		end

		private

		def yield_items
			if @item
				raise ArgumentError, "You can only yield_items when rendering a collection. You are currently rendering an item."
			end

			@collection.each_with_index do |item, index|
				@item = item
				@index = index
				@position = (index + 1)
				@first = (index == 0)
				@last = (@position == @collection.size)

				item_template
			end

			@item = nil
			@index = nil
			@first = nil
			@last = nil
			@position = nil
		end

		def first?
			raise ArgumentError unless @item

			@first
		end

		def last?
			raise ArgumentError unless @item

			@last
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phlex-0.4.0 lib/phlex/collection.rb