Module: Loaders
- Defined in:
- lib/loaders.rb
Overview
Original loader functions. These are accessible via Gen.loaders. New loaders should be added there.
Constant Summary collapse
- GEM_PREFIX =
'gem:'- RUBY_EXT =
'.rb'- YAML_PREFIX =
'yaml:'- YAML_EXTS =
[ '.yaml', '.yml' ]
- BIN_PREFIX =
'bin:'
Class Method Summary collapse
- .bin_loader(name) ⇒ Object
- .document ⇒ Object
- .gem_loader(name) ⇒ Object
- .loaders ⇒ Object
- .ruby_loader(name) ⇒ Object
- .yaml_loader(name) ⇒ Object
Class Method Details
.bin_loader(name) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/loaders.rb', line 67 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 = IO.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.to_s}" rescue Exception => e raise StandardError, "Failed to read #{f}\n#{e.to_s}" end |
.document ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/loaders.rb', line 86 def self.document %( - #{Loaders::GEM_PREFIX}gem_name : requires the gem. - 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. ) end |
.gem_loader(name) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/loaders.rb', line 13 def self.gem_loader(name) return false unless name.downcase.start_with?(GEM_PREFIX) begin require(name.slice(GEM_PREFIX.size...name.size)) rescue LoadError => e raise StandardError, "Failed to require #{name}\n#{e.to_s}" rescue Exception => e raise StandardError, "Problem with #{name}\n#{e.to_s}" end true end |
.loaders ⇒ Object
81 82 83 84 |
# File 'lib/loaders.rb', line 81 def self.loaders pre = @preloaders [ method(:gem_loader), method(:ruby_loader), method(:yaml_loader), method(:bin_loader) ] end |
.ruby_loader(name) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/loaders.rb', line 27 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 require(File.join(Dir.pwd, File.basename(name))) rescue LoadError => e raise StandardError, "Failed to require #{name}\n#{e.to_s}" rescue Exception => e raise StandardError, "Problem with #{name}\n#{e.to_s}" end Dir.chdir(origwd) unless d == '.' true end |
.yaml_loader(name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/loaders.rb', line 46 def self.yaml_loader(name) d = name.downcase if d.start_with?(YAML_PREFIX) name = name.slice(YAML_PREFIX.size...name.size) else return false if (YAML_EXTS.index { |s| d.end_with?(s) }).nil? 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.read(f)) raise StandardError, "#{name} #{n} exists already." unless Gen.d.add(n, doc) true rescue Errno::ENOENT raise StandardError, "Not found: #{f}\n#{e.to_s}" rescue Exception => e raise StandardError, "Failed to read as YAML: #{f}\n#{e.to_s}" end |