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.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/zenweb/config.rb', line 37 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.
32 33 34 |
# File 'lib/zenweb/config.rb', line 32 def parent @parent end |
#path ⇒ Object (readonly)
The path to this config’s file
27 28 29 |
# File 'lib/zenweb/config.rb', line 27 def path @path end |
#site ⇒ Object (readonly)
The shared site instance
22 23 24 |
# File 'lib/zenweb/config.rb', line 22 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]
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/zenweb/config.rb', line 66 def self.split 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 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.
52 53 54 |
# File 'lib/zenweb/config.rb', line 52 def [] k h[k.to_s] or parent[k] end |
#h ⇒ Object
:nodoc:
86 87 88 89 90 91 |
# File 'lib/zenweb/config.rb', line 86 def h # :nodoc: @h ||= begin config, _ = self.class.split path config && YAML.load(config) || {} end end |
#inspect ⇒ Object
:nodoc:
93 94 95 96 97 98 99 100 |
# File 'lib/zenweb/config.rb', line 93 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:
102 103 104 |
# File 'lib/zenweb/config.rb', line 102 def to_s # :nodoc: "Config[#{path.inspect}]" end |
#wire ⇒ Object
Wire up this config to the rest of the rake dependencies.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/zenweb/config.rb', line 109 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 |