Class: Diary::Entry
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#date_key ⇒ Object
Returns the value of attribute date_key.
-
#day ⇒ Object
Returns the value of attribute day.
-
#link ⇒ Object
Returns the value of attribute link.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#time ⇒ Object
Returns the value of attribute time.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
- .from_hash(record) ⇒ Object
- .generate(options = {}) ⇒ Object
- .keygen(day, time) ⇒ Object
- .table_name ⇒ Object
Instance Method Summary collapse
- #formatted_body ⇒ Object
- #identifier ⇒ Object
-
#initialize(options = {}) ⇒ Entry
constructor
A new instance of Entry.
- #save! ⇒ Object
- #summary ⇒ Object
- #to_hash ⇒ Object
- #truncated_body ⇒ Object
- #update_tags! ⇒ Object
Methods inherited from Model
connection, connection=, execute, select_rows, select_value, select_values
Methods included from ModelQuery
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(={}) @day = [:day] @time = [:time] @tags = [:tags] || [] @body = [:body] @title = [:title] if [:date_key].nil? @date_key = identifier else @date_key = [:date_key] end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
18 19 20 |
# File 'lib/diary-ruby/models/entry.rb', line 18 def body @body end |
#date_key ⇒ Object
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 |
#day ⇒ Object
Returns the value of attribute day.
18 19 20 |
# File 'lib/diary-ruby/models/entry.rb', line 18 def day @day end |
#link ⇒ Object
Returns the value of attribute link.
18 19 20 |
# File 'lib/diary-ruby/models/entry.rb', line 18 def link @link end |
#tags ⇒ Object
Returns the value of attribute tags.
18 19 20 |
# File 'lib/diary-ruby/models/entry.rb', line 18 def @tags end |
#time ⇒ Object
Returns the value of attribute time.
18 19 20 |
# File 'lib/diary-ruby/models/entry.rb', line 18 def time @time end |
#title ⇒ Object
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. = select_values("select name from tags where rowid in (#{ bind_hold })", *tag_ids) rescue => ex Diary.debug "FAILED TO LOAD TAGS. #{ ex. }" 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(={}) [:last_update_at] = connection.execute("select max(updated_at) from #{table_name}")[0] || '' # convert Arrays to dumb CSV .each do |(k, v)| if v.is_a?(Array) [k] = v.join(', ') end end TEMPLATE % 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_name ⇒ Object
20 21 22 |
# File 'lib/diary-ruby/models/entry.rb', line 20 def self.table_name 'entries' end |
Instance Method Details
#formatted_body ⇒ Object
80 81 82 |
# File 'lib/diary-ruby/models/entry.rb', line 80 def formatted_body RDiscount.new(body).to_html end |
#identifier ⇒ Object
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=#{} 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 (?, ?, ?, ?, ?, ?, #{}, #{})] self.class.execute(sql, day, time, body, link, title, date_key) end begin rescue => ex Diary.debug "FAILED TO UPDATE TAGS #{ .inspect }. #{ ex. }" end end |
#summary ⇒ Object
92 93 94 |
# File 'lib/diary-ruby/models/entry.rb', line 92 def summary "%-24s%-46s%s" % [date_key, truncated_body, .join(', ')] end |
#to_hash ⇒ Object
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: , body: body, title: title, date_key: date_key, } end |
#truncated_body ⇒ Object
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 # clean out existing Diary.debug "CLEANING `taggings`" self.class.execute('delete from taggings where entry_id = ?', [identifier]) # add back .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 |