Sha256: bca3e2d66e53c97d477a5066ec52bf94d19a9b6829620416c2389db9f02a1e9f

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require "erb"
require "raml"
require "rambo/raml_models"
require "rambo/rspec/examples"

module Rambo
  module RSpec
    class HelperFile
      attr_reader :options

      def initialize(template_path:, file_path:, raml: nil, options: nil)
        @template_path = template_path
        @file_path     = file_path
        @options       = options || { framework: :rails }
        @raml          = raml ? Rambo::RamlModels::Api.new(raml) : nil
      end

      def app_classes
        {
          :rails             => "Rails.application",
          :"sinatra:classic" => "Sinatra::Application",
          :"sinatra:modular" => "Sinatra::Base.descendants.find {|klass| klass != Sinatra::Application } || Sinatra::Application",
          :grape             => "Grape::API.descendants.first"
        }
      end

      def generate
        write_to_file(render) unless file_already_exists?
      end

      def render
        b = binding
        ERB.new(template, 0, "-", "@result").result(raml.instance_eval { b })
        @result
      end

      private

      attr_reader :template_path, :file_path, :raml

      def file_already_exists?
        File.exist?(file_path)
      end

      def write_to_file(template)
        File.write(file_path, template)
      end

      def template
        @template ||= File.read(template_path)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rambo_ruby-0.6.0 lib/rambo/rspec/helper_file.rb