Sha256: 25bd01a2bec63067fbb64965ca3080e93e5a36a2227d2d2d3a91d5828b9a0f45

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Phlex
	module Table
		include Collection

		module ClassMethods
			attr_accessor :header

			def property(header = nil, **attributes, &body)
				if header.is_a?(String)
					header_text = header
					header = -> { head_header(scope: "col") { header_text } }
				end

				properties << {
					header: header,
					body: body,
					attributes: attributes,
				}
			end

			def properties
				@properties ||= []
			end
		end

		def self.included(child)
			child.extend ClassMethods

			child.alias_method :head, :thead
			child.alias_method :body, :tbody
			child.alias_method :foot, :tfoot

			child.alias_method :row, :tr

			child.alias_method :header, :th
			child.alias_method :cell, :td

			child.alias_method :head_row, :row
			child.alias_method :body_row, :row
			child.alias_method :foot_row, :row

			child.alias_method :head_header, :header
			child.alias_method :foot_header, :header

			child.alias_method :head_cell, :cell
			child.alias_method :body_cell, :cell
			child.alias_method :foot_cell, :cell
		end

		private

		def properties
			self.class.properties
		end

		def collection_template(&block)
			table do
				head_template
				body_template(&block)
				foot_template
			end
		end

		def item_template
			row_template
		end

		def head_template
			if self.class.properties.any? { |p| p[:header] }
				head do
					head_row do
						self.class.properties.each do |property|
							case property[:header]
							when Proc
								instance_exec(&property[:header])
							when Symbol
								send(property[:header])
							end
						end
					end
				end
			end
		end

		def body_template
			body { yield_items }
		end

		def foot_template
		end

		def row_template
			body_row do
				self.class.properties.each do |property|
					body_cell(**property[:attributes]) do
						instance_exec(@item, &property[:body])
					end
				end
			end
		end
	end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
phlex-0.5.3 lib/phlex/table.rb
phlex-0.5.2 lib/phlex/table.rb
phlex-0.5.1 lib/phlex/table.rb
phlex-0.5.0 lib/phlex/table.rb
phlex-0.4.0 lib/phlex/table.rb