Module: GraphQLDocs::Helpers

Included in:
Generator, Parser, Renderer
Defined in:
lib/graphql-docs/helpers.rb

Constant Summary collapse

SLUGIFY_PRETTY_REGEXP =
Regexp.new("[^[:alnum:]._~!$&'()+,;[email protected]]+").freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#templatesObject

Returns the value of attribute templates


5
6
7
# File 'lib/graphql-docs/helpers.rb', line 5

def templates
  @templates
end

Instance Method Details

#graphql_enum_typesObject


40
41
42
# File 'lib/graphql-docs/helpers.rb', line 40

def graphql_enum_types
  @parsed_schema[:enum_types] || []
end

#graphql_input_object_typesObject


48
49
50
# File 'lib/graphql-docs/helpers.rb', line 48

def graphql_input_object_types
  @parsed_schema[:input_object_types] || []
end

#graphql_interface_typesObject


36
37
38
# File 'lib/graphql-docs/helpers.rb', line 36

def graphql_interface_types
  @parsed_schema[:interface_types] || []
end

#graphql_mutation_typesObject


28
29
30
# File 'lib/graphql-docs/helpers.rb', line 28

def graphql_mutation_types
  @parsed_schema[:mutation_types] || []
end

#graphql_object_typesObject


32
33
34
# File 'lib/graphql-docs/helpers.rb', line 32

def graphql_object_types
  @parsed_schema[:object_types] || []
end

#graphql_operation_typesObject


24
25
26
# File 'lib/graphql-docs/helpers.rb', line 24

def graphql_operation_types
  @parsed_schema[:operation_types] || []
end

#graphql_scalar_typesObject


52
53
54
# File 'lib/graphql-docs/helpers.rb', line 52

def graphql_scalar_types
  @parsed_schema[:scalar_types] || []
end

#graphql_union_typesObject


44
45
46
# File 'lib/graphql-docs/helpers.rb', line 44

def graphql_union_types
  @parsed_schema[:union_types] || []
end

#has_yaml?(contents) ⇒ Boolean


73
74
75
# File 'lib/graphql-docs/helpers.rb', line 73

def has_yaml?(contents)
  contents =~ /\A-{3,5}\s*$/
end

#include(filename, opts = {}) ⇒ Object


13
14
15
16
17
# File 'lib/graphql-docs/helpers.rb', line 13

def include(filename, opts = {})
  template = fetch_include(filename)
  opts = { base_url: @options[:base_url], classes: @options[:classes] }.merge(opts)
  template.result(OpenStruct.new(opts.merge(helper_methods)).instance_eval { binding })
end

#markdownify(string) ⇒ Object


19
20
21
22
# File 'lib/graphql-docs/helpers.rb', line 19

def markdownify(string)
  return '' if string.nil?
  ::CommonMarker.render_html(string, :DEFAULT).strip
end

#slugify(str) ⇒ Object


7
8
9
10
11
# File 'lib/graphql-docs/helpers.rb', line 7

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

#split_into_metadata_and_contents(contents) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/graphql-docs/helpers.rb', line 56

def (contents)
  opts = {}
  pieces = yaml_split(contents)
  if pieces.size < 4
    raise RuntimeError.new(
      "The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format.",
    )
  end
  # Parse
  begin
    meta = YAML.load(pieces[2]) || {}
  rescue Exception => e # rubocop:disable Lint/RescueException
    raise "Could not parse YAML for #{name}: #{e.message}"
  end
  [meta, pieces[4]]
end

#yaml_split(contents) ⇒ Object


77
78
79
# File 'lib/graphql-docs/helpers.rb', line 77

def yaml_split(contents)
  contents.split(/^(-{5}|-{3})[ \t]*\r?\n?/, 3)
end