Sha256: a7bd877718a16767658fe3d85205ae13190689956c02bc3fce2c1d041557e875

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require "active_support/inflector"

module Wizard
  class ConceptGenerator
    attr_reader :type

    def initialize(**args)
      @type = args[:type]
    end

    def generate(model, name, context = nil)
      materials = [model, type, name]
      materials.insert(1, context) unless context.nil?

      filename = materials.map { |material| ActiveSupport::Inflector.underscore(material) }.join("/")
      filename = "#{Wizard.configuration.base_directory}/#{filename}.rb"

      false if File.exist?(filename)

      content = copy(model, name, context)
      create_file(filename, content)

      filename
    end

    private

    def copy(model, name, context)
      template = File.dirname(__FILE__)
      template["lib/wizard"] = "lib/concept.txt"

      content = File.read(template)

      content["_MODEL_"] = model.camelize
      content["::_CONTEXT_"] = context.nil? ? "" : "::#{context.camelize}"
      content["_NAME_"] = name.camelize
      content["_CONCEPT_"] = type.camelize while content.include?("_CONCEPT_")

      content
    end

    def create_file(filename, content)
      FileHelper.mkdir(filename)
      File.open(filename, "w+") { |file| file.write(content) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trailblazer-wizard-0.0.4 lib/wizard/concept_generator.rb