Sha256: c10ead51d079db23da830f84bb8060a0554cb17769dec71454071cacc58460ab

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

require "active_support/core_ext/string"

# Encapsulates helper methods and instance variables to be rendered in the ERB
# templates.
module Forger::Template
  class Context
    include Forger::Template::Helper

    def initialize(options={})
      @options = options
      load_variables
      load_custom_helpers
    end

  private
    # Load variables from:
    #   config/variables/development.rb
    #   config/variables/production.rb
    #   etc
    def load_variables
      load_variables_file(:base)
      load_variables_file(Forger.env)
    end

    def load_variables_file(type)
      path = "#{Forger.root}/config/variables/#{type}.rb"
      instance_eval(IO.read(path), path) if File.exist?(path)
    end

    # Load custom helper methods from project
    def load_custom_helpers
      Dir.glob("#{Forger.root}/app/helpers/**/*_helper.rb").each do |path|
        filename = path.sub(%r{.*/},'').sub('.rb','')
        module_name = filename.camelize

        # Prepend a period so require works FORGER_ROOT is set to a relative path
        # without a period.
        #
        # Example: FORGER_ROOT=tmp/project
        first_char = path[0..0]
        path = "./#{path}" unless %w[. /].include?(first_char)
        require path
        self.class.send :include, module_name.constantize
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
forger-3.0.2 lib/forger/template/context.rb
forger-3.0.1 lib/forger/template/context.rb
forger-3.0.0 lib/forger/template/context.rb