Sha256: 337678e8554d59f4cdc1d14370167b8689b947aeff53d149f81c1da8a6f6d6d5

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id: scaffold.rb 270 2005-03-07 17:52:16Z 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)
			options[:nosuffix] ? suffix = nil : suffix = "_#{name}"

			# Add methods to the scaffolded class.
			
			klass.module_eval <<-"end_eval", __FILE__, __LINE__
				
				def view_uri
					# "view#{suffix}?oid=\#\{@oid\}"
					"view#{suffix}/\#\{@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}[@context['#{oid}']]
					@#{name} = #{klass}[@#{oid}]
				end
				action :view#{suffix}, :route => \%r\{view#{suffix}/(.*)\}, 'oid' => 1
				
				def save#{suffix}
				end
				
				def del#{suffix}
					#{klass}.delete(@context['#{oid}'])
				end
				alias_method :delete#{suffix}, :del#{suffix}
				
			end_eval
			
		end
	end

end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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