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:'
REREQ_PREFIX =
'rereq:'
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



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

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)


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

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



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

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

.loadersObject



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

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

.req_loader(name) ⇒ Object



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

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

.rereq_loader(name) ⇒ Object



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

def self.rereq_loader(name)
  return false unless name.downcase.start_with?(REREQ_PREFIX)
  begin
    t = OpenAPISourceTools::RestoreProcessorStorage.new({})
    Gen.tasks.push(t)
    code = name.slice(REREQ_PREFIX.size...name.size)
    eval(code)
    Gen.config = nil
    t.x = Gen.x # In case setup code replaced the object.
  rescue LoadError => e
    raise StandardError, "Failed to require again #{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



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

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?
    require(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 require #{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



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

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



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

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