Sha256: c45e700ef42927056f834c90d093e2da5c3ee28015177d7b5401ffd183ad002f

Contents?: true

Size: 1.54 KB

Versions: 8

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Luo
  module Prompts
    extend Dry::Configurable

    setting :gem_templates_dir, default: "#{__dir__}/../../templates/prompts"
    setting :prompts_dir, default: "#{Dir.pwd}/prompts"

    extend self

    def define_templates
      define_templates_in_dir(config.gem_templates_dir)
      define_templates_in_dir(config.prompts_dir)
    end

    def define_templates_in_dir(dir_path)
      path = Pathname.new(dir_path).expand_path.join("*.md.erb").to_s
      Dir.glob(path) do |file|
        template_name = File.basename(file, '.md.erb').to_sym
        define_template(template_name, file)
      end
    end

    def define_template(template_name, file_path)
      define_method(template_name) do
        PromptTemplate.create(file_path)
      end
    end

    define_templates

    def method_missing(method_name, *args, &block)
      template_file = template_file_path(method_name)
      if template_file
        define_template(method_name, template_file)
        send(method_name)
      else
        super
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      template_file_path(method_name) || super
    end

    private

    def template_file_path(template_name)
      gem_template_file = "#{config.gem_templates_dir}/#{template_name}.md.erb"
      prompts_template_file = "#{config.prompts_dir}/#{template_name}.md.erb"

      return prompts_template_file if File.exist?(prompts_template_file)
      return gem_template_file if File.exist?(gem_template_file)

      nil
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
luo-0.2.7 lib/luo/prompts.rb
luo-0.2.6 lib/luo/prompts.rb
luo-0.2.5 lib/luo/prompts.rb
luo-0.2.4 lib/luo/prompts.rb
luo-0.2.3 lib/luo/prompts.rb
luo-0.2.2 lib/luo/prompts.rb
luo-0.2.1 lib/luo/prompts.rb
luo-0.2.0 lib/luo/prompts.rb