Class: Schemacop::V3::GlobalContext

Inherits:
Context
  • Object
show all
Defined in:
lib/schemacop/v3/global_context.rb

Constant Summary collapse

DSL_METHODS =
%i[schema].freeze

Constants inherited from Context

Context::DEFAULT_JSON_FORMAT

Instance Attribute Summary

Attributes inherited from Context

#json_format, #schemas

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Context

#swagger_json?, #with_json_format

Class Method Details

.eager_load!Object



10
11
12
# File 'lib/schemacop/v3/global_context.rb', line 10

def self.eager_load!
  instance.eager_load!
end

.instanceObject



6
7
8
# File 'lib/schemacop/v3/global_context.rb', line 6

def self.instance
  @instance ||= new
end

.schema_for(path) ⇒ Object



18
19
20
# File 'lib/schemacop/v3/global_context.rb', line 18

def self.schema_for(path)
  instance.schema_for(path)
end

.schemasObject



14
15
16
# File 'lib/schemacop/v3/global_context.rb', line 14

def self.schemas
  instance.schemas
end

Instance Method Details

#eager_load!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



22
23
24
# 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) ⇒ Object



26
27
28
29
30
# 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