Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
olivernn committed Jan 27, 2011
0 parents commit 2e2f4f1
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pkg/*
*.gem
.bundle
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "http://rubygems.org"

# Specify your gem's dependencies in walrus.gemspec
gemspec
gem 'rails', '>=3.0.0'
gem 'mustache'
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'bundler'
Bundler::GemHelper.install_tasks
4 changes: 4 additions & 0 deletions lib/walrus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'walrus/handler'
require 'walrus/view'

ActionView::Template.register_template_handler(:mustache, Walrus::Handler)
20 changes: 20 additions & 0 deletions lib/walrus/handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Walrus
class Handler < ActionView::Template::Handler
include ActionView::Template::Handlers::Compilable

self.default_format = :mustache

def compile(template)
view_path = "#{template.virtual_path}_view"
abs_view_path = Rails.root.join('app/views', view_path)
if File.exists? "#{abs_view_path}.rb"
require abs_view_path
view_class = view_path.sub('/_','/').classify.constantize
Rails.logger.debug(view_class)
else
view_class = Walrus::View
end
"#{view_class}.new(self, '#{template.source.gsub(/'/, "\\\\'")}').render.html_safe"
end
end
end
3 changes: 3 additions & 0 deletions lib/walrus/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Walrus
VERSION = "0.0.1"
end
40 changes: 40 additions & 0 deletions lib/walrus/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Walrus
class View < Mustache

def initialize(view_context, template_source)
@view_context = view_context
self.template = template_source
assign_variables!
end

def respond_to?(method_sym, include_private = false)
if view_context.respond_to?(method_sym)
true
else
super
end
end

def method_missing(method_name, *args, &block)
instance_var = instance_variable_get("@#{method_name}")
if defined?(instance_var) && args.empty?
instance_var
else
view_context.send(method_name,*args, &block)
end
end

private

attr_reader :view_context

def assign_variables!
variables = view_context.instance_variable_names.select{|name| name =~ /^@[^_]/}
variables.each do |name|
instance_var = view_context.instance_variable_get(name)
instance_variable_set(name, instance_var)
self[name.tr('@','').to_sym] = instance_var
end
end
end
end
21 changes: 21 additions & 0 deletions walrus.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "walrus/version"

Gem::Specification.new do |s|
s.name = "walrus"
s.version = Walrus::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Oliver Nightingale", "Mark Evans"]
s.email = ["oliver.nightingale1@gmail.com"]
s.homepage = "http://rubygems.org/gems/walrus"
s.summary = %q{mustaches}
s.description = %q{mustaches are cool}

s.rubyforge_project = "walrus"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
end

0 comments on commit 2e2f4f1

Please sign in to comment.