Module: GraphQL::Client::ViewModule
- Defined in:
- lib/graphql/client/view_module.rb
Overview
Allows a magic namespace to map to app/views/*/.erb files to retrieve statically defined GraphQL definitions.
# app/views/users/show.html.erb
<%grapql
fragment UserFragment on User { }
%>
# Loads graphql section from app/views/users/show.html.erb
Views::Users::Show::UserFragment
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#path ⇒ Object
Public: Source location that defined the Module.
Class Method Summary collapse
-
.extract_graphql_section(src) ⇒ Object
Public: Extract GraphQL section from ERB template.
-
.valid_constant_name?(name) ⇒ Boolean
Internal: Check if name is a valid Ruby constant identifier.
Instance Method Summary collapse
-
#const_missing(name) ⇒ Object
Public: Implement constant missing hook to autoload View ERB statics.
-
#const_path(name) ⇒ Object
Internal: Detect source location for constant name.
-
#eager_load! ⇒ Object
Public: Eager load module and all subdependencies.
-
#load_module(path) ⇒ Object
Internal: Initialize new module for constant name and load ERB statics.
-
#loadable_const_defined?(name) ⇒ Boolean
Public: Override constant defined to check if constant name matches a view directory or template namespace.
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
20 21 22 |
# File 'lib/graphql/client/view_module.rb', line 20 def client @client end |
#path ⇒ Object
Public: Source location that defined the Module.
Returns absolute String path under app/views.
106 107 108 |
# File 'lib/graphql/client/view_module.rb', line 106 def path @path end |
Class Method Details
.extract_graphql_section(src) ⇒ Object
Public: Extract GraphQL section from ERB template.
src - String ERB text
Returns String GraphQL query and line number or nil or no section was defined.
28 29 30 31 32 |
# File 'lib/graphql/client/view_module.rb', line 28 def self.extract_graphql_section(src) query_string = src.scan(/<%graphql([^%]+)%>/).flatten.first return nil unless query_string [query_string, Regexp.last_match.pre_match.count("\n") + 1] end |
.valid_constant_name?(name) ⇒ Boolean
Internal: Check if name is a valid Ruby constant identifier.
name - String or Symbol constant name
Examples
valid_constant_name?("Foo") #=> true
valid_constant_name?("404") #=> false
Returns true if name is a valid constant, otherwise false if name would result in a “NameError: wrong constant name”.
72 73 74 |
# File 'lib/graphql/client/view_module.rb', line 72 def self.valid_constant_name?(name) name.to_s =~ /^[A-Z][a-zA-Z0-9_]*$/ end |
Instance Method Details
#const_missing(name) ⇒ Object
Public: Implement constant missing hook to autoload View ERB statics.
name - String or Symbol constant name
Returns module or raises NameError if missing.
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/graphql/client/view_module.rb', line 154 def const_missing(name) path = const_path(name) if path mod = load_module(path) const_set(name, mod) mod.unloadable mod else super end end |
#const_path(name) ⇒ Object
Internal: Detect source location for constant name.
name - String or Symbol constant name
Examples
Views.const_path(:Users) #=> "app/views/users"
Views::Users.const_path(:Show) #=> "app/views/users/show.html.erb"
Views::Users.const_path(:Profile) #=> "app/views/users/_profile.html.erb"
Returns String absolute path to file, otherwise nil.
119 120 121 122 |
# File 'lib/graphql/client/view_module.rb', line 119 def const_path(name) pathname = ActiveSupport::Inflector.underscore(name.to_s) Dir[File.join(path, "{#{pathname},_#{pathname}}{/,.*}")].map { |fn| File.(fn) }.first end |
#eager_load! ⇒ Object
Public: Eager load module and all subdependencies.
Use in production when cache_classes is true.
Traverses all app/views/*/.erb and loads all static constants defined in ERB files.
Examples
Views.eager_load!
Returns nothing.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/graphql/client/view_module.rb', line 46 def eager_load! return unless File.directory?(path) Dir.entries(path).each do |entry| next if entry == "." || entry == ".." name = entry.sub(/(\.\w+)+$/, "").camelize.to_sym if ViewModule.valid_constant_name?(name) && loadable_const_defined?(name) mod = const_get(name, false) mod.eager_load! end end nil end |
#load_module(path) ⇒ Object
Internal: Initialize new module for constant name and load ERB statics.
path - String path of directory or erb file.
Examples
load_module("app/views/users")
load_module("app/views/users/show.html.erb")
Returns new Module implementing Loadable concern.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/graphql/client/view_module.rb', line 134 def load_module(path) mod = Module.new if File.extname(path) == ".erb" contents = File.read(path) query, lineno = ViewModule.extract_graphql_section(contents) mod = client.parse(query, path, lineno) if query end mod.extend(ViewModule) mod.client = client mod.path = path mod end |
#loadable_const_defined?(name) ⇒ Boolean
Public: Override constant defined to check if constant name matches a view directory or template namespace.
name - String or Symbol constant name inherit - If the lookup will also search the ancestors (default: true)
Returns true if definition is found, otherwise false. def const_defined?(name, inherit = true)
if super(name.to_sym, inherit)
true
elsif const_path(name)
true
else
false
end
end
93 94 95 96 97 98 99 100 101 |
# File 'lib/graphql/client/view_module.rb', line 93 def loadable_const_defined?(name) if const_defined?(name.to_sym, false) true elsif const_path(name) true else false end end |