Sha256: 3a4ceb02ca7e9efb678ab711ec8a34cc8245d230c7d683d2e906ce8eeba874f7

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id: scaffold.rb 248 2005-01-31 13:38:34Z 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}[@context['#{oid}']]
				end
				
				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

4 entries across 4 versions & 1 rubygems

Version Path
nitro-0.10.0 lib/nitro/scaffold.rb
nitro-0.11.0 lib/nitro/scaffold.rb
nitro-0.9.5 lib/nitro/scaffold.rb
nitro-0.9.3 lib/nitro/scaffold.rb