smartgen_action_view 0.0.1 → 0.0.2
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/lib/smartgen_action_view.rb +6 -0
- data/lib/smartgen_action_view/action_view_processor.rb +46 -0
- data/lib/smartgen_action_view/engine.rb +20 -0
- data/lib/smartgen_action_view/pre_processor.rb +6 -26
- data/lib/smartgen_action_view/version.rb +1 -1
- data/spec/lib/engine_spec.rb +38 -0
- metadata +11 -7
    
        data/lib/smartgen_action_view.rb
    CHANGED
    
    | @@ -1,11 +1,17 @@ | |
| 1 | 
            +
            require 'tempfile'
         | 
| 2 | 
            +
             | 
| 1 3 | 
             
            require 'action_controller'
         | 
| 2 4 | 
             
            require 'action_view'
         | 
| 3 5 |  | 
| 4 6 | 
             
            require 'smartgen'
         | 
| 5 7 |  | 
| 6 8 | 
             
            require File.expand_path(File.join('smartgen_action_view/renderer'), File.dirname(__FILE__))
         | 
| 9 | 
            +
            require File.expand_path(File.join('smartgen_action_view/action_view_processor'), File.dirname(__FILE__))
         | 
| 10 | 
            +
            require File.expand_path(File.join('smartgen_action_view/engine'), File.dirname(__FILE__))
         | 
| 7 11 | 
             
            require File.expand_path(File.join('smartgen_action_view/pre_processor'), File.dirname(__FILE__))
         | 
| 8 12 |  | 
| 13 | 
            +
            Smartgen::MarkupFile.register(Smartgen::Engine::ActionView)
         | 
| 14 | 
            +
             | 
| 9 15 | 
             
            Smartgen::Engine::Textile.register(Smartgen::Engine::ActionViewPreProcessor.new)
         | 
| 10 16 | 
             
            Smartgen::Engine::Markdown.register(Smartgen::Engine::ActionViewPreProcessor.new)
         | 
| 11 17 | 
             
            Smartgen::Engine::ERB.register(Smartgen::Engine::ActionViewPreProcessor.new)
         | 
| @@ -0,0 +1,46 @@ | |
| 1 | 
            +
            module Smartgen
         | 
| 2 | 
            +
              module Engine
         | 
| 3 | 
            +
                module ActionViewProcessor
         | 
| 4 | 
            +
                  attr_accessor :tempfile
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  module DefaultHelpers
         | 
| 7 | 
            +
                    attr_accessor :metadata
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  def process_template(body, metadata)
         | 
| 11 | 
            +
                    create_tempfile_with(body)
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                    view = ::ActionView::Base.new File.dirname(tempfile.path)
         | 
| 14 | 
            +
                    view.extend(DefaultHelpers)
         | 
| 15 | 
            +
                    view.metadata = metadata
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                    result = view.render :file => tempfile.path
         | 
| 18 | 
            +
                    setup_content_for(view, metadata)
         | 
| 19 | 
            +
                    result
         | 
| 20 | 
            +
                  ensure
         | 
| 21 | 
            +
                    tempfile.close! if tempfile
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                private
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                  def create_tempfile_with(body)
         | 
| 27 | 
            +
                    self.tempfile = Tempfile.new 'smartgen'
         | 
| 28 | 
            +
                    tempfile.write body
         | 
| 29 | 
            +
                    tempfile.rewind
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  def setup_content_for(view, metadata)
         | 
| 33 | 
            +
                    metadata.content_for = Smartgen::ObjectHash.new unless metadata.has_key?(:content_for)
         | 
| 34 | 
            +
                    metadata.content_for.merge!(process_content_for(view, metadata))
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                  def process_content_for(view, metadata)
         | 
| 38 | 
            +
                    view.instance_variable_get('@_content_for').inject({}) do |result, item|
         | 
| 39 | 
            +
                      name, contents = item
         | 
| 40 | 
            +
                      result[name] = process_by_engine(contents, metadata)
         | 
| 41 | 
            +
                      result
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
              end
         | 
| 46 | 
            +
            end
         | 
| @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            module Smartgen
         | 
| 2 | 
            +
              module Engine
         | 
| 3 | 
            +
                class ActionView < Base
         | 
| 4 | 
            +
                  include ActionViewProcessor
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  protected
         | 
| 7 | 
            +
                    def parse(body, metadata)
         | 
| 8 | 
            +
                      process_template(body, metadata)
         | 
| 9 | 
            +
                    end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    def extensions
         | 
| 12 | 
            +
                      @extensions ||= ['.erb']
         | 
| 13 | 
            +
                    end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                    def process_by_engine(contents, metadata)
         | 
| 16 | 
            +
                      process_without_pre_processors(contents, metadata).html_safe
         | 
| 17 | 
            +
                    end
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| @@ -3,38 +3,18 @@ require 'tempfile' | |
| 3 3 | 
             
            module Smartgen
         | 
| 4 4 | 
             
              module Engine
         | 
| 5 5 | 
             
                class ActionViewPreProcessor
         | 
| 6 | 
            +
                  include ActionViewProcessor
         | 
| 7 | 
            +
             | 
| 6 8 | 
             
                  attr_accessor :engine
         | 
| 7 | 
            -
                  attr_accessor :tempfile
         | 
| 8 9 |  | 
| 9 10 | 
             
                  def process(body, metadata)
         | 
| 10 | 
            -
                     | 
| 11 | 
            -
                    view = ::ActionView::Base.new File.dirname(tempfile.path)
         | 
| 12 | 
            -
                    result = view.render :file => tempfile.path
         | 
| 13 | 
            -
                    setup_content_for(view, metadata)
         | 
| 14 | 
            -
                    result
         | 
| 15 | 
            -
                  ensure
         | 
| 16 | 
            -
                    tempfile.close! if tempfile
         | 
| 11 | 
            +
                    process_template(body, metadata)
         | 
| 17 12 | 
             
                  end
         | 
| 18 13 |  | 
| 19 | 
            -
                   | 
| 20 | 
            -
             | 
| 21 | 
            -
                    def create_tempfile_with(body)
         | 
| 22 | 
            -
                      self.tempfile = Tempfile.new 'smartgen'
         | 
| 23 | 
            -
                      tempfile.write body
         | 
| 24 | 
            -
                      tempfile.rewind
         | 
| 25 | 
            -
                    end
         | 
| 26 | 
            -
             | 
| 27 | 
            -
                    def setup_content_for(view, metadata)
         | 
| 28 | 
            -
                      metadata.content_for = Smartgen::ObjectHash.new unless metadata.has_key?(:content_for)
         | 
| 29 | 
            -
                      metadata.content_for.merge!(process_content_for(view, metadata))
         | 
| 30 | 
            -
                    end
         | 
| 14 | 
            +
                  protected
         | 
| 31 15 |  | 
| 32 | 
            -
                    def  | 
| 33 | 
            -
                       | 
| 34 | 
            -
                        name, contents = item
         | 
| 35 | 
            -
                        result[name] = engine.process_without_pre_processors(contents, metadata).html_safe
         | 
| 36 | 
            -
                        result
         | 
| 37 | 
            -
                      end
         | 
| 16 | 
            +
                    def process_by_engine(contents, metadata)
         | 
| 17 | 
            +
                      engine.process_without_pre_processors(contents, metadata).html_safe
         | 
| 38 18 | 
             
                    end
         | 
| 39 19 | 
             
                end
         | 
| 40 20 | 
             
              end
         | 
| @@ -0,0 +1,38 @@ | |
| 1 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 2 | 
            +
            require 'spec_helper'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            describe Smartgen::Engine::ActionView do
         | 
| 5 | 
            +
              let :body do
         | 
| 6 | 
            +
                "2 + 2 = <%= 2 * 2 %>"
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
              
         | 
| 9 | 
            +
              let :contents do
         | 
| 10 | 
            +
                "2 + 2 = 4"
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
              
         | 
| 13 | 
            +
              it "should process body using ERB" do
         | 
| 14 | 
            +
                subject.process(body).should == contents
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
              
         | 
| 17 | 
            +
              it "should support .erb extension" do
         | 
| 18 | 
            +
                should be_supported('.erb')
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
              
         | 
| 21 | 
            +
              context "with metadata" do
         | 
| 22 | 
            +
                let :body do
         | 
| 23 | 
            +
                  "Some metadata: <%= metadata.name %>"
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
                
         | 
| 26 | 
            +
                let :contents do
         | 
| 27 | 
            +
                  "Some metadata: #{metadata.name}"
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
                
         | 
| 30 | 
            +
                let :metadata do
         | 
| 31 | 
            +
                  Smartgen::ObjectHash.new :name => 'Vicente'
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
                
         | 
| 34 | 
            +
                it "should process body using ERB" do
         | 
| 35 | 
            +
                  subject.process(body, metadata).should == contents
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: smartgen_action_view
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.2
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -14,7 +14,7 @@ default_executable: | |
| 14 14 | 
             
            dependencies:
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 16 16 | 
             
              name: smartgen
         | 
| 17 | 
            -
              requirement: & | 
| 17 | 
            +
              requirement: &2161521700 !ruby/object:Gem::Requirement
         | 
| 18 18 | 
             
                none: false
         | 
| 19 19 | 
             
                requirements:
         | 
| 20 20 | 
             
                - - ~>
         | 
| @@ -22,10 +22,10 @@ dependencies: | |
| 22 22 | 
             
                    version: 0.6.0.beta
         | 
| 23 23 | 
             
              type: :runtime
         | 
| 24 24 | 
             
              prerelease: false
         | 
| 25 | 
            -
              version_requirements: * | 
| 25 | 
            +
              version_requirements: *2161521700
         | 
| 26 26 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 27 27 | 
             
              name: actionpack
         | 
| 28 | 
            -
              requirement: & | 
| 28 | 
            +
              requirement: &2161521200 !ruby/object:Gem::Requirement
         | 
| 29 29 | 
             
                none: false
         | 
| 30 30 | 
             
                requirements:
         | 
| 31 31 | 
             
                - - ! '>='
         | 
| @@ -33,10 +33,10 @@ dependencies: | |
| 33 33 | 
             
                    version: 3.0.4
         | 
| 34 34 | 
             
              type: :runtime
         | 
| 35 35 | 
             
              prerelease: false
         | 
| 36 | 
            -
              version_requirements: * | 
| 36 | 
            +
              version_requirements: *2161521200
         | 
| 37 37 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 38 38 | 
             
              name: rspec
         | 
| 39 | 
            -
              requirement: & | 
| 39 | 
            +
              requirement: &2161520740 !ruby/object:Gem::Requirement
         | 
| 40 40 | 
             
                none: false
         | 
| 41 41 | 
             
                requirements:
         | 
| 42 42 | 
             
                - - ! '>='
         | 
| @@ -44,7 +44,7 @@ dependencies: | |
| 44 44 | 
             
                    version: 2.3.0
         | 
| 45 45 | 
             
              type: :development
         | 
| 46 46 | 
             
              prerelease: false
         | 
| 47 | 
            -
              version_requirements: * | 
| 47 | 
            +
              version_requirements: *2161520740
         | 
| 48 48 | 
             
            description: This gem gives you all the power of ActionView when rendering files in
         | 
| 49 49 | 
             
              smartgen, such as using partials, helpers, etc.
         | 
| 50 50 | 
             
            email:
         | 
| @@ -61,6 +61,8 @@ files: | |
| 61 61 | 
             
            - README.md
         | 
| 62 62 | 
             
            - Rakefile
         | 
| 63 63 | 
             
            - lib/smartgen_action_view.rb
         | 
| 64 | 
            +
            - lib/smartgen_action_view/action_view_processor.rb
         | 
| 65 | 
            +
            - lib/smartgen_action_view/engine.rb
         | 
| 64 66 | 
             
            - lib/smartgen_action_view/pre_processor.rb
         | 
| 65 67 | 
             
            - lib/smartgen_action_view/renderer.rb
         | 
| 66 68 | 
             
            - lib/smartgen_action_view/version.rb
         | 
| @@ -78,6 +80,7 @@ files: | |
| 78 80 | 
             
            - spec/fixtures/layout_with_markup_file.html.erb
         | 
| 79 81 | 
             
            - spec/fixtures/layout_with_metadata.html.erb
         | 
| 80 82 | 
             
            - spec/fixtures/layout_with_partial.html.erb
         | 
| 83 | 
            +
            - spec/lib/engine_spec.rb
         | 
| 81 84 | 
             
            - spec/lib/pre_processor_spec.rb
         | 
| 82 85 | 
             
            - spec/lib/renderer_spec.rb
         | 
| 83 86 | 
             
            - spec/spec_helper.rb
         | 
| @@ -120,6 +123,7 @@ test_files: | |
| 120 123 | 
             
            - spec/fixtures/layout_with_markup_file.html.erb
         | 
| 121 124 | 
             
            - spec/fixtures/layout_with_metadata.html.erb
         | 
| 122 125 | 
             
            - spec/fixtures/layout_with_partial.html.erb
         | 
| 126 | 
            +
            - spec/lib/engine_spec.rb
         | 
| 123 127 | 
             
            - spec/lib/pre_processor_spec.rb
         | 
| 124 128 | 
             
            - spec/lib/renderer_spec.rb
         | 
| 125 129 | 
             
            - spec/spec_helper.rb
         |