class Trackstar::Post

Attributes

fields[R]
formatting[R]
values[RW]

Public Class Methods

new(path = nil) click to toggle source
# File lib/trackstar/post.rb, line 8
def initialize(path = nil)
  @log = Trackstar::Log.new
  @fields = @log.fields
  @formatting = @log.formatting
  @values = {}
  
  if path
    load_from_path(path)
    # note this doesn't load the text of the post
  else
    now = Time.now
    @values[:timestamp] = now.to_i
    @values[:date] = date_time_format(now)
  end
end

Public Instance Methods

file_name() click to toggle source
# File lib/trackstar/post.rb, line 30
def file_name
  "#{@values[:timestamp]}-#{subject_stub}.md"
end
persist!() click to toggle source
# File lib/trackstar/post.rb, line 38
def persist!
  post_file_path = "#{Trackstar::LogHelper::POSTS_DIR}/#{file_name}"
  File.open(post_file_path, 'w') do |post_file|
    post_file.puts "date: #{@values[:date]}"

    fields.keys.each do |field_name|
      post_file.puts "#{field_name.to_s}: #{values[field_name]}"
      if formatting.include? field_name
        self.send(formatting[field_name], post_file)
      end
    end
  end
  post_file_path
end
preview() click to toggle source
# File lib/trackstar/post.rb, line 24
def preview
  @values.each do |key, value|
    puts "#{key}: #{value}"
  end
end
subject_stub() click to toggle source
# File lib/trackstar/post.rb, line 34
def subject_stub
  Trackstar::LogHelper.stubify(@values[:subject])
end

Private Instance Methods

date_time_format(timestamp) click to toggle source
# File lib/trackstar/post.rb, line 69
def date_time_format(timestamp)
  timestamp.strftime("%b %e %Y %l:%M %P")
end
hr_after(file_handle) click to toggle source

persisted post formatting methods

# File lib/trackstar/post.rb, line 75
def hr_after(file_handle)
  file_handle.puts "-" * 20
end
load_from_path(path) click to toggle source
# File lib/trackstar/post.rb, line 55
def load_from_path(path)
  pn = Pathname.new(path)
  @values[:timestamp] = pn.basename.to_s.split('-').first
  File.readlines(path).each do |line|
    break if line[0] == "-"
    line.gsub!(/\n?/, "")
    split_line = line.split(": ")
    key = split_line.first
    value = split_line.last
    @values[key.to_sym] = value
  end
  
end