module GraphQL::Client::ViewModule

def self.extract_graphql_section(src)

defined.
Returns String GraphQL query and line number or nil or no section was

src - String ERB text

Public: Extract GraphQL section from ERB template.
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

def self.valid_constant_name?(name)

result in a "NameError: wrong constant name".
Returns true if name is a valid constant, otherwise false if name would

valid_constant_name?("404") #=> false
valid_constant_name?("Foo") #=> true

Examples

name - String or Symbol constant name

Internal: Check if name is a valid Ruby constant identifier.
def self.valid_constant_name?(name)
  name.to_s =~ /^[A-Z][a-zA-Z0-9_]*$/
end

def const_missing(name)

Returns module or raises NameError if missing.

name - String or Symbol constant name

Public: Implement constant missing hook to autoload View ERB statics.
def const_missing(name)
  load_and_set_module(name) || super
end

def eager_load!

Returns nothing.

Views.eager_load!

Examples

ERB files.
Traverses all app/views/**/*.erb and loads all static constants defined in

Use in production when cache_classes is true.

Public: Eager load module and all subdependencies.
def eager_load!
  return unless File.directory?(load_path)
  Dir.entries(load_path).sort.each do |entry|
    next if entry == "." || entry == ".."
    name = entry.sub(/(\.\w+)+$/, "").camelize.to_sym
    if ViewModule.valid_constant_name?(name)
      mod = const_defined?(name, false) ? const_get(name) : load_and_set_module(name)
      mod.eager_load! if mod
    end
  end
  nil
end

def load_and_set_module(name)

def load_and_set_module(name)
  placeholder = placeholder_module(name)
  const_set(name, placeholder) if placeholder
  mod = load_module(name)
  return placeholder unless mod
  remove_const(name) if placeholder
  const_set(name, mod)
  mod.unloadable if mod.respond_to?(:unloadable)
  mod
end

def load_module(name)

Returns new Module implementing Loadable concern.

Views::Users::Profile.load_module(:Show)
Views::Users.load_module(:Profile)

Examples

name - String or Symbol constant name.

Internal: Initialize new module for constant name and load ERB statics.
def load_module(name)
  pathname = ActiveSupport::Inflector.underscore(name.to_s)
  path = Dir[File.join(load_path, "{#{pathname},_#{pathname}}{.*}")].sort.map { |fn| File.expand_path(fn) }.first
  return if !path || File.extname(path) != ".erb"
  contents = File.read(path)
  query, lineno = ViewModule.extract_graphql_section(contents)
  return unless query
  mod = client.parse(query, path, lineno)
  mod.extend(ViewModule)
  mod.load_path = File.join(load_path, pathname)
  mod.source_path = path
  mod.client = client
  mod
end

def placeholder_module(name)

def placeholder_module(name)
  dirname = File.join(load_path, ActiveSupport::Inflector.underscore(name.to_s))
  return nil unless Dir.exist?(dirname)
  Module.new.tap do |mod|
    mod.extend(ViewModule)
    mod.load_path = dirname
    mod.client = client
  end
end