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"
- Null =
:stopdoc:
Class.new Config do def [] 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:.
-
#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]
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/zenweb/config.rb', line 64 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 "utf-8" if File::RUBY19 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[k.to_s] or parent[k] end |
#h ⇒ Object
:nodoc:
89 90 91 92 93 94 95 |
# File 'lib/zenweb/config.rb', line 89 def h # :nodoc: @h ||= begin thing = File.file?(path) ? path : site.pages[path] config, _ = self.class.split thing config && YAML.load(config) || {} end end |
#inspect ⇒ Object
:nodoc:
97 98 99 100 101 102 103 104 |
# File 'lib/zenweb/config.rb', line 97 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 |
#to_s ⇒ Object
:nodoc:
106 107 108 |
# File 'lib/zenweb/config.rb', line 106 def to_s # :nodoc: "Config[#{path.inspect}]" end |
#wire ⇒ Object
Wire up this config to the rest of the rake dependencies.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/zenweb/config.rb', line 113 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 |