Module: OpenAPISourceTools::Loaders

Defined in:
lib/openapi/sourcetools/loaders.rb

Overview

Loaders used to load gems and files and set config etc. Exposed as Gen.loaders if you need to modify the array.

Constant Summary collapse

REQ_PREFIX =

Prefix etc. and loader pairs for all loaders.

'req:'
EVAL_PREFIX =
'eval:'
RUBY_EXT =
'.rb'
YAML_PREFIX =
'yaml:'
YAML_EXTS =
[ '.yaml', '.yml' ].freeze
BIN_PREFIX =
'bin:'
CONFIG_PREFIX =
'config:'
SEPARATOR_PREFIX =
'separator:'

Class Method Summary collapse

Class Method Details

.bin_loader(name) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/openapi/sourcetools/loaders.rb', line 102

def self.bin_loader(name)
  return false unless name.downcase.start_with?(BIN_PREFIX)
  n, _sep, f = name.slice(BIN_PREFIX.size...name.size).partition(':')
  raise StandardError, 'No name given.' if n.empty?
  raise StandardError, 'No filename given.' if f.empty?
  doc = File.binread(f)
  raise StandardError, "#{name} #{n} exists already." unless Gen.d.add(n, doc)
  true
rescue Errno::ENOENT
  raise StandardError, "Not found: #{f}\n#{e}"
rescue Exception => e # Whatever was raised, we want it.
  raise StandardError, "Failed to read #{f}\n#{e}"
end

.config_loader(name) ⇒ Object

Raises:

  • (StandardError)


118
119
120
121
122
123
124
125
126
# File 'lib/openapi/sourcetools/loaders.rb', line 118

def self.config_loader(name)
  return false unless name.downcase.start_with?(CONFIG_PREFIX)
  raise StandardError, "Config name remains: #{Gen.config}" unless Gen.config.nil?
  n = name.slice(CONFIG_PREFIX.size...name.size)
  raise StandardError, 'No name given.' if n.empty?
  # Interpretation left completely to config loading.
  Gen.config = n
  true
end

.documentObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/openapi/sourcetools/loaders.rb', line 150

def self.document
  <<EOB
- #{Loaders::REQ_PREFIX}req_name : requires the gem.
- #{Loaders::EVAL_PREFIX}code : runs code to add gem tasks again.
- ruby_file#{Loaders::RUBY_EXT} : changes to Ruby file directory and requires the file.
- #{Loaders::YAML_PREFIX}name:filename : Loads YAML file into Gen.d.name.
- name:filename.{#{(Loaders::YAML_EXTS.map { |s| s[1...s.size] }).join('|')}} : Loads YAML file into Gen.d.name.
- #{Loaders::BIN_PREFIX}name:filename : Loads binary file into Gen.d.name.
- #{Loaders::CONFIG_PREFIX}name : Sets Gen.config for next gem/Ruby file configuration loading.
- #{Loaders::SEPARATOR_PREFIX}string : Sets Gen.separator to string.
EOB
end

.eval_loader(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/openapi/sourcetools/loaders.rb', line 39

def self.eval_loader(name)
  return false unless name.downcase.start_with?(EVAL_PREFIX)
  begin
    t = OpenAPISourceTools::RestoreProcessorStorage.new({})
    Gen.tasks.push(t)
    code = name.slice(EVAL_PREFIX.size...name.size)
    eval(code)
    Gen.config = nil
    t.x = Gen.x # In case setup code replaced the object.
  rescue Exception => e
    raise StandardError, "Problem with #{name}\n#{e}\n#{e.backtrace.join("\n")}"
  end
  true
end

.loadersObject



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/openapi/sourcetools/loaders.rb', line 138

def self.loaders
  [
    method(:req_loader),
    method(:eval_loader),
    method(:ruby_loader),
    method(:yaml_loader),
    method(:bin_loader),
    method(:config_loader),
    method(:separator_loader)
  ]
end

.req_loader(name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/openapi/sourcetools/loaders.rb', line 20

def self.req_loader(name)
  return false unless name.downcase.start_with?(REQ_PREFIX)
  begin
    t = OpenAPISourceTools::RestoreProcessorStorage.new({})
    Gen.tasks.push(t)
    base = name.slice(REQ_PREFIX.size...name.size)
    require(base)
    Gen.config = nil
    t.x = Gen.x # In case setup code replaced the object.
  rescue LoadError => e
    raise StandardError, "Failed to require #{name}\n#{e}"
  rescue Exception => e
    raise StandardError, "Problem with #{name}\n#{e}\n#{e.backtrace.join("\n")}"
  end
  true
end

.ruby_loader(name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/openapi/sourcetools/loaders.rb', line 56

def self.ruby_loader(name)
  return false unless name.downcase.end_with?(RUBY_EXT)
  origwd = Dir.pwd
  d = File.dirname(name)
  Dir.chdir(d) unless d == '.'
  begin
    t = OpenAPISourceTools::RestoreProcessorStorage.new({})
    Gen.tasks.push(t)
    base = File.basename(name)
    Gen.config = base[0..-4] if Gen.config.nil?
    load(File.join(Dir.pwd, base))
    Gen.config = nil
    t.x = Gen.x # In case setup code replaced the object.
  rescue LoadError => e
    raise StandardError, "Failed to load #{name}\n#{e}"
  rescue Exception => e
    raise StandardError, "Problem with #{name}\n#{e}\n#{e.backtrace.join("\n")}"
  end
  Dir.chdir(origwd) unless d == '.'
  true
end

.separator_loader(name) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/openapi/sourcetools/loaders.rb', line 130

def self.separator_loader(name)
  return false unless name.downcase.start_with?(SEPARATOR_PREFIX)
  n = name.slice(SEPARATOR_PREFIX.size...name.size)
  n = nil if n.empty?
  Gen.separator = n
  true
end

.yaml_loader(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/openapi/sourcetools/loaders.rb', line 81

def self.yaml_loader(name)
  d = name.downcase
  if d.start_with?(YAML_PREFIX)
    name = name.slice(YAML_PREFIX.size...name.size)
  elsif (YAML_EXTS.index { |s| d.end_with?(s) }).nil?
    return false
  end
  n, _sep, f = name.partition(':')
  raise StandardError, 'No name given.' if n.empty?
  raise StandardError, 'No filename given.' if f.empty?
  doc = YAML.safe_load_file(f)
  raise StandardError, "#{name} #{n} exists already." unless Gen.d.add(n, doc)
  true
rescue Errno::ENOENT
  raise StandardError, "Not found: #{f}\n#{e}"
rescue Exception => e # Whatever was raised, we want it.
  raise StandardError, "Failed to read as YAML: #{f}\n#{e}"
end