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



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



195
196
197
198
# File 'lib/ll/listy_list.rb', line 195

def await_input
  @input = @cli.await_input message: "listy>"
  @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



191
192
193
# File 'lib/ll/listy_list.rb', line 191

def boot?
  self.status == :boot
end

#create_directoriesObject



221
222
223
224
225
226
# File 'lib/ll/listy_list.rb', line 221

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ll/listy_list.rb', line 145

def display
  consider self.uri do

    puts self.uri

    given "/help" do
      @cli.print_help
    end

    given "/install" do
      directory = File.pwd.file_join "exe"
      @cli.install( executable_directory: directory )
    end

    given "/version" do
      @cli.print_version
    end

    given "/unknown" do
      @cli.print_error command: @input
    end

  end
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



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

def load
  self.load_schemas
  self.load_checklists
end

#load_checklistsObject



116
117
118
# File 'lib/ll/listy_list.rb', line 116

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

#load_schemasObject



112
113
114
# File 'lib/ll/listy_list.rb', line 112

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

#lockObject



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

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

ZACH WAS HERE: run ./bin/listy and hit lock error. Maybe

it was me assuming that a lock would have happened?


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

def lock_contents
  File.read(lock_path).chomp
end

#lock_pathObject



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

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



187
188
189
# File 'lib/ll/listy_list.rb', line 187

def loop?
  self.status == :loop
end

#persistObject



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

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

#reactObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ll/listy_list.rb', line 53

def react
  consider @input do

    given String.empty_string do
      self.uri = "/"
    end

    given "help" do
      self.uri = "/help"
    end

    given "install" do
      self.uri = "/install"
    end

    within %w[ version v ] do
      self.uri = "/version"
    end

    otherwise do
      self.uri = "/unknown"
    end

  end
end

#save_checklistObject



180
181
# File 'lib/ll/listy_list.rb', line 180

def save_checklist
end

#save_schema_changesObject



182
183
# File 'lib/ll/listy_list.rb', line 182

def save_schema_changes
end

#save_uriObject

TOOD: Implement these



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

def save_uri
end

#show_help!Object



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

def show_help!
  @cli.print_help
  self.shutdown!
end

#show_versionObject



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

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

#show_version!Object



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

def show_version!
  self.show_version
  self.shutdown!
end

#shutdownObject



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

def shutdown
  :shutdown_safely
end

#shutdown!Object



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

def shutdown!
  self.status = :shutdown
end

#shutdown?Boolean



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

def shutdown?
  self.status == :shutdown
end

#statusObject



217
218
219
# File 'lib/ll/listy_list.rb', line 217

def status
  @_status
end

#status=(status) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/ll/listy_list.rb', line 209

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



184
185
# File 'lib/ll/listy_list.rb', line 184

def sync
end

#unlockObject



138
139
140
141
142
143
# File 'lib/ll/listy_list.rb', line 138

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



205
206
207
# File 'lib/ll/listy_list.rb', line 205

def uri
  @_uri
end

#uri=(uri) ⇒ Object

Log this. Maybe incorporate into VV::CLI?



201
202
203
# File 'lib/ll/listy_list.rb', line 201

def uri= uri
  @_uri = uri
end