Sha256: 8c57cf88cdd5a661fc2572e3a0d58efd130743e1098b33e67bb6cd32df622888

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id: table.rb 266 2005-02-28 14:50:48Z gmosx $

module N

# 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 

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nitro-0.11.0 lib/nitro/builders/table.rb
nitro-0.12.0 lib/nitro/builders/table.rb
nitro-0.13.0 lib/nitro/builders/table.rb
nitro-0.14.0 lib/nitro/builders/table.rb
nitro-0.15.0 lib/nitro/builders/table.rb