11# frozen_string_literal: true
2+
23require 'erb'
34require 'sass'
45
@@ -14,14 +15,13 @@ def initialize(parsed_schema, options)
1415
1516 @renderer = @options [ :renderer ] . new ( @parsed_schema , @options )
1617
17- %i( operations objects mutations interfaces enums unions input_objects scalars directives ) . each do |sym |
18- if !File . exist? ( @options [ :templates ] [ sym ] )
19- raise IOError , "`#{ sym } ` template #{ @options [ :templates ] [ sym ] } was not found"
20- end
18+ %i[ operations objects mutations interfaces enums unions input_objects scalars directives ] . each do |sym |
19+ raise IOError , "`#{ sym } ` template #{ @options [ :templates ] [ sym ] } was not found" unless File . exist? ( @options [ :templates ] [ sym ] )
20+
2121 instance_variable_set ( "@graphql_#{ sym } _template" , ERB . new ( File . read ( @options [ :templates ] [ sym ] ) ) )
2222 end
2323
24- %i( index object query mutation interface enum union input_object scalar directive ) . each do |sym |
24+ %i[ index object query mutation interface enum union input_object scalar directive ] . each do |sym |
2525 if @options [ :landing_pages ] [ sym ] . nil?
2626 instance_variable_set ( "@#{ sym } _landing_page" , nil )
2727 elsif !File . exist? ( @options [ :landing_pages ] [ sym ] )
@@ -33,7 +33,7 @@ def initialize(parsed_schema, options)
3333
3434 if File . extname ( ( @options [ :landing_pages ] [ sym ] ) ) == '.erb'
3535 opts = @options . merge ( @options [ :landing_pages ] [ :variables ] ) . merge ( helper_methods )
36- if has_yaml ?( landing_page_contents )
36+ if yaml ?( landing_page_contents )
3737 metadata , landing_page = split_into_metadata_and_contents ( landing_page_contents , parse : false )
3838 erb_template = ERB . new ( landing_page )
3939 else
@@ -60,45 +60,25 @@ def generate
6060 create_graphql_scalar_pages
6161 create_graphql_directive_pages
6262
63- unless @graphql_index_landing_page . nil?
64- write_file ( 'static' , 'index' , @graphql_index_landing_page , trim : false )
65- end
63+ write_file ( 'static' , 'index' , @graphql_index_landing_page , trim : false ) unless @graphql_index_landing_page . nil?
6664
67- unless @graphql_object_landing_page . nil?
68- write_file ( 'static' , 'object' , @graphql_object_landing_page , trim : false )
69- end
65+ write_file ( 'static' , 'object' , @graphql_object_landing_page , trim : false ) unless @graphql_object_landing_page . nil?
7066
71- if !@graphql_query_landing_page . nil? && !has_query
72- write_file ( 'operation' , 'query' , @graphql_query_landing_page , trim : false )
73- end
67+ write_file ( 'operation' , 'query' , @graphql_query_landing_page , trim : false ) if !@graphql_query_landing_page . nil? && !has_query
7468
75- unless @graphql_mutation_landing_page . nil?
76- write_file ( 'operation' , 'mutation' , @graphql_mutation_landing_page , trim : false )
77- end
69+ write_file ( 'operation' , 'mutation' , @graphql_mutation_landing_page , trim : false ) unless @graphql_mutation_landing_page . nil?
7870
79- unless @graphql_interface_landing_page . nil?
80- write_file ( 'static' , 'interface' , @graphql_interface_landing_page , trim : false )
81- end
71+ write_file ( 'static' , 'interface' , @graphql_interface_landing_page , trim : false ) unless @graphql_interface_landing_page . nil?
8272
83- unless @graphql_enum_landing_page . nil?
84- write_file ( 'static' , 'enum' , @graphql_enum_landing_page , trim : false )
85- end
73+ write_file ( 'static' , 'enum' , @graphql_enum_landing_page , trim : false ) unless @graphql_enum_landing_page . nil?
8674
87- unless @graphql_union_landing_page . nil?
88- write_file ( 'static' , 'union' , @graphql_union_landing_page , trim : false )
89- end
75+ write_file ( 'static' , 'union' , @graphql_union_landing_page , trim : false ) unless @graphql_union_landing_page . nil?
9076
91- unless @graphql_input_object_landing_page . nil?
92- write_file ( 'static' , 'input_object' , @graphql_input_object_landing_page , trim : false )
93- end
77+ write_file ( 'static' , 'input_object' , @graphql_input_object_landing_page , trim : false ) unless @graphql_input_object_landing_page . nil?
9478
95- unless @graphql_scalar_landing_page . nil?
96- write_file ( 'static' , 'scalar' , @graphql_scalar_landing_page , trim : false )
97- end
79+ write_file ( 'static' , 'scalar' , @graphql_scalar_landing_page , trim : false ) unless @graphql_scalar_landing_page . nil?
9880
99- unless @graphql_directive_landing_page . nil?
100- write_file ( 'static' , 'directive' , @graphql_directive_landing_page , trim : false )
101- end
81+ write_file ( 'static' , 'directive' , @graphql_directive_landing_page , trim : false ) unless @graphql_directive_landing_page . nil?
10282
10383 if @options [ :use_default_styles ]
10484 assets_dir = File . join ( File . dirname ( __FILE__ ) , 'layouts' , 'assets' )
@@ -117,23 +97,23 @@ def generate
11797 def create_graphql_query_pages
11898 graphql_operation_types . each do |query_type |
11999 metadata = ''
120- if query_type [ :name ] == graphql_root_types [ 'query' ]
121- unless @options [ :landing_pages ] [ :query ] . nil?
122- query_landing_page = @options [ :landing_pages ] [ :query ]
123- query_landing_page = File . read ( query_landing_page )
124- if has_yaml? ( query_landing_page )
125- pieces = yaml_split ( query_landing_page )
126- pieces [ 2 ] = pieces [ 2 ] . chomp
127- metadata = pieces [ 1 , 3 ] . join ( "\n " )
128- query_landing_page = pieces [ 4 ]
129- end
130- query_type [ :description ] = query_landing_page
100+ next unless query_type [ :name ] == graphql_root_types [ 'query' ]
101+
102+ unless @options [ :landing_pages ] [ :query ] . nil?
103+ query_landing_page = @options [ :landing_pages ] [ :query ]
104+ query_landing_page = File . read ( query_landing_page )
105+ if yaml? ( query_landing_page )
106+ pieces = yaml_split ( query_landing_page )
107+ pieces [ 2 ] = pieces [ 2 ] . chomp
108+ metadata = pieces [ 1 , 3 ] . join ( "\n " )
109+ query_landing_page = pieces [ 4 ]
131110 end
132- opts = default_generator_options ( type : query_type )
133- contents = @graphql_operations_template . result ( OpenStruct . new ( opts ) . instance_eval { binding } )
134- write_file ( 'operation' , 'query' , metadata + contents )
135- return true
111+ query_type [ :description ] = query_landing_page
136112 end
113+ opts = default_generator_options ( type : query_type )
114+ contents = @graphql_operations_template . result ( OpenStruct . new ( opts ) . instance_eval { binding } )
115+ write_file ( 'operation' , 'query' , metadata + contents )
116+ return true
137117 end
138118 false
139119 end
@@ -229,7 +209,7 @@ def write_file(type, name, contents, trim: true)
229209 FileUtils . mkdir_p ( path )
230210 end
231211
232- if has_yaml ?( contents )
212+ if yaml ?( contents )
233213 # Split data
234214 meta , contents = split_into_metadata_and_contents ( contents )
235215 @options = @options . merge ( meta )
0 commit comments