Sha256: e2eed5a805ff7e08cfd1ce6b48d9d98574df00a25ae77aa137fdf5e33406c35a

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

module Nitro

# The TableBuilder is a helper class that automates the creation
# of tables from collections of objects. The resulting html
# can be styled using css.
#
# === Example
#
# <?r
# users = User.all.map { |u| [u.name, u.first_name, u.last_name, u.email] }
# header = ['Username', 'First name', 'Last name', 'Email']
# ?>
# 
# <div class="custom-table-class">
#   #{N::TableBuilder.build(users, header)}
# </div>
#
# or:
#
# <div class="custom-table-class">
#   #{@out.build_table(users, header)}
# </div>
#--
# TODO: sorting, thead/tbody/legend etc, verbose...
#++

module TableBuilderMixin

	# [+options+]
	#		A hash of options.
	# 
	# :id = id of the component.
	# :headers = an array of the header values
	# :values = an array of arrays.

	def build_table(options)
		c = options

		buf = '<table'
		buf << %| id="#{c[:id]}"| if c[:id]
		buf << '><tr>'

		for h in c[:headers]
			buf << %|<th>#{h}</th>|
		end

		buf << "</tr>"

		for row in c[:values]
			buf << "<tr>"
			
			for v in row
				buf << %|<td>#{v}</td>|
			end
			
			buf << "</tr>"
		end

		buf << "</table>"

		return buf
	end

end

# Abstract class for the TableBuilderMixin.

class TableBuilder
	include TableBuilderMixin

	class << self
		def build(options)
			TableBuilder.new.build_table(options)
		end
	end
end

end 

# * George Moschovitis <gm@navel.gr>

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nitro-0.18.0 lib/nitro/builder/table.rb
nitro-0.18.1 lib/nitro/builder/table.rb
nitro-0.19.0 lib/nitro/builder/table.rb
nitro-0.20.0 lib/nitro/builder/table.rb