class Diary::Entry
Attributes
body[RW]
date_key[RW]
day[RW]
link[RW]
time[RW]
title[RW]
Public Class Methods
from_hash(record)
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 24 def self.from_hash(record) entry = self.new( day: record['day'], time: record['time'], body: record['body'], title: record['title'], date_key: record['date_key'], ) # taggings! begin tag_ids = select_values('select tag_id from taggings where entry_id = ?', [entry.identifier]) bind_hold = tag_ids.map {|_| '?'}.join(',') entry.tags = select_values("select name from tags where rowid in (#{ bind_hold })", *tag_ids) rescue => ex Diary.debug "FAILED TO LOAD TAGS. #{ ex.message }" end entry end
generate(options={})
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 49 def self.generate(options={}) options[:last_update_at] = connection.execute("select max(updated_at) from #{table_name}")[0] || '' # convert Arrays to dumb CSV options.each do |(k, v)| if v.is_a?(Array) options[k] = v.join(', ') end end TEMPLATE % options end
keygen(day, time)
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 45 def self.keygen(day, time) "%s-%s" % [day, time] end
new(options={})
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 62 def initialize(options={}) @day = options[:day] @time = options[:time] @tags = options[:tags] || [] @body = options[:body] @title = options[:title] if options[:date_key].nil? @date_key = identifier else @date_key = options[:date_key] end end
table_name()
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 20 def self.table_name 'entries' end
Public Instance Methods
formatted_body()
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 80 def formatted_body RDiscount.new(body).to_html end
identifier()
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 76 def identifier self.class.keygen(day, time) end
save!()
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 107 def save! if self.class.find(date_key: date_key) # update record sql = "UPDATE entries SET day=?, time=?, body=?, link=?, title=?, updated_at=#{timestamp_sql} WHERE date_key=?" self.class.execute(sql, day, time, body, link, title, date_key) else # insert sql = %[INSERT INTO entries (day, time, body, link, title, date_key, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, #{timestamp_sql}, #{timestamp_sql})] self.class.execute(sql, day, time, body, link, title, date_key) end begin update_tags! rescue => ex Diary.debug "FAILED TO UPDATE TAGS #{ tags.inspect }. #{ ex.message }" end end
summary()
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 92 def summary "%-24s%-46s%s" % [date_key, truncated_body, tags.join(', ')] end
to_hash()
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 96 def to_hash { day: day, time: time, tags: tags, body: body, title: title, date_key: date_key, } end
truncated_body()
click to toggle source
# File lib/diary-ruby/models/entry.rb, line 84 def truncated_body _truncated = body if _truncated.size > 40 _truncated = "#{ _truncated[0..40] }..." end _truncated end