stache 0.0.3 → 0.1.0
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/README.md +27 -1
 - data/lib/stache/asset_helper.rb +22 -3
 - data/lib/stache/handler.rb +2 -1
 - data/lib/stache/railtie.rb +11 -0
 - data/lib/stache/version.rb +1 -1
 - data/lib/stache/view.rb +3 -2
 - data/lib/stache.rb +4 -0
 - data/spec/stache/asset_helper_spec.rb +20 -1
 - data/spec/stache/config_spec.rb +3 -0
 - metadata +2 -1
 
    
        data/README.md
    CHANGED
    
    | 
         @@ -28,7 +28,33 @@ Needless to say, it's probably better if your custom View objects are subclasses 
     | 
|
| 
       28 
28 
     | 
    
         | 
| 
       29 
29 
     | 
    
         
             
            An example by way of explanation:
         
     | 
| 
       30 
30 
     | 
    
         | 
| 
       31 
     | 
    
         
            -
            With a template `app/templates/profiles/index`, Stache will look for a view named `Profiles::Index`, and, if not found, will just use the base `Stache::View`.
         
     | 
| 
      
 31 
     | 
    
         
            +
            With a template `app/templates/profiles/index`, Stache will look for a view named `Profiles::Index`, and, if not found, will just use the base `Stache::View`. Stache adds `app/views` to Rails' autoload paths, so here's a sample directory structure and some sample files:
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
            ```
         
     | 
| 
      
 34 
     | 
    
         
            +
            app/
         
     | 
| 
      
 35 
     | 
    
         
            +
              templates/
         
     | 
| 
      
 36 
     | 
    
         
            +
                profiles/
         
     | 
| 
      
 37 
     | 
    
         
            +
                  index.html.mustache
         
     | 
| 
      
 38 
     | 
    
         
            +
              views/
         
     | 
| 
      
 39 
     | 
    
         
            +
                profiles/
         
     | 
| 
      
 40 
     | 
    
         
            +
                  index.rb
         
     | 
| 
      
 41 
     | 
    
         
            +
            ```
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 44 
     | 
    
         
            +
            # in profiles/index.rb
         
     | 
| 
      
 45 
     | 
    
         
            +
            module Profiles
         
     | 
| 
      
 46 
     | 
    
         
            +
              class Index < ::Stache::View
         
     | 
| 
      
 47 
     | 
    
         
            +
                def my_view_helper_method
         
     | 
| 
      
 48 
     | 
    
         
            +
                  "whoo"
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
              end
         
     | 
| 
      
 51 
     | 
    
         
            +
            end
         
     | 
| 
      
 52 
     | 
    
         
            +
            ```
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
            ```html
         
     | 
| 
      
 55 
     | 
    
         
            +
            <!-- in the view, then -->
         
     | 
| 
      
 56 
     | 
    
         
            +
            <p>Here's a helper_method call: {{ my_view_helper_method }}</p>
         
     | 
| 
      
 57 
     | 
    
         
            +
            ```
         
     | 
| 
       32 
58 
     | 
    
         | 
| 
       33 
59 
     | 
    
         
             
            ## Of Note
         
     | 
| 
       34 
60 
     | 
    
         | 
    
        data/lib/stache/asset_helper.rb
    CHANGED
    
    | 
         @@ -7,10 +7,29 @@ module Stache 
     | 
|
| 
       7 
7 
     | 
    
         
             
                    exploded = source.split("/")
         
     | 
| 
       8 
8 
     | 
    
         
             
                    file = exploded.pop
         
     | 
| 
       9 
9 
     | 
    
         
             
                    file = file.split(".").first
         
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
                     
     | 
| 
       12 
     | 
    
         
            -
                     
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                    base_path = Stache.template_base_path.join(*exploded)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    template_path = locate_template_for(base_path, file)
         
     | 
| 
      
 13 
     | 
    
         
            +
                    if template_path
         
     | 
| 
      
 14 
     | 
    
         
            +
                      template = ::File.open(template_path, "rb")
         
     | 
| 
      
 15 
     | 
    
         
            +
                      content_tag(:script, template.read.html_safe, :type => "text/html", :id => "#{file.dasherize.underscore}_template")
         
     | 
| 
      
 16 
     | 
    
         
            +
                    else
         
     | 
| 
      
 17 
     | 
    
         
            +
                      raise ActionView::MissingTemplate.new(potential_paths(base_path, file), file, [base_path], false, { :handlers => [:mustache] })
         
     | 
| 
      
 18 
     | 
    
         
            +
                    end
         
     | 
| 
       13 
19 
     | 
    
         
             
                  end.join("\n").html_safe
         
     | 
| 
       14 
20 
     | 
    
         
             
                end
         
     | 
| 
      
 21 
     | 
    
         
            +
                
         
     | 
| 
      
 22 
     | 
    
         
            +
                def potential_paths(path, candidate_file_name)
         
     | 
| 
      
 23 
     | 
    
         
            +
                  [
         
     | 
| 
      
 24 
     | 
    
         
            +
                    path.join("_#{candidate_file_name}.html.mustache"),
         
     | 
| 
      
 25 
     | 
    
         
            +
                    path.join("_#{candidate_file_name}.mustache"),
         
     | 
| 
      
 26 
     | 
    
         
            +
                    path.join("#{candidate_file_name}.html.mustache"),
         
     | 
| 
      
 27 
     | 
    
         
            +
                    path.join("#{candidate_file_name}.mustache")
         
     | 
| 
      
 28 
     | 
    
         
            +
                  ]
         
     | 
| 
      
 29 
     | 
    
         
            +
                end
         
     | 
| 
      
 30 
     | 
    
         
            +
                
         
     | 
| 
      
 31 
     | 
    
         
            +
                def locate_template_for(path, candidate_file_name)
         
     | 
| 
      
 32 
     | 
    
         
            +
                  potential_paths(path, candidate_file_name).find { |file| File.file?(file.to_s) }
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
       15 
34 
     | 
    
         
             
              end
         
     | 
| 
       16 
35 
     | 
    
         
             
            end
         
     | 
    
        data/lib/stache/handler.rb
    CHANGED
    
    | 
         @@ -20,6 +20,7 @@ module Stache 
     | 
|
| 
       20 
20 
     | 
    
         
             
                    mustache = ::#{mustache_class}.new
         
     | 
| 
       21 
21 
     | 
    
         
             
                    mustache.view = self
         
     | 
| 
       22 
22 
     | 
    
         
             
                    mustache.template = '#{template.source.gsub(/'/, "\\\\'")}'
         
     | 
| 
      
 23 
     | 
    
         
            +
                    mustache.virtual_path = '#{template.virtual_path.to_s}'
         
     | 
| 
       23 
24 
     | 
    
         
             
                    mustache[:yield] = content_for(:layout)
         
     | 
| 
       24 
25 
     | 
    
         
             
                    mustache.context.update(local_assigns)
         
     | 
| 
       25 
26 
     | 
    
         
             
                    variables = controller.instance_variable_names
         
     | 
| 
         @@ -53,7 +54,7 @@ module Stache 
     | 
|
| 
       53 
54 
     | 
    
         
             
                  const_name = ActiveSupport::Inflector.camelize(template.virtual_path.to_s)
         
     | 
| 
       54 
55 
     | 
    
         
             
                  begin
         
     | 
| 
       55 
56 
     | 
    
         
             
                    const_name.constantize
         
     | 
| 
       56 
     | 
    
         
            -
                  rescue NameError
         
     | 
| 
      
 57 
     | 
    
         
            +
                  rescue NameError, LoadError
         
     | 
| 
       57 
58 
     | 
    
         
             
                    Stache::View
         
     | 
| 
       58 
59 
     | 
    
         
             
                  end
         
     | 
| 
       59 
60 
     | 
    
         
             
                end
         
     | 
| 
         @@ -0,0 +1,11 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module Stache
         
     | 
| 
      
 2 
     | 
    
         
            +
              class Railtie < ::Rails::Railtie
         
     | 
| 
      
 3 
     | 
    
         
            +
                initializer 'stache.autoload', :before => :set_autoload_paths do |app|
         
     | 
| 
      
 4 
     | 
    
         
            +
                  app.config.autoload_paths << Rails.root + 'app/views'
         
     | 
| 
      
 5 
     | 
    
         
            +
                end
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                config.to_prepare do
         
     | 
| 
      
 8 
     | 
    
         
            +
                  ApplicationController.send(:append_view_path, 'app/templates')
         
     | 
| 
      
 9 
     | 
    
         
            +
                end
         
     | 
| 
      
 10 
     | 
    
         
            +
              end
         
     | 
| 
      
 11 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/stache/version.rb
    CHANGED
    
    
    
        data/lib/stache/view.rb
    CHANGED
    
    | 
         @@ -6,7 +6,7 @@ module Stache 
     | 
|
| 
       6 
6 
     | 
    
         
             
              #
         
     | 
| 
       7 
7 
     | 
    
         
             
              # e.g. if the handler is loading a template from templates/
         
     | 
| 
       8 
8 
     | 
    
         
             
              class View < ::Mustache
         
     | 
| 
       9 
     | 
    
         
            -
                attr_accessor :view, :template
         
     | 
| 
      
 9 
     | 
    
         
            +
                attr_accessor :view, :template, :virtual_path
         
     | 
| 
       10 
10 
     | 
    
         | 
| 
       11 
11 
     | 
    
         
             
                def method_missing(method, *args, &block)
         
     | 
| 
       12 
12 
     | 
    
         
             
                  view.send(method, *args, &block)
         
     | 
| 
         @@ -23,8 +23,9 @@ module Stache 
     | 
|
| 
       23 
23 
     | 
    
         
             
                #
         
     | 
| 
       24 
24 
     | 
    
         
             
                def partial(name)
         
     | 
| 
       25 
25 
     | 
    
         
             
                  partial_name = "_#{name}.#{Stache.template_extension}"
         
     | 
| 
       26 
     | 
    
         
            -
                  template_dir =  
     | 
| 
      
 26 
     | 
    
         
            +
                  template_dir = self.virtual_path.split("/")[0..-2].join("/")
         
     | 
| 
       27 
27 
     | 
    
         
             
                  partial_path = File.expand_path(File.join(Stache.template_base_path, template_dir, partial_name))
         
     | 
| 
      
 28 
     | 
    
         
            +
                  # ::Rails.logger.info "Checking for #{partial_path} in template_dir: #{template_dir}"
         
     | 
| 
       28 
29 
     | 
    
         
             
                  unless File.file?(partial_path)
         
     | 
| 
       29 
30 
     | 
    
         
             
                    partial_path = "#{Stache.shared_path}/#{partial_name}"
         
     | 
| 
       30 
31 
     | 
    
         
             
                  end
         
     | 
    
        data/lib/stache.rb
    CHANGED
    
    
| 
         @@ -10,8 +10,9 @@ describe Stache::AssetHelper do 
     | 
|
| 
       10 
10 
     | 
    
         
             
                @helper ||= MyViewContext.new
         
     | 
| 
       11 
11 
     | 
    
         
             
              end
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
     | 
    
         
            -
              describe "template_include_tag" do
         
     | 
| 
      
 13 
     | 
    
         
            +
              describe "#template_include_tag" do
         
     | 
| 
       14 
14 
     | 
    
         
             
                it "renders a script tag with the template contents" do
         
     | 
| 
      
 15 
     | 
    
         
            +
                  File.stub!(:file?).with(Rails.root.join("app/views/widgets/_oh_herro.html.mustache").to_s).and_return(true)
         
     | 
| 
       15 
16 
     | 
    
         
             
                  File.stub!(:open).with(Rails.root.join("app/views/widgets/_oh_herro.html.mustache"), "rb").
         
     | 
| 
       16 
17 
     | 
    
         
             
                    and_return(StringIO.new("{{ awyeah }}"))
         
     | 
| 
       17 
18 
     | 
    
         | 
| 
         @@ -21,10 +22,28 @@ describe Stache::AssetHelper do 
     | 
|
| 
       21 
22 
     | 
    
         
             
                  Stache.configure do |c|
         
     | 
| 
       22 
23 
     | 
    
         
             
                    c.template_base_path = "/tmp/whee"
         
     | 
| 
       23 
24 
     | 
    
         
             
                  end
         
     | 
| 
      
 25 
     | 
    
         
            +
                  File.stub!(:file?).with("/tmp/whee/_whooo.html.mustache").and_return(true)
         
     | 
| 
       24 
26 
     | 
    
         
             
                  File.stub!(:open).with(Pathname.new("/tmp/whee/_whooo.html.mustache"), "rb").
         
     | 
| 
       25 
27 
     | 
    
         
             
                    and_return(StringIO.new("{{ awyeah }}"))
         
     | 
| 
       26 
28 
     | 
    
         | 
| 
       27 
29 
     | 
    
         
             
                  helper.template_include_tag("whooo").should == "<script id=\"whooo_template\" type=\"text/html\">{{ awyeah }}</script>"
         
     | 
| 
       28 
30 
     | 
    
         
             
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
                it "raises if it cannot find the template" do
         
     | 
| 
      
 32 
     | 
    
         
            +
                  -> { helper.template_include_tag("arrrgh") }.should raise_error(ActionView::MissingTemplate)
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
      
 34 
     | 
    
         
            +
              end
         
     | 
| 
      
 35 
     | 
    
         
            +
              
         
     | 
| 
      
 36 
     | 
    
         
            +
              describe "#locate_template_for" do
         
     | 
| 
      
 37 
     | 
    
         
            +
                it "tries permutations of partial names and file extensions to find the requested file" do
         
     | 
| 
      
 38 
     | 
    
         
            +
                  File.should_receive(:file?).with("/tmp/whee/_whooo.html.mustache")
         
     | 
| 
      
 39 
     | 
    
         
            +
                  File.should_receive(:file?).with("/tmp/whee/_whooo.mustache")
         
     | 
| 
      
 40 
     | 
    
         
            +
                  File.should_receive(:file?).with("/tmp/whee/whooo.html.mustache")
         
     | 
| 
      
 41 
     | 
    
         
            +
                  File.should_receive(:file?).with("/tmp/whee/whooo.mustache").and_return(true)
         
     | 
| 
      
 42 
     | 
    
         
            +
                  
         
     | 
| 
      
 43 
     | 
    
         
            +
                  helper.locate_template_for(Pathname.new("/tmp/whee"), "whooo").should == Pathname.new("/tmp/whee/whooo.mustache")
         
     | 
| 
      
 44 
     | 
    
         
            +
                end
         
     | 
| 
      
 45 
     | 
    
         
            +
                it "returns nil if it cannot find anything" do
         
     | 
| 
      
 46 
     | 
    
         
            +
                  helper.locate_template_for(Pathname.new("/tmp/whee"), "whooo").should be_nil
         
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
       29 
48 
     | 
    
         
             
              end
         
     | 
| 
       30 
49 
     | 
    
         
             
            end
         
     | 
    
        data/spec/stache/config_spec.rb
    CHANGED
    
    | 
         @@ -30,6 +30,9 @@ describe "Stache::Config" do 
     | 
|
| 
       30 
30 
     | 
    
         
             
                    config.template_base_path = "/dev/null"
         
     | 
| 
       31 
31 
     | 
    
         
             
                  end
         
     | 
| 
       32 
32 
     | 
    
         
             
                  Stache.template_base_path.should == Pathname.new("/dev/null")
         
     | 
| 
      
 33 
     | 
    
         
            +
                  Stache.configure do |config|
         
     | 
| 
      
 34 
     | 
    
         
            +
                    config.template_base_path = ::Rails.root.join('app', 'views')
         
     | 
| 
      
 35 
     | 
    
         
            +
                  end
         
     | 
| 
       33 
36 
     | 
    
         
             
                end
         
     | 
| 
       34 
37 
     | 
    
         
             
              end
         
     | 
| 
       35 
38 
     | 
    
         | 
    
        metadata
    CHANGED
    
    | 
         @@ -2,7 +2,7 @@ 
     | 
|
| 
       2 
2 
     | 
    
         
             
            name: stache
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version 
         
     | 
| 
       4 
4 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       5 
     | 
    
         
            -
              version: 0.0 
     | 
| 
      
 5 
     | 
    
         
            +
              version: 0.1.0
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors: 
         
     | 
| 
       8 
8 
     | 
    
         
             
            - Matt Wilson
         
     | 
| 
         @@ -122,6 +122,7 @@ files: 
     | 
|
| 
       122 
122 
     | 
    
         
             
            - lib/stache/asset_helper.rb
         
     | 
| 
       123 
123 
     | 
    
         
             
            - lib/stache/config.rb
         
     | 
| 
       124 
124 
     | 
    
         
             
            - lib/stache/handler.rb
         
     | 
| 
      
 125 
     | 
    
         
            +
            - lib/stache/railtie.rb
         
     | 
| 
       125 
126 
     | 
    
         
             
            - lib/stache/util.rb
         
     | 
| 
       126 
127 
     | 
    
         
             
            - lib/stache/version.rb
         
     | 
| 
       127 
128 
     | 
    
         
             
            - lib/stache/view.rb
         
     |