Sha256: 2518340eb92cd24d92983ee6ed752d5e0598ca5fbf7e860377a6eca5357c06d2

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'erb'

module RenderingEngine
  class Content
    attr_reader :file_path

    def initialize(file_path, base_folder_path=nil)
      @file_path        = file_path
      @base_folder_path = base_folder_path
    end

    def source
      @source ||= (
        case kind
        when :orginal  then File.read(file_path)
        when :template then ERB.new(File.read(file_path_as_erb)).result(helpers(base_folder_path).instance_eval { binding })
        else ''
        end
      )
    end

    def kind
      @kind ||= (
        if orginal_file_present?
          :orginal
        elsif file_as_erb_present?
          :template
        else
          :unknown
        end
      )
    end

    def template?
      kind == :template
    end

    def orginal?
      kind == :orginal
    end

    def unknown?
      kind == :unknown
    end

    def base_folder_path
      @base_folder_path ||= File.dirname(file_path)
    end

    private

    def helpers(path)
      ContentHelpers.new(base_path: path)
    end

    def orginal_file_present?
      File.exist?(file_path)
    end

    def file_as_erb_present?
      File.exist?(file_path_as_erb)
    end

    def file_path_as_erb
      @file_as_erb ||= "#{file_path}.erb"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rendering_engine-0.0.1 lib/rendering_engine/content.rb