Class: Polygon::ContentLoader
- Inherits:
-
Object
- Object
- Polygon::ContentLoader
- Defined in:
- lib/polygon/content_loader.rb
Instance Method Summary collapse
- #enable_all! ⇒ Object
- #enable_json!(*ext) ⇒ Object
- #enable_ruby!(*ext) ⇒ Object
-
#enable_yaml!(*ext) ⇒ Object
Loaders.
- #enable_yaml_front_matter!(*ext) ⇒ Object
- #extensions ⇒ Object
- #load(file) ⇒ Object
- #register(*extensions, &loader) ⇒ Object
Instance Method Details
#enable_all! ⇒ Object
59 60 61 62 63 64 |
# File 'lib/polygon/content_loader.rb', line 59 def enable_all! enable_yaml! enable_json! enable_ruby! enable_yaml_front_matter! end |
#enable_json!(*ext) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/polygon/content_loader.rb', line 32 def enable_json!(*ext) require 'json' ext = [".json"] if ext.empty? register(*ext) do |f| JSON.load(f.read) end end |
#enable_ruby!(*ext) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/polygon/content_loader.rb', line 40 def enable_ruby!(*ext) ext = [".rb", ".ruby"] if ext.empty? register(*ext) do |f| ::Kernel.eval(f.read, TOPLEVEL_BINDING, f.to_s) end end |
#enable_yaml!(*ext) ⇒ Object
Loaders
24 25 26 27 28 29 30 |
# File 'lib/polygon/content_loader.rb', line 24 def enable_yaml!(*ext) require 'yaml' ext = [".yml", ".yaml"] if ext.empty? register(*ext) do |f| YAML.load(f.read) end end |
#enable_yaml_front_matter!(*ext) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/polygon/content_loader.rb', line 47 def enable_yaml_front_matter!(*ext) ext = [".md"] if ext.empty? register(*ext) do |f| content = f.read.force_encoding(Encoding::UTF_8) if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m YAML::load($1).merge("content" => $') else {"content" => content} end end end |
#extensions ⇒ Object
10 11 12 |
# File 'lib/polygon/content_loader.rb', line 10 def extensions loaders.map(&:first).flatten end |
#load(file) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/polygon/content_loader.rb', line 14 def load(file) file = Path(file) unless l = loader(file) raise "Unable to load #{file.basename} (unrecognized extension)" end l.call(file) end |
#register(*extensions, &loader) ⇒ Object
4 5 6 7 8 |
# File 'lib/polygon/content_loader.rb', line 4 def register(*extensions, &loader) raise ArgumentError, "Extensions expected" if extensions.empty? loaders << [extensions, loader] self end |