Class: Ellipses::Client::MetaFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ellipses/client/meta_file.rb

Constant Summary collapse

FILES =
%w[.local/etc/src.lock src.lock .src.lock].freeze
EMPTY =
"[]\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetaFile

Returns a new instance of MetaFile.



24
25
26
# File 'lib/ellipses/client/meta_file.rb', line 24

def initialize
  @directory, @file = Support.search_path FILES
end

Instance Attribute Details

#alternativesObject (readonly)

Returns the value of attribute alternatives.



22
23
24
# File 'lib/ellipses/client/meta_file.rb', line 22

def alternatives
  @alternatives
end

#directoryObject (readonly)

Returns the value of attribute directory.



22
23
24
# File 'lib/ellipses/client/meta_file.rb', line 22

def directory
  @directory
end

Class Method Details

.create(directory) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/ellipses/client/meta_file.rb', line 11

def self.create(directory)
  Support.dir!(directory)

  selected = FILES.find { |path| ::Dir.exist? File.join(directory, ::File.dirname(path)) }
  file     = ::File.join(directory, selected)

  ::File.write(file, EMPTY) unless ::File.exist? file

  new
end

Instance Method Details

#fileObject



28
29
30
31
# File 'lib/ellipses/client/meta_file.rb', line 28

def file
  loaded!
  @file
end

#loaded!Object



37
38
39
40
41
# File 'lib/ellipses/client/meta_file.rb', line 37

def loaded!
  raise 'Lockfile not located' if @file.nil?

  raise "Lockfile not found: #{@file}" unless ::File.exist? @file
end

#loaded?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ellipses/client/meta_file.rb', line 33

def loaded?
  @file && ::File.exist?(@file)
end

#readObject



43
44
45
# File 'lib/ellipses/client/meta_file.rb', line 43

def read
  Meta.from_array JSON.load_file(file, symbolize_names: true)
end

#to_sObject



51
52
53
# File 'lib/ellipses/client/meta_file.rb', line 51

def to_s
  file
end

#write(meta) ⇒ Object



47
48
49
# File 'lib/ellipses/client/meta_file.rb', line 47

def write(meta)
  Support.updatelines(file, meta.empty? ? EMPTY : JSON.pretty_generate(meta))
end