Sha256: 64ac87b2e95ecbd8b2c39adc3e6c120d0c32825c2edbf51d2d12fec0966f1c7f

Contents?: true

Size: 1.78 KB

Versions: 9

Compression:

Stored size: 1.78 KB

Contents

module Ore
  module Template
    #
    # Handles the expansion of paths and substitution of path keywords.
    # The following keywords are supported:
    #
    # * `[name]` - The name of the project.
    # * `[project_dir]` - The directory base-name derived from the project
    #   name.
    # * `[namespace_path]` - The full directory path derived from the
    #   project name.
    # * `[namespace_dir]` - The last directory name derived from the
    #    project name.
    #
    module Interpolations
      # The accepted interpolation keywords that may be used in paths
      @@keywords = %w[
        name
        version
        project_dir
        namespace_path
        namespace_dir
        markup
        date
        year
        month
        day
      ]

      protected

      #
      # Expands the given path by substituting the interpolation keywords
      # for the related instance variables.
      #
      # @param [String] path
      #   The path to expand.
      #
      # @return [String]
      #   The expanded path.
      #
      # @example Assuming `@project_dir` contains `my_project`.
      #   interpolate("lib/[project_dir].rb")
      #   # => "lib/my_project.rb"
      #
      # @example Assuming `@namespace_path` contains `my/project`.
      #   interpolate("spec/[namespace_path]_spec.rb")
      #   # => "spec/my/project_spec.rb"
      #
      def interpolate(path)
        dirs = path.split(File::SEPARATOR)

        dirs.each do |dir|
          dir.gsub!(/(\[[a-z_]+\])/) do |capture|
            keyword = capture[1..-2]

            if @@keywords.include?(keyword)
              instance_variable_get("@#{keyword}")
            else
              capture
            end
          end
        end

        return File.join(dirs.reject { |dir| dir.empty? })
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ore-0.11.0 lib/ore/template/interpolations.rb
ore-0.10.0 lib/ore/template/interpolations.rb
ore-0.9.4 lib/ore/template/interpolations.rb
ore-0.9.3 lib/ore/template/interpolations.rb
ore-0.9.2 lib/ore/template/interpolations.rb
ore-0.9.1 lib/ore/template/interpolations.rb
ore-0.9.0 lib/ore/template/interpolations.rb
ore-0.8.1 lib/ore/template/interpolations.rb
ore-0.8.0 lib/ore/template/interpolations.rb