A
download DynamicElements.rb
Language: Ruby
LOC: 119
Project Info
IOWA
Server: RubyForge
Type: cvs
...Forge\i\iowa\iowa\iowa\src\
   Application.rb
   ApplicationStats.rb
   Association.rb
   BindingsParser.rb
   Client.rb
   Component.rb
   ComponentProxy.rb
   config.rb
   Context.rb
   DbPool.rb
   DynamicElements.rb
   Element.rb
   Form.rb
   HTTPServer.rb
   ISAAC.rb
   KeyValueCoding.rb
   LRUCache.rb
   PageStore.rb
   read_multipart.rb
   Request.rb
   SciTE.properties
   Session.rb
   SessionStats.rb
   SessionStore.rb
   Tag.rb
   TemplateParser.rb
   WEBrickServlet.rb

module Iowa

# Subclass of Tag that defines an Iowa anchor element.

	class A < Tag
	
		# The default binding for an anchor is to establish an action
		# with a literal association.  i.e. oid="frumption" binds a
		# call to "frumption" as the action for the element.
	
		def defaultBindings
			@bindings["action"] = LiteralAssociation.new(@name)
		end
		
		def handleRequest(context)
			if(context.actionID == context.elementID)
				context.setAction(@bindings["action"])
				context.element_attributes = @attributes.dup
			end
			
			handleChildren(:handleRequest, context)
		end
		
		def handleResponse(context)
			context.response << openTag("href", context.actionURL)
			handleChildren(:handleResponse, context)
			context.response << closeTag
		end
	end
	
	# Subclass of Tag that defines an Iowa string element.
	# Note that Strings are usually referred to in a template
	# via the shortcut syntax of '@foo'.
	
	class String < Tag
	
		def defaultBindings
			@bindings["value"] = PathAssociation.new(@name)
		end
		
		def handleResponse(context)
			val = context.getBinding(@bindings["value"])
			if(val)
				context.response << "#{val}"
			end
		end
	end
	
	# Subclass of Tag that implements an Iowa element for repeating
	# portions of a template.
	
	class Repeat < Tag
	
		def defaultBindings
			@bindings["list"] = PathAssociation.new(@name + "Array")
			@bindings["item"] = PathAssociation.new(@name)
			@bindings["key"] = nil
		end
		
		def handleRequestOrResponse(method, context)
			list = context.getBinding(@bindings["list"])
			key = @bindings["key"]
			if list
				context.pushElement
				list.each do |item|
					context.setBindingNow(@bindings["item"], item)
					if(key)
						context.elementID = context.getBinding(key)
					else
						context.nextElement!
					end
					handleChildren(method, context)
				end
				context.popElement
			end
		end
	end
	
	# Subclass of Repeat.  Syntactic sugar.
	
	class Table < Repeat	
		def handleResponse(context)
			context.response << openTag
			super(context)
			context.response << closeTag
		end
	end
	
	# Subclass of Repeat.  Syntactic sugar.
	
	class UL < Repeat	
		def handleResponse(context)
			context.response << openTag
			super(context)
			context.response << closeTag
		end
	end

	# Subclass of Repeat.  Syntactic sugar.
	
	class OL < Repeat	
		def handleResponse(context)
			context.response << openTag
			super(context)
			context.response << closeTag
		end
	end

	# Subclass of Repeat.  Syntactic sugar.
	
	class TR < Repeat		
		def handleResponse(context)
			context.response << openTag
			super(context)
			context.response << closeTag
		end
	end
	
	# Subclass of Tag that allows for optional sections within a template.
	# Evaluation is simple.  If the condition (the item referenced by the
	# oid attribute in the tag) evaulates to true (Ruby's notion of true)
	# then the body of the <IF> tag will be displayed.  Otherwise it will
	# not be displayed.
	
	class If < Tag
	
		def defaultBindings
			@bindings["condition"] = PathAssociation.new(@name)
		end
		
		def testCondition(condition)
			condition
		end
	
		def handleRequestOrResponse(method, context)
			condition = context.getBinding(@bindings["condition"])
			context.element_attributes = @attributes
			if testCondition(condition)
				handleChildren(method, context)
			end
		end
	end
	
	# Subclass of If that implements inverse functionality.  If the
	# condition evaluates to false, then the body of the <UNLESS> tag
	# will be displayed.
	
	class Unless < If
		def testCondition(condition)
			!condition
		end
	end
	
	class Span < If
	end
	
	class TextElement < Element
	
		def initialize(name)
			super(name, {}, {})
		end
		
		def handleResponse(context)
			context.response << @name
		end
	end
	
	class BodyContent < Element
	
		def handleRequestOrResponse(method, context)
			root = context.popRoot
			root.handleChildren(method, context)
			context.pushRoot root
		end
	end

end

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us