Class: Trackstar::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/trackstar/post.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Post

Returns a new instance of Post.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/trackstar/post.rb', line 6

def fields
  @fields
end

#formattingObject (readonly)

Returns the value of attribute formatting.



6
7
8
# File 'lib/trackstar/post.rb', line 6

def formatting
  @formatting
end

#valuesObject

Returns the value of attribute values.



5
6
7
# File 'lib/trackstar/post.rb', line 5

def values
  @values
end

Instance Method Details

#file_nameObject



30
31
32
# File 'lib/trackstar/post.rb', line 30

def file_name
  "#{@values[:timestamp]}-#{subject_stub}.md"
end

#persist!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#previewObject



24
25
26
27
28
# File 'lib/trackstar/post.rb', line 24

def preview
  @values.each do |key, value|
    puts "#{key}: #{value}"
  end
end

#subject_stubObject



34
35
36
# File 'lib/trackstar/post.rb', line 34

def subject_stub
  Trackstar::LogHelper.stubify(@values[:subject])
end