poirot 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in poirot.gemspec
4
+ gemspec
5
+ gem 'rails', '>=3.0.0'
6
+ gem 'mustache'
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/Readme.markdown ADDED
@@ -0,0 +1,58 @@
1
+ # Poirot
2
+
3
+ ## Description
4
+
5
+ Allows you to use [Mustache](http://mustache.github.com/) template partials in Rails, also
6
+ ads a helper method to easily allow JavaScript to re-use the same templates.
7
+
8
+ ## Usage
9
+
10
+ Create a partial just like you would with erb, prefixing the name with an underscore.
11
+
12
+ app/views/posts/_post_list.html.mustache
13
+
14
+ The template will have access to all normal rails helper methods and any instance variables
15
+ that were set in the controller. If you need more than this an optional view class can be
16
+ included, it should have the same name as the partial, but without the underscore.
17
+
18
+ app/views/posts/post_list_view.rb
19
+
20
+ module Posts
21
+ class PostListView < Poirot::View
22
+ def foo
23
+ "bar"
24
+ end
25
+
26
+ def post_link
27
+ post_path(post)
28
+ end
29
+ end
30
+ end
31
+
32
+ The view class has access to all the normal Rails helpers and access to the controller
33
+ instance variables, e.g @post becomes the method post.
34
+
35
+ Also included is a simple view helper for including mustache templates in a page ready for
36
+ use by JavaScript.
37
+
38
+ <%= template_include_tag 'post_list' %>
39
+
40
+ The above will insert a script tag with the contents of the partial called `post_list`, the
41
+ type will be set as `text/mustache` and the id will be `post-list-template`.
42
+
43
+ <script id="post-list-template" type="text/mustache">
44
+ <!-- template will be here! -->
45
+ </script>
46
+
47
+ ## Dependencies
48
+
49
+ * Rails >3.0.0
50
+ * Mustache
51
+
52
+ ## More
53
+
54
+ An [example](http://github.com/olivernn/notepad) app using Poirot
55
+
56
+ ## Credits
57
+
58
+ [Mark Evans](http://github.com/markevans) & [Oliver Nightingale](http://github.com/olivernn)
@@ -0,0 +1,11 @@
1
+ module Poirot
2
+ module AssetHelper
3
+ def template_include_tag(*sources)
4
+ sources.collect do |source|
5
+ template_path = Rails.root.join('app/views', controller_name, "_#{source}.html.mustache")
6
+ template = File.open(template_path, "rb")
7
+ content_tag :script, template.read.html_safe, :type => "text/mustache", :id => "#{source.dasherize}-template"
8
+ end.join("\n").html_safe
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ module Poirot
2
+ class Handler < ActionView::Template::Handler
3
+ include ActionView::Template::Handlers::Compilable
4
+
5
+ self.default_format = :mustache
6
+
7
+ def compile(template)
8
+ view_path = "#{template.virtual_path}_view"
9
+ abs_view_path = Rails.root.join('app/views', view_path)
10
+ view_class = begin
11
+ view_path.classify.constantize
12
+ rescue NameError => e
13
+ Poirot::View
14
+ end
15
+ "#{view_class}.new(self, '#{template.source.gsub(/'/, "\\\\'")}').render.html_safe"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Poirot
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ module Poirot
2
+ class View < Mustache
3
+
4
+ def initialize(view_context, template_source)
5
+ @view_context = view_context
6
+ self.template = template_source
7
+ assign_variables!
8
+ end
9
+
10
+ def respond_to?(method_sym, include_private = false)
11
+ if view_context.respond_to?(method_sym)
12
+ true
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ def method_missing(method_name, *args, &block)
19
+ instance_var = instance_variable_get("@#{method_name}")
20
+ if defined?(instance_var) && args.empty?
21
+ instance_var
22
+ else
23
+ view_context.send(method_name,*args, &block)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :view_context
30
+
31
+ def assign_variables!
32
+ variables = view_context.instance_variable_names.select{|name| name =~ /^@[^_]/}
33
+ variables.each do |name|
34
+ instance_var = view_context.instance_variable_get(name)
35
+ instance_variable_set(name, instance_var)
36
+ self[name.tr('@','').to_sym] = instance_var
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/poirot.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'poirot/handler'
2
+ require 'poirot/view'
3
+ require 'poirot/asset_helper'
4
+
5
+ ActionView::Template.register_template_handler(:mustache, Poirot::Handler)
6
+ ActionView::Base.send :include, Poirot::AssetHelper
7
+
8
+ module Poirot
9
+ class Railtie < Rails::Railtie
10
+
11
+ config.before_configuration do |app|
12
+ app.config.autoload_paths += %W(#{app.config.root}/app/views)
13
+ end
14
+
15
+ end
16
+ end
data/poirot.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "poirot/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "poirot"
7
+ s.version = Poirot::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Oliver Nightingale", "Mark Evans"]
10
+ s.email = ["oliver.nightingale1@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/poirot"
12
+ s.summary = %q{mustaches}
13
+ s.description = %q{mustaches are cool}
14
+
15
+ s.rubyforge_project = "poirot"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: poirot
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Oliver Nightingale
13
+ - Mark Evans
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-02 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: mustaches are cool
23
+ email:
24
+ - oliver.nightingale1@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - Readme.markdown
36
+ - lib/poirot.rb
37
+ - lib/poirot/asset_helper.rb
38
+ - lib/poirot/handler.rb
39
+ - lib/poirot/version.rb
40
+ - lib/poirot/view.rb
41
+ - poirot.gemspec
42
+ has_rdoc: true
43
+ homepage: http://rubygems.org/gems/poirot
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: poirot
68
+ rubygems_version: 1.3.6
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: mustaches
72
+ test_files: []
73
+