Module: Magneto::Readable

Included in:
Item, Template
Defined in:
lib/magneto/readable.rb

Instance Method Summary collapse

Instance Method Details

#contentObject Also known as: to_s



22
23
24
25
# File 'lib/magneto/readable.rb', line 22

def content
  read if @content.nil?
  @content || ''
end

#content=(content) ⇒ Object



29
30
31
# File 'lib/magneto/readable.rb', line 29

def content=(content)
  @content = content || ''
end

#content?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/magneto/readable.rb', line 18

def content?
  not @content.nil? || @content.empty?
end

#metadataObject



9
10
11
12
# File 'lib/magneto/readable.rb', line 9

def 
  read if @metadata.nil?
  @metadata || {}
end

#metadata=(metadata) ⇒ Object



14
15
16
# File 'lib/magneto/readable.rb', line 14

def metadata=()
  @metadata =  || {}
end

#metadata?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/magneto/readable.rb', line 5

def metadata?
  not @metadata.nil? || @metadata.empty?
end

#readObject

Adapted from ‘jekyll/convertible.rb` with thanks to Tom Preston-Werner.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/magneto/readable.rb', line 34

def read
  @metadata = {}
  @content = File.read(path)

  if @content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
    @content = $POSTMATCH

    begin
      @metadata = YAML.load($1)
      raise unless @metadata.is_a? Hash
    rescue => ex
      $stderr.puts "#{File.basename($PROGRAM_NAME)}: #{ex.to_s}"
      $stderr.puts "WARNING: Couldn't load metadata."
      @metadata = {}
    end

    @metadata.symbolize_keys!
    
  end
end