Sha256: db4c8db906634f133f3a964141f31cdfce8a498ead0911b62ce60a779400218c

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module GraphQLDocs
  class Generator
    module Helpers
      SLUGIFY_PRETTY_REGEXP = Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze

      attr_accessor :templates

      def slugify(str)
        slug = str.gsub(SLUGIFY_PRETTY_REGEXP, '-')
        slug.gsub!(%r!^\-|\-$!i, '')
        slug.downcase
      end

      def include(filename, opts)
        template = fetch_include(filename)
        template.result(OpenStruct.new(opts.merge(helper_methods)).instance_eval { binding })
      end

      def fetch_include(filename)
        @templates ||= {}

        return @templates[filename] unless @templates[filename].nil?

        @templates[filename] = ERB.new(File.read(File.join(@options[:templates][:includes], filename)))
        @templates[filename]
      end

      def markdown(string)
        GitHub::Markdown.render(string || 'n/a')
      end

      def helper_methods
        return @helper_methods if defined?(@helper_methods)

        @helper_methods = {}

        Helpers.instance_methods.each do |name|
          next if name == :helper_methods
          @helper_methods[name] = method(name)
        end

        @helper_methods
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-docs-0.1.0 lib/graphql-docs/generator/helpers.rb