Class: Zenweb::Config
- Inherits:
-
Object
- Object
- Zenweb::Config
- Includes:
- Rake::DSL
- Defined in:
- lib/zenweb/config.rb
Overview
Provides a hierarchical dictionary made of yaml fragments and files.
Any given page in zenweb can start with a YAML header. All files named “_config.yml” up the directory tree to the top are considered parents of that config. Access a config like you would any hash and you get inherited values.
Constant Summary collapse
- UTF_BOM =
"\xEF\xBB\xBF".b
- Null =
:stopdoc:
Class.new Config do def [] k; end def key? k; end def initialize; end def inspect; "Config::Null"; end def wire; end end.new
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
The parent to this config or nil if we’re at the top level _config.yml.
-
#path ⇒ Object
readonly
The path to this config’s file.
-
#site ⇒ Object
readonly
The shared site instance.
Class Method Summary collapse
-
.split(path) ⇒ Object
Splits a file and returns the yaml header and body, as applicable.
Instance Method Summary collapse
-
#[](k) ⇒ Object
Access value at
k
. -
#h ⇒ Object
:nodoc:.
-
#initialize(site, path) ⇒ Config
constructor
Create a new Config for site at a given path.
-
#inspect ⇒ Object
:nodoc:.
- #key?(k) ⇒ Boolean
- #maybe_load_yaml(config) ⇒ Object
-
#to_s ⇒ Object
:nodoc:.
-
#wire ⇒ Object
Wire up this config to the rest of the rake dependencies.
Constructor Details
#initialize(site, path) ⇒ Config
Create a new Config for site at a given path.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/zenweb/config.rb', line 35 def initialize site, path @site, @path, @parent = site, path, nil File.each_parent path, "_config.yml" do |config| next unless File.file? config @parent = site.configs[config] unless config == path break if @parent end @parent ||= Config::Null end |
Instance Attribute Details
#parent ⇒ Object (readonly)
The parent to this config or nil if we’re at the top level _config.yml.
30 31 32 |
# File 'lib/zenweb/config.rb', line 30 def parent @parent end |
#path ⇒ Object (readonly)
The path to this config’s file
25 26 27 |
# File 'lib/zenweb/config.rb', line 25 def path @path end |
#site ⇒ Object (readonly)
The shared site instance
20 21 22 |
# File 'lib/zenweb/config.rb', line 20 def site @site end |
Class Method Details
.split(path) ⇒ Object
Splits a file and returns the yaml header and body, as applicable.
split("_config.yml") => [config, nil]
split("blah.txt") => [nil, content]
split("index.html.md") => [config, content]
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/zenweb/config.rb', line 67 def self.split path body, yaml_file = nil, false if String === path and File.file? path body = File.binread path raise ArgumentError, "UTF BOM not supported: #{path}" if body.start_with? UTF_BOM yaml_file = File.extname(path) == ".yml" body.force_encoding Encoding::UTF_8 else body = path.content end if yaml_file then [body, nil] elsif body.start_with? "---" then body.split(/^\.\.\.$/, 2) else [nil, body.valid_encoding? ? body : body.force_encoding('ASCII-8BIT')] end end |
Instance Method Details
#[](k) ⇒ Object
Access value at k
. The value can be inherited from the parent configs.
50 51 52 |
# File 'lib/zenweb/config.rb', line 50 def [] k h.key?(k.to_s) ? h[k.to_s] : parent[k] end |
#h ⇒ Object
:nodoc:
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/zenweb/config.rb', line 92 def h # :nodoc: @h ||= begin thing = File.file?(path) ? path : site.pages[path] config, _ = self.class.split thing maybe_load_yaml(config) || {} end rescue => e warn "#{self.path}: #{e}" raise end |
#inspect ⇒ Object
:nodoc:
113 114 115 116 117 118 119 120 |
# File 'lib/zenweb/config.rb', line 113 def inspect # :nodoc: if Rake.application..trace then hash = h.sort.map { |k,v| "#{k.inspect} => #{v.inspect}" }.join ", " "Config[#{path.inspect}, #{parent.inspect}, #{hash}]" else "Config[#{path.inspect}, #{parent.inspect}]" end end |
#key?(k) ⇒ Boolean
54 55 56 |
# File 'lib/zenweb/config.rb', line 54 def key? k h.key?(k.to_s) or parent.key?(k) end |
#maybe_load_yaml(config) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/zenweb/config.rb', line 103 def maybe_load_yaml config if config then if YAML.respond_to? :safe_load_file then YAML.safe_load config, permitted_classes: [Time] else YAML.load config end end end |
#to_s ⇒ Object
:nodoc:
122 123 124 |
# File 'lib/zenweb/config.rb', line 122 def to_s # :nodoc: "Config[#{path.inspect}]" end |
#wire ⇒ Object
Wire up this config to the rest of the rake dependencies.
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/zenweb/config.rb', line 129 def wire @wired ||= false # HACK return if @wired @wired = true file self.path file self.path => self.parent.path if self.parent.path # HACK self.parent.wire end |