class Schemacop::V3::GlobalContext

Constants

DSL_METHODS

Public Class Methods

eager_load!() click to toggle source
# File lib/schemacop/v3/global_context.rb, line 10
def self.eager_load!
  instance.eager_load!
end
instance() click to toggle source
# File lib/schemacop/v3/global_context.rb, line 6
def self.instance
  @instance ||= new
end
new() click to toggle source
Calls superclass method Schemacop::V3::Context::new
# File lib/schemacop/v3/global_context.rb, line 48
def initialize
  super
  @schemas = {}
  @load_paths_by_schemas = {}
  @eager_loaded = false
  @current_virtual_path = nil
end
schema_for(path) click to toggle source
# File lib/schemacop/v3/global_context.rb, line 18
def self.schema_for(path)
  instance.schema_for(path)
end
schemas() click to toggle source
# File lib/schemacop/v3/global_context.rb, line 14
def self.schemas
  instance.schemas
end

Public Instance Methods

eager_load!() click to toggle source
# File lib/schemacop/v3/global_context.rb, line 32
def eager_load!
  @schemas = {}

  fail "Global context can't be eager loaded more than once." if @eager_loaded

  Schemacop.load_paths.each do |load_path|
    Dir.glob(File.join(load_path, '**', '*.rb')).sort.each do |file|
      load_file(file, load_path)
    end
  end

  @eager_loaded = true
end
schema(type = :hash, **options, &block) click to toggle source
# File lib/schemacop/v3/global_context.rb, line 22
def schema(type = :hash, **options, &block)
  @current_schemas << Node.create(type, **options, &block)
end
schema_for(path) click to toggle source
# File lib/schemacop/v3/global_context.rb, line 26
def schema_for(path)
  path = path.to_sym
  load_schema(path) unless @eager_loaded
  @schemas[path]
end

Private Instance Methods

load_file(path, load_path) click to toggle source
# File lib/schemacop/v3/global_context.rb, line 79
def load_file(path, load_path)
  return false unless File.exist?(path)

  # Determine virtual path
  virtual_path = virtual_path_for(path, load_path)

  # Run file and collect schemas
  begin
    @current_schemas = []
    env = ScopedEnv.new(self, DSL_METHODS)
    env.instance_eval IO.read(path)
  rescue StandardError => e
    fail "Could not load schema #{path.inspect}: #{e.message}"
  end

  # Load schemas
  case @current_schemas.size
  when 0
    fail "Schema #{path.inspect} does not define any schema."
  when 1
    if @schemas.include?(virtual_path)
      fail "Schema #{virtual_path.to_s.inspect} is defined in both load paths "\
           "#{@load_paths_by_schemas[virtual_path].inspect} and #{load_path.inspect}."
    end

    @load_paths_by_schemas[virtual_path] = load_path
    @schemas = @schemas.merge(virtual_path => @current_schemas.first)
  else
    fail "Schema #{path.inspect} defines multiple schemas."
  end

  return true
end
load_schema(virtual_path) click to toggle source
# File lib/schemacop/v3/global_context.rb, line 64
def load_schema(virtual_path)
  path = path_for(virtual_path)

  @schemas = schemas.except(virtual_path).freeze
  @load_paths_by_schemas = @load_paths_by_schemas.except(virtual_path)

  Schemacop.load_paths.each do |load_path|
    path_in_load_path = File.join(load_path, path)

    if File.exist?(path_in_load_path)
      load_file(path_in_load_path, load_path)
    end
  end
end
path_for(virtual_path) click to toggle source
# File lib/schemacop/v3/global_context.rb, line 56
def path_for(virtual_path)
  "#{virtual_path.to_s.underscore}.rb"
end
virtual_path_for(path, load_path) click to toggle source
# File lib/schemacop/v3/global_context.rb, line 60
def virtual_path_for(path, load_path)
  Pathname.new(path).relative_path_from(load_path).to_s.underscore.gsub(/\.rb$/, '').to_sym
end