class Zenweb::Config

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.

Constants

Null
UTF_BOM

Attributes

parent[R]

The parent to this config or nil if we’re at the top level _config.yml.

path[R]

The path to this config’s file

site[R]

The shared site instance

Public Class Methods

new(site, path) click to toggle source

Create a new Config for site at a given path.

# 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
split(path) click to toggle source

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]
# 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

Public Instance Methods

[](k) click to toggle source

Access value at k. The value can be inherited from the parent configs.

# File lib/zenweb/config.rb, line 50
def [] k
  h.key?(k.to_s) ? h[k.to_s] : parent[k]
end
key?(k) click to toggle source
# File lib/zenweb/config.rb, line 54
def key? k
  h.key?(k.to_s) or parent.key?(k)
end
maybe_load_yaml(config) click to toggle source
# 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
wire() click to toggle source

Wire up this config to the rest of the rake dependencies.

# 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