Sha256: fbbaae549bdb5908b4cbfe4f3d84272bb4ccb4a80dfff9a318eba472c1b1dd63

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'erb'

module RenderingEngine
  class Content
    attr_reader :file_repo, :file_path, :data

    def initialize(file_repo, file_path, opts = {})
      @file_repo        = file_repo
      @file_path        = file_path
      @base_folder_path = opts[:base_folder_path]
      @data             = opts[:data]
      @custom_helper    = opts[:custom_helper]
    end

    def source
      @source ||= (
        case kind
        when :orginal  then file_repo.read(file_path)
        when :template
          ERB.new(file_repo.read(file_path_as_erb)).result(
            helper(base_folder_path, data).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_repo.file_dirname(file_path)
    end

    private

    attr_reader :custom_helper

    def helper(path, content_data)
      (custom_helper || ContentHelpers).new(file_repo,
                                            base_path: path,
                                            data: content_data)
    end

    def orginal_file_present?
      file_repo.exist?(file_path)
    end

    def file_as_erb_present?
      file_repo.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.2.0 lib/rendering_engine/content.rb