Class: Zenweb::Config

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#parentObject (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

#pathObject (readonly)

The path to this config’s file



25
26
27
# File 'lib/zenweb/config.rb', line 25

def path
  @path
end

#siteObject (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

#hObject

: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

#inspectObject

:nodoc:



97
98
99
100
101
102
103
104
# File 'lib/zenweb/config.rb', line 97

def inspect # :nodoc:
  if Rake.application.options.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_sObject

:nodoc:



106
107
108
# File 'lib/zenweb/config.rb', line 106

def to_s # :nodoc:
  "Config[#{path.inspect}]"
end

#wireObject

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