Class: Forematter::Frontmatter

Inherits:
Object
  • Object
show all
Defined in:
lib/forematter/frontmatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Frontmatter

Returns a new instance of Frontmatter.



5
6
7
# File 'lib/forematter/frontmatter.rb', line 5

def initialize(input)
  init_stream(input)
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/forematter/frontmatter.rb', line 19

def [](key)
  data.children.each_index do |i|
    next unless i % 2
    return data.children[i + 1] if data.children[i].to_ruby == key
  end
  nil
end

#[]=(key, val) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/forematter/frontmatter.rb', line 27

def []=(key, val)
  data.children.each_index do |i|
    next unless i % 2
    if data.children[i].to_ruby == key
      data.children[i + 1] = thunk(val, data.children[i + 1])
      return
    end
  end

  data.children << Psych::Nodes::Scalar.new(key)
  data.children << thunk(val)
end

#delete(key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/forematter/frontmatter.rb', line 40

def delete(key)
  data.children.each_index do |i|
    next unless i % 2
    if data.children[i].to_ruby == key
      val = data.children.delete_at(i + 1)
      data.children.delete_at(i)
      return val
    end
  end
end

#key?(key) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
# File 'lib/forematter/frontmatter.rb', line 9

def key?(key)
  data.children.each_index do |i|
    next unless i % 2
    return true if data.children[i].to_ruby == key
  end

  false
end

#rename(key, new_key) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/forematter/frontmatter.rb', line 51

def rename(key, new_key)
  data.children.each_index do |i|
    next unless i % 2
    if data.children[i].to_ruby == key
      data.children[i].value = new_key
      return
    end
  end
end

#to_yamlObject



61
62
63
# File 'lib/forematter/frontmatter.rb', line 61

def to_yaml
  @stream.to_yaml
end