Class: Diary::Entry

Inherits:
Model
  • Object
show all
Defined in:
lib/diary-ruby/models/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

connection, connection=, execute, select_rows, select_value, select_values

Methods included from ModelQuery

#timestamp_sql

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included

Constructor Details

#initialize(options = {}) ⇒ Entry

Returns a new instance of Entry.



62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



18
19
20
# File 'lib/diary-ruby/models/entry.rb', line 18

def body
  @body
end

#date_keyObject

Returns the value of attribute date_key.



18
19
20
# File 'lib/diary-ruby/models/entry.rb', line 18

def date_key
  @date_key
end

#dayObject

Returns the value of attribute day.



18
19
20
# File 'lib/diary-ruby/models/entry.rb', line 18

def day
  @day
end

Returns the value of attribute link.



18
19
20
# File 'lib/diary-ruby/models/entry.rb', line 18

def link
  @link
end

#tagsObject

Returns the value of attribute tags.



18
19
20
# File 'lib/diary-ruby/models/entry.rb', line 18

def tags
  @tags
end

#timeObject

Returns the value of attribute time.



18
19
20
# File 'lib/diary-ruby/models/entry.rb', line 18

def time
  @time
end

#titleObject

Returns the value of attribute title.



18
19
20
# File 'lib/diary-ruby/models/entry.rb', line 18

def title
  @title
end

Class Method Details

.from_hash(record) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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 = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



45
46
47
# File 'lib/diary-ruby/models/entry.rb', line 45

def self.keygen(day, time)
  "%s-%s" % [day, time]
end

.table_nameObject



20
21
22
# File 'lib/diary-ruby/models/entry.rb', line 20

def self.table_name
  'entries'
end

Instance Method Details

#formatted_bodyObject



80
81
82
# File 'lib/diary-ruby/models/entry.rb', line 80

def formatted_body
  RDiscount.new(body).to_html
end

#identifierObject



76
77
78
# File 'lib/diary-ruby/models/entry.rb', line 76

def identifier
  self.class.keygen(day, time)
end

#save!Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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

#summaryObject



92
93
94
# File 'lib/diary-ruby/models/entry.rb', line 92

def summary
  "%-24s%-46s%s" % [date_key, truncated_body, tags.join(', ')]
end

#to_hashObject



96
97
98
99
100
101
102
103
104
105
# 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_bodyObject



84
85
86
87
88
89
90
# 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

#update_tags!Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/diary-ruby/models/entry.rb', line 126

def update_tags!
  # clean out existing
  Diary.debug "CLEANING `taggings`"
  self.class.execute('delete from taggings where entry_id = ?', [identifier])

  # add back
  tags.each do |tag|
    # is tag in db?
    tag_id = self.class.select_value('select rowid from tags where name = ?', tag)

    if tag_id.nil?
      Diary.debug "CREATING TAG #{ tag.inspect }"

      # exists
      Diary.debug self.class.select_rows('PRAGMA table_info(tags)').inspect
      self.class.execute("insert into tags (name) values (?)", [tag])
      tag_id = self.class.select_value('select last_insert_rowid()')
    end

    Diary.debug "CREATING tagging"
    self.class.execute('insert into taggings (tag_id, entry_id) values (?, ?)', [tag_id, identifier])
  end
end