Module: FlightConfig::Core
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- PLACEHOLDER =
'__flight_config_placeholder__'
Instance Attribute Summary collapse
-
#__inputs__ ⇒ Object
readonly
Returns the value of attribute __inputs__.
-
#__read_mode__ ⇒ Object
readonly
Returns the value of attribute read_mode.
Class Method Summary collapse
- .included(base) ⇒ Object
- .lock(obj) ⇒ Object
- .log(obj, action) ⇒ Object
-
.read(obj) ⇒ Object
Deprecated: The mode can be switched directly on the object.
- .write(obj) ⇒ Object
Instance Method Summary collapse
- #__data__ ⇒ Object
- #__data__initialize(_tty_config) ⇒ Object
- #__registry__ ⇒ Object
- #initialize(*input_args, registry: nil, read_mode: nil) ⇒ Object
-
#path ⇒ Object
TODO: Eventually remove the error section as all the configs will have a class path method TODO: Set the path in initialize.
Instance Attribute Details
#__inputs__ ⇒ Object (readonly)
Returns the value of attribute __inputs__.
124 125 126 |
# File 'lib/flight_config/core.rb', line 124 def __inputs__ @__inputs__ end |
#__read_mode__ ⇒ Object (readonly)
Returns the value of attribute read_mode.
124 125 126 |
# File 'lib/flight_config/core.rb', line 124 def __read_mode__ @__read_mode__ end |
Class Method Details
.included(base) ⇒ Object
37 38 39 |
# File 'lib/flight_config/core.rb', line 37 def self.included(base) base.extend(ClassMethods) end |
.lock(obj) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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) ⇒ Object
41 42 43 |
# File 'lib/flight_config/core.rb', line 41 def self.log(obj, action) Log.info "#{action}: #{obj.path}" end |
.read(obj) ⇒ Object
Deprecated: The mode can be switched directly on the object
46 47 48 49 50 |
# 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) ⇒ Object
52 53 54 55 |
# 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 |
Instance Method Details
#__data__ ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# 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) ⇒ Object
138 139 |
# File 'lib/flight_config/core.rb', line 138 def __data__initialize(_tty_config) end |
#__registry__ ⇒ Object
134 135 136 |
# File 'lib/flight_config/core.rb', line 134 def __registry__ @__registry__ ||= FlightConfig::Registry.new end |
#initialize(*input_args, registry: nil, read_mode: nil) ⇒ Object
126 127 128 129 130 131 132 |
# 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 |
#path ⇒ Object
TODO: Eventually remove the error section as all the configs will have a class path method TODO: Set the path in initialize
161 162 163 164 165 166 167 168 169 170 171 |
# 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 |