Class: DayOneKindle::DayOne::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/dayone-kindle/day_one.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, options = {}) ⇒ Entry

Returns a new instance of Entry.



18
19
20
21
22
# File 'lib/dayone-kindle/day_one.rb', line 18

def initialize(content, options = {})
  @content = content
  @tags = options[:tags] || []
  @created_at = options[:created_at] || Time.now
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



16
17
18
# File 'lib/dayone-kindle/day_one.rb', line 16

def content
  @content
end

#created_atObject

Returns the value of attribute created_at.



16
17
18
# File 'lib/dayone-kindle/day_one.rb', line 16

def created_at
  @created_at
end

#tagsObject

Returns the value of attribute tags.



16
17
18
# File 'lib/dayone-kindle/day_one.rb', line 16

def tags
  @tags
end

Instance Method Details

#fileObject



28
29
30
# File 'lib/dayone-kindle/day_one.rb', line 28

def file
  @file ||= File.join(DayOne::journal_location, 'entries', "#{uuid}.doentry")
end

#save!Object



61
62
63
64
# File 'lib/dayone-kindle/day_one.rb', line 61

def save!
  File.open(file, 'w') { |f| f.write(to_xml) }
  uuid
end

#to_xmlObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dayone-kindle/day_one.rb', line 32

def to_xml
  tags_xml = ''

  unless tags.empty?
    tags_xml = <<XML
  <key>Tags</key>
  <array>
  #{tags.map { |tag| "  <string>#{tag}</string>" }.join("\n  ") }
  </array>
XML
  end

  <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Creation Date</key>
  <date>#{created_at.utc.iso8601}</date>
  <key>Entry Text</key>
  <string>#{content}</string>
  #{tags_xml.strip}
  <key>UUID</key>
  <string>#{uuid}</string>
</dict>
</plist>
XML
end

#uuidObject



24
25
26
# File 'lib/dayone-kindle/day_one.rb', line 24

def uuid
  @uuid ||= `uuidgen`.gsub('-', '').strip
end