Skip to content

Commit

Permalink
Organizing plugin into features (essential, visualize, and teleport)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschementi committed May 11, 2008
1 parent c881ebd commit 9f7e3e6
Show file tree
Hide file tree
Showing 25 changed files with 446 additions and 301 deletions.
26 changes: 15 additions & 11 deletions app/views/posts/_posts.rb
Expand Up @@ -4,26 +4,30 @@ class Posts < SilverlightApplication
def initialize
super
result = download "/posts.json"
@posts = JSONParser.new.parse(result)
@posts = result.empty? ? [] : JSONParser.new.parse(result)
render_posts :id => "posts"
end

def render_posts(options = {})
id, posts = options[:id], @posts
output = "<table>"
output += " <tr>"
posts.first.keys.each do |key|
output += " <th>#{key}</th>"
end
output += " </tr>"
posts.each do |post|
if @posts.empty?
output = "No posts! Add one below!"
else
output = "<table>"
output += " <tr>"
post.keys.each do |key|
output += " <td>#{post[key]}</td>"
posts.first.keys.each do |key|
output += " <th>#{key}</th>"
end
output += " </tr>"
posts.each do |post|
output += " <tr>"
post.keys.each do |key|
output += " <td>#{post[key]}</td>"
end
output += " </tr>"
end
output += "</table>"
end
output += "</table>"
document.send(id)[:innerHTML] = output
end
end
Expand Down
5 changes: 1 addition & 4 deletions vendor/plugins/silverline/init.rb
@@ -1,4 +1 @@
require 'silverline'
require 'generator'

Generator.register
require 'silverline'
99 changes: 0 additions & 99 deletions vendor/plugins/silverline/lib/controller/base.rb

This file was deleted.

File renamed without changes.
11 changes: 6 additions & 5 deletions vendor/plugins/silverline/lib/silverline.rb
@@ -1,7 +1,4 @@
require 'ruby_patch'

require 'controller/base'
require 'view/base'
require 'patch/ruby'

module Silverline
RAILS_VIEWS = "#{RAILS_ROOT}/app/views/"
Expand All @@ -16,4 +13,8 @@ module FileExtensions
XAML = "xaml"
RB = "rb"
end
end
end

require 'silverline/essential'
require 'silverline/visualize'
require 'silverline/teleport'
11 changes: 11 additions & 0 deletions vendor/plugins/silverline/lib/silverline/essential.rb
@@ -0,0 +1,11 @@
module Silverline::Essential
# Nothing on purpose =)
end

require 'silverline/essential/generator'
Silverline::Essential::Generator.register

require 'silverline/essential/html'
ActionView::Base.class_eval do
include Silverline::Essential::Html
end
@@ -1,21 +1,20 @@
# NOTE: requires filesystemwatcher to be installed
# http://www.jhorman.org/FileSystemWatcher/index.html
require "filesystemwatcher"
require 'xap'
require 'silverline/essential/xap'

# Generates the XAP on modification of watched files
module Generator
module Silverline::Essential::Generator
include Silverline

Xap = XAPChiron
Xap = Silverline::Essential::XAPChiron

# List of files/directories to watch for modification.
# Triggers generation of the Silverlight package (XAP)
def self.register
puts "** Initializing Silverlight"
watcher = FileSystemWatcher.new
watcher.addDirectory Silverline::CLIENT_ROOT
watcher.addDirectory Silverline::PLUGIN_CLIENT
watcher.addDirectory CLIENT_ROOT
watcher.addDirectory PLUGIN_CLIENT

# TODO: watch all client controllers, as well as all views
watcher.addFile "#{RAILS_ROOT}/app/controllers/client_controller.rb"
Expand Down
58 changes: 58 additions & 0 deletions vendor/plugins/silverline/lib/silverline/essential/html.rb
@@ -0,0 +1,58 @@
module Silverline::Essential::Html

def silverlight_include_tag(options)
templatify("head.html.erb", binding)
end

def silverlight_object(options = {})
require 'erb'
defaults = {
:start => "app",
:debug => true,
:reportErrors => "errorLocation",
:properties => {
:width => 1,
:height => 1,
:background => "#ffffffff",
:windowless => true
}
}
options = defaults.merge(options)
options[:start] << ".rb"
retval = ""
if options[:properties][:width].to_i < 2 || options[:properties][:height].to_i < 2
retval << %Q(
<style type='text/css'>
#SilverlightControlHost {
position: absolute;
}
</style>
)
end
retval << templatify("body.html.erb", binding)
end

private

def http_host
session.cgi.instance_variable_get(:"@request").params["HTTP_HOST"]
end

def generate_init_params(options)
options.delete(:properties)
p = options.collect { |k,v| "#{k.to_s}=#{v.to_s}" }.join(", ")
p << ", http_host=#{http_host}"
end

def public_xap_file
"/#{Silverline::XAP_FILE.split("/").last}"
end

def jsonify(o)
o.to_json.gsub("\"", "'").gsub(",", "==>")
end

def templatify(path, b)
ERB.new(File.open("#{Silverline::PLUGIN_ROOT}/templates/#{path}"){|f| f.read}).result(b)
end
end
86 changes: 86 additions & 0 deletions vendor/plugins/silverline/lib/silverline/essential/xap.rb
@@ -0,0 +1,86 @@
module Silverline::Essential

# Handles packaging the Silverlight application.
class XAP

# require rubyzip (gem install rubyzip | http://rubyzip.sourceforge.net/)
require 'zip/zipfilesystem'
require 'erb'

def initialize(file, directory)
@file = file
@directory = directory
@files = []
end

def generate
Zip::ZipFile.open @file, Zip::ZipFile::CREATE do |zip|
xap zip, @directory
zip.file.open "AppManifest.xaml", 'w' do |m|
m.puts manifest
@files << "AppManifest.xaml"
end
end
# Remove temp files left behind by rubyzip
# Bug: http://sourceforge.net/tracker/index.php?func=detail&aid=1702240&group_id=43107&atid=435170
@files.each do |file|
file = file.split("/").last
Dir["public/#{file}.*"].each do |f|
File.delete f
end
end
@files = []
end

private

def manifest
@assembly_path = "/public/ironruby"
# Note: Silverlight entry-point assembly must be the first in this list
# (Microsoft.Scripting.Silverlight in this case)
@assemblies = %w(Microsoft.Scripting.Silverlight Microsoft.Scripting IronRuby IronRuby.Libraries)
@entry_point_type = "Microsoft.Scripting.Silverlight.DynamicSilverlight"
file = File.open("#{PLUGIN_ROOT}/templates/AppManifest.xaml.erb", 'r'){|f| f.read }
xaml = ERB.new(file).
xaml.run(binding)
end

def xap(zip, dir)
xap_helper(zip, dir)
end

def xap_helper(zip, dir, root = dir)
Dir["#{dir}/*"].each do |client|
short_client = client.split(root).last
if File.directory? client
zip.dir.mkdir short_client
xap_helper zip, client, root
else
zip.file.open short_client, 'w' do |zf|
f = File.open client
zf.puts f.read
f.close
end
end
@files << short_client
end
end

end

# Handles packaging the Silverlight application with Chiron, a .NET console
# application written in C#. It handles generation of the AppManifest.xaml, as
# well as packaging the app and including language assemblies if needed.
# Note: because Chiron is a .NET application, it requires mono to run on
# Mac/Linux. If you don't want to install mono, use the pure-ruby XAP module.
class XAPChiron < XAP

def generate
cmd = "public/ironruby/Chiron.exe /s /d:#{@directory} /z:#{@file}"
# TODO: Should I do some platform detection rather than trial&error?
system "#{cmd}" unless system "mono #{cmd}"
end

end

end
14 changes: 14 additions & 0 deletions vendor/plugins/silverline/lib/silverline/teleport.rb
@@ -0,0 +1,14 @@
module Silverline::Teleport
# Nothing on purpose =)
end

require 'silverline/teleport/controller'
require 'silverline/teleport/view'
require 'silverline/teleport/html'
ActionController::Base.class_eval do
include Silverline::Teleport::Controller
end
ActionView::Base.class_eval do
include Silverline::Teleport::View
include Silverline::Teleport::Html
end

0 comments on commit 9f7e3e6

Please sign in to comment.