Module: GreenHat::ThingHistory

Defined in:
lib/greenhat/thing/history.rb

Overview

Helper for Remembering what files were

Class Method Summary collapse

Class Method Details

.add(name, type) ⇒ Object



36
37
38
39
40
# File 'lib/greenhat/thing/history.rb', line 36

def self.add(name, type)
  files[name] = { type: type, time: expire }

  write
end

.expireObject

Date to Expire



43
44
45
# File 'lib/greenhat/thing/history.rb', line 43

def self.expire
  2.weeks.from_now.to_i
end

.fileObject



23
24
25
# File 'lib/greenhat/thing/history.rb', line 23

def self.file
  Settings.history_file
end

.filesObject



4
5
6
7
8
# File 'lib/greenhat/thing/history.rb', line 4

def self.files
  @files ||= read

  @files
end

.match(name) ⇒ Object



32
33
34
# File 'lib/greenhat/thing/history.rb', line 32

def self.match(name)
  files.dig(name.to_sym, :type)
end

.match?(name) ⇒ Boolean

Initial Entry Point

Returns:

  • (Boolean)


28
29
30
# File 'lib/greenhat/thing/history.rb', line 28

def self.match?(name)
  files.key? name.to_sym
end

.readObject

Read File / Remove Old Entries



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/greenhat/thing/history.rb', line 11

def self.read
  if File.exist? file
    results = Oj.load File.read(file)

    results.reject { |_k, v| Time.at(v.time) < Time.now }
  else
    {}
  end
ensure
  {}
end

.writeObject



47
48
49
# File 'lib/greenhat/thing/history.rb', line 47

def self.write
  File.write(file, Oj.dump(files))
end