Sha256: bdf8bd343f7e90528569e2af22c5d8a63c82e8b78aa88ec477c15d789469a2b0

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2004-2005 Navel, all rights reserved.
# $Id: scaffold.rb 300 2005-03-16 13:23:10Z gmosx $

require 'glue/inflector'

module N

module Scaffolding

	def self.append_features(base) # :nodoc: 
		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

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.13.0 lib/nitro/scaffold.rb
nitro-0.14.0 lib/nitro/scaffold.rb