Sha256: 767ed350160fa9a221bb6f049337a4dc9061903dc28d264c8a6b1ce9fd4e25aa

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 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.haml"]
    end

    def language
      :markup
    end
  end

  class SassSnippet < Snippet
    def path_segments
      [SOURCE_DIR, 'stylesheets', 'breezeblocks', "_#{name.dasherize}.sass"]
    end

    def language
      :sass
    end
  end

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

    def language
      :javascript
    end
  end

  class CoffeeScriptSnippet < Snippet
    def path_segments
      [
        SOURCE_DIR,
        "javascripts",
        "breezeblocks",
        "coffeescript",
        "#{name.underscore}.coffee"
      ]
    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,
      SassSnippet,
      JavaScriptSnippet,
      CoffeeScriptSnippet,
    ].map do |snippet_factory|
      render_snippet snippet_factory.new(name)
    end.join("\n")
  end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
breezeblocks-0.0.4 lib/snippet_helpers.rb
breezeblocks-0.0.2 lib/snippet_helpers.rb
breezeblocks-0.0.1 lib/snippet_helpers.rb