Class: LL::ListyList

Inherits:
Object
  • Object
show all
Defined in:
lib/ll/listy_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(cli: nil, name: nil) ⇒ ListyList

Returns a new instance of ListyList.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ll/listy_list.rb', line 3

def initialize cli: nil,
               name: nil

  @checklists = []
  @schemas    = []

  name ||= "listy-list".style :forestgreen, :bold

  @cli   = cli
  @cli ||= VV::CLI.new name: name,
                       version: LL::VERSION,
                       argv: ARGV
  self.uri = "/"

  self.first_boot
end

Instance Method Details

#await_inputObject



154
155
156
157
# File 'lib/ll/listy_list.rb', line 154

def await_input
  input = @cli.await_input message: "hi"
  input
end

#boot!Object



29
30
31
32
33
# File 'lib/ll/listy_list.rb', line 29

def boot!
  @_status = :boot
  yield
  self.load
end

#boot?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/ll/listy_list.rb', line 150

def boot?
  self.status == :boot
end

#create_directoriesObject



180
181
182
183
184
185
# File 'lib/ll/listy_list.rb', line 180

def create_directories
  %w[ checklists schemas ].each do     | _directory |
    directory = @cli.data_path.file_join _directory
    File.make_directory_if_not_exists     directory
  end
end

#displayObject



125
126
127
# File 'lib/ll/listy_list.rb', line 125

def display
  puts :display
end

#first_bootObject



20
21
22
23
24
25
26
27
# File 'lib/ll/listy_list.rb', line 20

def first_boot
  self.create_directories

  self.boot! do
    self.show_help!    if @cli.help?
    self.show_version! if @cli.version?
  end
end

#loadObject Also known as: reload



88
89
90
91
# File 'lib/ll/listy_list.rb', line 88

def load
  self.load_schemas
  self.load_checklists
end

#load_checklistsObject



98
99
100
# File 'lib/ll/listy_list.rb', line 98

def load_checklists
  @checklists = LL::Checklist.load dir: @cli.data_path
end

#load_schemasObject



94
95
96
# File 'lib/ll/listy_list.rb', line 94

def load_schemas
  @schemas = LL::Schema.load dir: @cli.data_path
end

#lockObject



110
111
112
113
114
115
116
# File 'lib/ll/listy_list.rb', line 110

def lock
  File.write(lock_path, Process.pid.to_s) unless File.exists? lock_path

  return true if lock_contents == Process.pid.to_s

  fail "Unable to lock #{lock_path}, contains pid mismatch."
end

#lock_contentsObject



106
107
108
# File 'lib/ll/listy_list.rb', line 106

def lock_contents
  File.read(lock_path).chomp
end

#lock_pathObject



102
103
104
# File 'lib/ll/listy_list.rb', line 102

def lock_path
  @cli.data_path.file_join "listy.lock"
end

#loopObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ll/listy_list.rb', line 35

def loop
  self.status = :loop unless shutdown?

  while loop? do
    self.load
    self.lock
    self.display
    self.await_input
    self.react
    self.persist
    self.unlock
  end

  self.shutdown
ensure
  self.unlock
end

#loop?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/ll/listy_list.rb', line 146

def loop?
  self.status == :loop
end

#persistObject



129
130
131
132
133
134
# File 'lib/ll/listy_list.rb', line 129

def persist
  self.save_uri
  self.save_checklist
  self.save_schema_changes
  self.sync
end

#reactObject



53
54
# File 'lib/ll/listy_list.rb', line 53

def react
end

#save_checklistObject



139
140
# File 'lib/ll/listy_list.rb', line 139

def save_checklist
end

#save_schema_changesObject



141
142
# File 'lib/ll/listy_list.rb', line 141

def save_schema_changes
end

#save_uriObject

TOOD: Implement these



137
138
# File 'lib/ll/listy_list.rb', line 137

def save_uri
end

#show_helpObject



56
57
58
59
# File 'lib/ll/listy_list.rb', line 56

def show_help
  self.uri = "/help"
  @cli.print_help
end

#show_help!Object



61
62
63
64
# File 'lib/ll/listy_list.rb', line 61

def show_help!
  self.show_help
  self.shutdown!
end

#show_versionObject



66
67
68
69
# File 'lib/ll/listy_list.rb', line 66

def show_version
  self.uri = "/version"
  @cli.print_version
end

#show_version!Object



71
72
73
74
# File 'lib/ll/listy_list.rb', line 71

def show_version!
  self.show_version
  self.shutdown!
end

#shutdownObject



76
77
78
# File 'lib/ll/listy_list.rb', line 76

def shutdown
  :shutdown_safely
end

#shutdown!Object



84
85
86
# File 'lib/ll/listy_list.rb', line 84

def shutdown!
  self.status = :shutdown
end

#shutdown?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/ll/listy_list.rb', line 80

def shutdown?
  self.status == :shutdown
end

#statusObject



176
177
178
# File 'lib/ll/listy_list.rb', line 176

def status
  @_status
end

#status=(status) ⇒ Object



168
169
170
171
172
173
174
# File 'lib/ll/listy_list.rb', line 168

def status= status
  message = \
  "Currently shutdown. Call `boot!` to return to resume."
  fail message if self.status == :shutdown
  %i[ loop boot pause shutdown ].includes! status
  @_status = status
end

#syncObject



143
144
# File 'lib/ll/listy_list.rb', line 143

def sync
end

#unlockObject



118
119
120
121
122
123
# File 'lib/ll/listy_list.rb', line 118

def unlock
  message = "Unable to unlock #{lock_path}, contains pid mismatch."
  fail message if lock_contents != Process.pid.to_s

  File.remove lock_path
end

#uriObject



164
165
166
# File 'lib/ll/listy_list.rb', line 164

def uri
  @_uri
end

#uri=(uri) ⇒ Object

Log this. Maybe incorporate into VV::CLI?



160
161
162
# File 'lib/ll/listy_list.rb', line 160

def uri= uri
  @_uri = uri
end