Sha256: e8ca0ba34c03700d900631b6f879a0a2cead05fa15b39df815ec22a8584bedef

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 KB

Contents

module SnippetHelpers
  SOURCE_DIR = File.expand_path('../../source', __FILE__)

  class Snippet
    def initialize(name)
      @name = name
    end

    def path
      File.join(*path_segments)
    end

    private

    attr_reader :name
  end

  class HtmlSnippet < Snippet
    def path_segments
      [SOURCE_DIR, "_#{name.underscore}.html.erb"]
    end

    def language
      :markup
    end
  end

  class ScssSnippet < Snippet
    def path_segments
      [SOURCE_DIR, 'stylesheets', 'staple', "_#{name.dasherize}.scss"]
    end

    def language
      :scss
    end
  end

  class JavaScriptSnippet < Snippet
    def path_segments
      [SOURCE_DIR, 'javascripts', 'staple', "#{name.underscore}.js"]
    end

    def language
      :javascript
    end
  end

  def code_for(snippet_name)
    partial 'code', locals: { snippets: snippets_for(snippet_name) }
  end

  private

  def snippets_for(name)
    [HtmlSnippet, ScssSnippet, JavaScriptSnippet].map do |snippet_factory|
      snippet = snippet_factory.new(name)
      render_snippet snippet.path, snippet.language
    end.join("\n")
  end

  def render_snippet(path, language)
    if File.exists?(path)
      partial 'snippet', locals: {
        snippet: File.read(path),
        language: language,
      }
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
staple-0.1.7 lib/snippet_helpers.rb
staple-0.1.6 lib/snippet_helpers.rb
staple-0.1.5 lib/snippet_helpers.rb
staple-0.1.4 lib/snippet_helpers.rb
staple-0.1.3 lib/snippet_helpers.rb
staple-0.1.2 lib/snippet_helpers.rb
staple-0.1.1 lib/snippet_helpers.rb
staple-0.1.0 lib/snippet_helpers.rb
staple-0.0.9 lib/snippet_helpers.rb
staple-0.0.8 lib/snippet_helpers.rb
staple-0.0.7 lib/snippet_helpers.rb
staple-0.0.6 lib/snippet_helpers.rb
staple-0.0.5 lib/snippet_helpers.rb
staple-0.0.4 lib/snippet_helpers.rb
staple-0.0.3 lib/snippet_helpers.rb
staple-0.0.2 lib/snippet_helpers.rb