Created
April 18, 2009 05:18
-
-
Save julien51/97446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "nokogiri" | |
require 'dike' | |
Dike.logfactory '/tmp/dikelogfactory/' | |
class ZipZap | |
attr_reader :zip, :zap | |
def initialize | |
@zip = "zip" | |
@zap = "zap" | |
end | |
def switch | |
@zap, @zip = @zip, @zap | |
end | |
end | |
## | |
# Your application's views (stanzas) should be descendant of this class. | |
class View | |
attr_reader :output, :view_template | |
## | |
# Instantiate a new view with the various varibales passed in assigns and the path of the template to render. | |
def initialize(path, assigns) | |
@output = "" | |
@view_template = path | |
assigns.each do |key, value| | |
instance_variable_set("@#{key}", value) | |
self.class.send(:define_method, key) do # Defining accessors | |
value | |
end | |
end | |
@z = ZipZap.new | |
end | |
## | |
# "Loads" the view file, and uses the Nokogiri Builder to build the XML stanzas that will be sent. | |
def evaluate(str) | |
builder = Nokogiri::XML::Builder.new do |xml| | |
eval(str) | |
end | |
builder.children #we return the doc's children (to avoid the instruct) | |
end | |
end | |
10.times do | |
Dike.finger | |
@v = View.new("/path/to/my/view", {:id => rand(100), :name => "julien", :age => 26, :city => "San Francisco", :friends => ["simon", "romain", "pierric"], :family => ["nicolas", "antoine", "emma"]}) | |
@z = ZipZap.new | |
str = <<-STR | |
xml.person(:sex => "male", :id => @id) do | |
xml.name(@name) | |
xml.age(@age) | |
xml.city(@city) | |
xml.contacts do | |
@friends.each do |friend| | |
xml.friend(friend) | |
end | |
@family.each do |family| | |
xml.family(family) | |
end | |
end | |
end | |
STR | |
@v.evaluate(str) | |
@v = nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment