module FlightConfig::Core

Constants

PLACEHOLDER

Attributes

__inputs__[R]
__read_mode__[R]

Public Class Methods

included(base) click to toggle source
# File lib/flight_config/core.rb, line 37
def self.included(base)
  base.extend(ClassMethods)
end
lock(obj) { || ... } click to toggle source
# File lib/flight_config/core.rb, line 57
def self.lock(obj)
  placeholder = false
  io = nil
  unless File.exists?(obj.path)
    Core.log(obj, 'placeholder')
    placeholder = true
    FileUtils.mkdir_p(File.dirname(obj.path))
    File.write(obj.path, PLACEHOLDER)
  end
  begin
    io = File.open(obj.path, 'r+')
    Timeout.timeout(0.1) { io.flock(File::LOCK_EX) }
  rescue Timeout::Error
    raise ResourceBusy, "The following resource is busy: #{obj.path}"
  end
  yield if block_given?
ensure
  io&.close
  if placeholder && File.read(obj.path) == PLACEHOLDER
    FileUtils.rm_f(obj.path)
    Core.log(obj, 'placeholder (removed)')
  end
end
log(obj, action) click to toggle source
# File lib/flight_config/core.rb, line 41
def self.log(obj, action)
  Log.info "#{action}: #{obj.path}"
end
new(*input_args, registry: nil, read_mode: nil) click to toggle source
# File lib/flight_config/core.rb, line 126
def initialize(*input_args, registry: nil, read_mode: nil)
  @__inputs__ = input_args
  # TODO: Make this @path = self.class.path(*__inputs__)
  self.path # Ensure the path can be established with the __inputs__
  @__registry__ = registry
  @__read_mode__ = read_mode
end
read(obj) click to toggle source

Deprecated: The mode can be switched directly on the object

# File lib/flight_config/core.rb, line 46
def self.read(obj)
  obj.instance_variable_set(:@__read_mode__, true)
  obj.instance_variable_set(:@__data__, nil)
  obj.__data__
end
write(obj) click to toggle source
# File lib/flight_config/core.rb, line 52
def self.write(obj)
  FileUtils.mkdir_p(File.dirname(obj.path))
  obj.__data__.write(obj.path, force: true)
end

Public Instance Methods

__data__() click to toggle source
# File lib/flight_config/core.rb, line 141
def __data__
  @__data__ ||= self.class.data_core.tap do |core|
    if __read_mode__ && File.exists?(path)
      Core.log(self, 'read')
      str = File.read(path)
      yaml_h = (str == Core::PLACEHOLDER ? nil : YAML.load(str))
      core.merge(yaml_h) if yaml_h
    elsif __read_mode__ && self.class.allow_missing_read(fetch: true)
      Core.log(self, 'missing (skip read)')
    elsif __read_mode__
      raise MissingFile, "The file does not exist: #{path}"
    else
      __data__initialize(core)
    end
  end
end
__data__initialize(_tty_config) click to toggle source
# File lib/flight_config/core.rb, line 138
def __data__initialize(_tty_config)
end
__registry__() click to toggle source
# File lib/flight_config/core.rb, line 134
def __registry__
  @__registry__ ||= FlightConfig::Registry.new
end
path() click to toggle source

TODO: Eventually remove the error section as all the configs will have a class path method TODO: Set the path in initialize

# File lib/flight_config/core.rb, line 161
    def path
      @path ||= begin
        if self.class.respond_to?(:path)
          self.class.path(*__inputs__)
        else
          raise FlightConfigError, <<~ERROR.chomp
            #{self.class}.path has not been defined!
          ERROR
        end
      end
    end