-
-
Save tenderlove/97637 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 = assigns | |
assigns.each do |key, value| | |
instance_variable_set(:"@#{key}", value) | |
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, filename = __FILE__, lineno = __LINE__) | |
xml = Nokogiri::XML::Builder.new | |
eval(str, binding, filename, lineno) | |
xml.children #we return the doc's children (to avoid the instruct) | |
end | |
def method_missing thing | |
super unless @assigns.key? thing | |
@assigns[key] | |
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 | |
@v.evaluate(<<-STR, __FILE__, __LINE__ + 1) | |
xml.person(:sex => "male", :id => @id) do |x| | |
xml.name(@name) | |
xml.age(@age) | |
xml.city(@city) | |
xml.contacts do |contact| | |
@friends.each do |friend| | |
xml.friend(friend) | |
end | |
@family.each do |family| | |
xml.family(family) | |
end | |
end | |
end | |
STR | |
@v = nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment