Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc2/base/core_ext/hash.rb,
lib/nanoc2/extra/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#cleanObject

Cleans up the hash and returns the result. It performs the following operations:

  • Values with keys ending in _at and _on are converted into Time and and Date objects, respectively

  • All keys are converted to symbols

  • Value strings ‘true’, ‘false’ and ‘none’ are converted into true, false and nil, respectively

Hashes are cleaned recursively, so the value of a hash pair will also be cleaned if the value is a hash.

For example, the following hash:

{
  'foo'       => 'bar',
  :created_on => '2008-05-19',
  :layout     => 'none'
}

will be converted into:

{
  :foo        => 'bar',
  :created_on => Date.parse('2008-05-19'),
  :layout     => nil
}


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nanoc2/base/core_ext/hash.rb', line 32

def clean
  inject({}) do |hash, (key, value)|
    real_key = key.to_s

    if real_key =~ /_on$/ and value.is_a?(String)
      hash.merge(key.to_sym => Date.parse(value))
    elsif real_key =~ /_at$/ and value.is_a?(String)
      hash.merge(key.to_sym => Time.parse(value.to_s))
    elsif value == 'true'
      hash.merge(key.to_sym => true)
    elsif value == 'false'
      hash.merge(key.to_sym => false)
    elsif value == 'none'
      hash.merge(key.to_sym => nil)
    elsif value.is_a?(Hash)
      hash.merge(key.to_sym => value.clean)
    else
      hash.merge(key.to_sym => value)
    end
  end
end

#stringify_keysObject

Returns the hash where all keys are converted to strings. Hash keys are converted to strings recursively, so the keys of a value of a hash pair will also be converted to strings if the value is a hash.

For example, the following hash:

{
  'foo' => 'bar',
  :baz  => 'quux'
}

will be converted into:

{
  :foo  => 'bar',
  :baz  => 'quux'
}


72
73
74
75
76
# File 'lib/nanoc2/base/core_ext/hash.rb', line 72

def stringify_keys
  inject({}) do |hash, (key, value)|
    hash.merge(key.to_s => value.is_a?(Hash) ? value.stringify_keys : value)
  end
end

#to_split_yamlObject

Converts this hash into YAML format, splitting the YAML output into a ‘builtin’ and a ‘custom’ section. A key that is present in Nanoc2::Page::DEFAULTS will be considered a ‘default’ key; all other keys will be put in the ‘Custom’ section.

For example, the hash:

{
  :title       => 'My Cool Page',
  :filters_pre => [ 'foo', 'bar' ]
}

will be converted into:

# Built-in
filters_pre: [ 'foo', 'bar' ]

# Custom
title: 'My Cool Page'

as filters_pre is considered a ‘default’ key while title is not.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nanoc2/extra/core_ext/hash.rb', line 24

def to_split_yaml
  # Skip irrelevant keys
  hash = self.reject { |k,v| k == :file }

  # Split keys
  hashes = { :builtin => {}, :custom => {} }
  hash.each_pair do |key, value|
    kind = Nanoc2::Page::DEFAULTS.include?(key) || Nanoc2::Asset::DEFAULTS.include?(key) ? :builtin : :custom
    hashes[kind][key] = value
  end

  # Dump and clean hashes
  dumps = { :builtin => '', :custom => '' }
  [ :builtin, :custom ].each do |kind|
    if hashes[kind].keys.empty?
      dumps[kind] = "\n"
    else
      raw_dump = YAML.dump(hashes[kind].stringify_keys)
      dumps[kind] = raw_dump.split('---')[1].gsub("\n\n", "\n")
    end
  end

  # Built composite YAML file
  '# Built-in' +
  dumps[:builtin] +
  "\n" +
  '# Custom' +
  dumps[:custom]
end