class TextUtils::Page

fix:

add some unit tests!!!!!!!!!!!!!!!!!

Public Class Methods

create( path, opts={} ) { |page| ... } click to toggle source
# File lib/textutils/page.rb, line 31
def self.create( path, opts={} )
  ## todo: check if 'w' is good enough?? do NOT need to add +
  page = self.new( path, 'w+', opts )
  yield( page )
  page.close
end
new( path, mode, opts={} ) click to toggle source
# File lib/textutils/page.rb, line 46
def initialize( path, mode, opts={} )
  ## check if folders exists? if not create folder in path
  FileUtils.mkdir_p( File.dirname(path) )

  @file = File.new( path, mode )

  ## add frontmatter if passed in
  ## todo: assert check if mode = 'w' and NOT 'a' !!!
  @file.write render_frontmatter( opts[:frontmatter] )  if opts[:frontmatter]
end
open( path, mode, opts={} ) { |page| ... } click to toggle source

convenience helper; use like:

Page.open() do |page|
   page.write( text )
   page.write( text )
end
# File lib/textutils/page.rb, line 25
def self.open( path, mode, opts={} )
  page = self.new( path, mode, opts )
  yield( page )
  page.close
end
update( path, opts={} ) { |page| ... } click to toggle source
# File lib/textutils/page.rb, line 38
def self.update( path, opts={} )
  ## todo: check if 'a' is good enough?? do NOT need to add +
  page = self.new( path, 'a+', opts )
  yield( page )
  page.close
end

Public Instance Methods

close() click to toggle source
# File lib/textutils/page.rb, line 61
def close
  @file.close
end
write( text ) click to toggle source
# File lib/textutils/page.rb, line 57
def write( text )
  @file.write( text )
end

Private Instance Methods

render_frontmatter( h ) click to toggle source

helpers

- make public for reuse !!!!!
# File lib/textutils/page.rb, line 71
def render_frontmatter( h )
  buf = ''
  buf += "---\n"

  h.each do |key,value|
    buf += "#{key}: #{value}\n"
  end

  buf += "---\n\n"
  buf
end