Class: Diary::Entry

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

Constant Summary collapse

CURRENT_VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Entry.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/diary-ruby/entry.rb', line 52

def initialize(version, options={})
  @version = version || CURRENT_VERSION

  @day = options[:day]
  @time = options[:time]
  @tags = options[:tags] || []
  @text = options[:text]
  @title = options[:title]

  if options[:key].nil?
    @key = Entry.keygen(day, time)
  else
    @key = options[:key]
  end
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



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

def day
  @day
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

#textObject

Returns the value of attribute text.



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

def text
  @text
end

#timeObject

Returns the value of attribute time.



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

def time
  @time
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.from_store(record) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/diary-ruby/entry.rb', line 21

def self.from_store(record)
  if record[:version] == 1
    self.new(
      record[:version],
      day: record[:day],
      time: record[:time],
      tags: record[:tags],
      text: record[:text],
      title: record[:title],
      key: record[:key],
    )
  end
end

.generate(options = {}, store) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/diary-ruby/entry.rb', line 39

def self.generate(options={}, store)
  options[:last_update_at] = store.read(:last_update_at)

  # 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



35
36
37
# File 'lib/diary-ruby/entry.rb', line 35

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

Instance Method Details

#formatted_textObject



68
69
70
# File 'lib/diary-ruby/entry.rb', line 68

def formatted_text
  RDiscount.new(text).to_html
end

#to_hashObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/diary-ruby/entry.rb', line 72

def to_hash
  {
    version: version,
    day: day,
    time: time,
    tags: tags,
    text: text,
    title: '',
    key: key,
  }
end