Sha256: 1c331d48e9ef9563142b52360d9fb87d2a324a2de367cd19b7137e78f3e8cb2f

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# code: 
# * George Moschovitis  <gm@navel.gr>
#
# (c) 2004 Navel, all rights reserved.
# $Id: scaffold.rb 175 2004-11-26 16:11:27Z gmosx $

require 'glue/inflector'

module N

module Scaffolding

	# Ruby is sometimes VERY surprising, the following trick is needed
	# to include singleton methods in other classes.
	#
	def self.append_features(base)
		super
		base.extend(SingletonMethods)
	end

	module SingletonMethods 
		# Enchant the caller with a number of default methods.
		# Override the automatically generated methods as needed.
		#
		def scaffold(klass, options = {})
			
			oid = options[:oid] || 'oid'
			name = options[:name] || N::Inflector.name(klass.name)
			list_name = options[:list_name] || N::Inflector.plural_name(name)
			suffix = "_#{name}"

			# Add methods to the scaffolded class.
			
			klass.module_eval <<-"end_eval", __FILE__, __LINE__
				
				def view_uri
					"view#{suffix}?oid=\#\{@oid\}"
				end
				
			end_eval
			
			# Add methods to the service.
			
			if options[:index]
				module_eval <<-"end_eval", __FILE__, __LINE__
					
					def index
						list#{suffix}
					end
					
				end_eval
			end
		
			module_eval <<-"end_eval", __FILE__, __LINE__
				
				# TODO: add pager support here!
				def list#{suffix}
					@#{list_name} = #{klass}.all('ORDER BY oid')
				end
				
				def view#{suffix}
					@#{name} = #{klass}[@params['#{oid}']]
				end
				
				def save#{suffix}
				end
				
				def del#{suffix}
					#{klass}.delete(@params['#{oid}'])
				end
				alias_method :delete#{suffix}, :del#{suffix}
				
			end_eval
			
		end
	end

end

end # module

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.8.0 lib/nitro/scaffold.rb