Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 2e2f4f16b53705226e81157966533ddc7d373e8f @olivernn committed Jan 27, 2011
Showing with 99 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +6 −0 Gemfile
  3. +2 −0 Rakefile
  4. +4 −0 lib/walrus.rb
  5. +20 −0 lib/walrus/handler.rb
  6. +3 −0 lib/walrus/version.rb
  7. +40 −0 lib/walrus/view.rb
  8. +21 −0 walrus.gemspec
@@ -0,0 +1,3 @@
+pkg/*
+*.gem
+.bundle
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in walrus.gemspec
+gemspec
+gem 'rails', '>=3.0.0'
+gem 'mustache'
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
@@ -0,0 +1,4 @@
+require 'walrus/handler'
+require 'walrus/view'
+
+ActionView::Template.register_template_handler(:mustache, Walrus::Handler)
@@ -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
@@ -0,0 +1,3 @@
+module Walrus
+ VERSION = "0.0.1"
+end
@@ -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
@@ -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.