Class: Entry

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date, yornal) ⇒ Entry

Returns a new instance of Entry.



16
17
18
19
# File 'lib/entry.rb', line 16

def initialize(date, yornal)
  @date = date.is_a?(Time) ? date.to_a[..5].drop_while { |i| i == 0 }.reverse.join("/") : date
  @yornal = yornal.is_a?(Yornal) ? yornal : Yornal.new(yornal)
end

Instance Attribute Details

#dateObject (readonly)

pseudo date, yornal (obj or name)



3
4
5
# File 'lib/entry.rb', line 3

def date
  @date
end

#yornalObject (readonly)

pseudo date, yornal (obj or name)



3
4
5
# File 'lib/entry.rb', line 3

def yornal
  @yornal
end

Class Method Details

.fromPath(path) ⇒ Object



9
10
11
12
13
14
# File 'lib/entry.rb', line 9

def self.fromPath(path)
  path[$yornalPath.size..].stlip("/")
    .partition { |x| x =~ /^\d+$/ }
    .map { |a| a.join("/") }
    .then { |date, yornal| Entry.new date, yornal }
end

Instance Method Details

#<=>(other) ⇒ Object



5
6
7
# File 'lib/entry.rb', line 5

def <=>(other)
  to_t <=> (other.is_a?(Entry) ? other.to_t : other)
end

#contains?(word) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/entry.rb', line 33

def contains?(word)
  File.read(path) =~ Regexp.new(word, :i)
end

#delete(ask = true) ⇒ Object



52
53
54
55
56
# File 'lib/entry.rb', line 52

def delete(ask = true)
  pre = "You are about to delete yornal entry '#{name}'."
  question = "Are you sure you want to delete it?"
  git(:rm, "#{path}") if !ask || yes_or_no?(question, pre)
end

#edit(editor = editor(), action = :Modify, ignore = nil) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/entry.rb', line 43

def edit(editor = editor(), action = :Modify, ignore = nil)
  digest = SHA256.digest(File.read(path))
  system "#{editor} #{path}"
  return if ignore || digest == SHA256.digest(File.read(path))

  git(:add, path)
  git(:commit, "-m '#{action} #{@yornal.name} entry #{@date}'")
end

#matches?(regex) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/entry.rb', line 37

def matches?(regex)
  File.read(path) =~ Regexp.new(regex)
rescue RegexpError
  err "Malformed regexp"
end

#nameObject



25
26
27
# File 'lib/entry.rb', line 25

def name
  [@yornal.name, @date].jomp("/")
end

#pathObject



21
22
23
# File 'lib/entry.rb', line 21

def path
  [$yornalPath, name].jomp("/")
end

#printout(delimiter = "\n\n") ⇒ Object



58
59
60
61
# File 'lib/entry.rb', line 58

def printout(delimiter = "\n\n")
  $stdout.print File.read(path)
  $stdout.print delimiter
end

#printpath(delimiter = "\n", fullpath) ⇒ Object



63
64
65
66
# File 'lib/entry.rb', line 63

def printpath(delimiter = "\n", fullpath)
  $stdout.print(fullpath ? path : name)
  $stdout.print delimiter
end

#to_tObject



29
30
31
# File 'lib/entry.rb', line 29

def to_t
  Time.new(*@date.split("/"))
end