Class: Embargoed::MaintenanceFile
- Inherits:
-
Object
- Object
- Embargoed::MaintenanceFile
- Defined in:
- lib/embargoed/maintenance_file.rb
Constant Summary collapse
- SETTINGS =
[:reason, :allowed_paths, :allowed_ips, :response_code, :retry_after]
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .default ⇒ Object
-
.find ⇒ Object
Find the first MaintenanceFile that exists.
- .named(name) ⇒ Object
Instance Method Summary collapse
- #delete ⇒ Object
- #exists? ⇒ Boolean
- #import(hash) ⇒ Object (also: #import_env_vars)
-
#initialize(path) ⇒ MaintenanceFile
constructor
A new instance of MaintenanceFile.
- #to_h ⇒ Object
- #to_yaml(key_mapper = :to_s) ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(path) ⇒ MaintenanceFile
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/embargoed/maintenance_file.rb', line 11 def initialize(path) @path = path @reason = .config.default_reason @allowed_paths = .config.default_allowed_paths @allowed_ips = .config.default_allowed_ips @response_code = .config.default_response_code @retry_after = .config.default_retry_after import_yaml if exists? end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/embargoed/maintenance_file.rb', line 6 def path @path end |
Class Method Details
.default ⇒ Object
70 71 72 |
# File 'lib/embargoed/maintenance_file.rb', line 70 def self.default self.new(named_paths.values.first) end |
.find ⇒ Object
Find the first MaintenanceFile that exists
60 61 62 63 |
# File 'lib/embargoed/maintenance_file.rb', line 60 def self.find path = named_paths.values.find { |p| File.exist? p } self.new(path) if path end |
.named(name) ⇒ Object
65 66 67 68 |
# File 'lib/embargoed/maintenance_file.rb', line 65 def self.named(name) path = named_paths[name.to_sym] self.new(path) unless path.nil? end |
Instance Method Details
#delete ⇒ Object
46 47 48 |
# File 'lib/embargoed/maintenance_file.rb', line 46 def delete File.delete(path) if exists? end |
#exists? ⇒ Boolean
22 23 24 |
# File 'lib/embargoed/maintenance_file.rb', line 22 def exists? File.exist? path end |
#import(hash) ⇒ Object Also known as: import_env_vars
50 51 52 53 54 55 56 |
# File 'lib/embargoed/maintenance_file.rb', line 50 def import(hash) SETTINGS.map(&:to_s).each do |att| self.send(:"#{att}=", hash[att]) unless hash[att].nil? end true end |
#to_h ⇒ Object
26 27 28 29 30 |
# File 'lib/embargoed/maintenance_file.rb', line 26 def to_h SETTINGS.each_with_object({}) do |att, hash| hash[att] = send(att) end end |
#to_yaml(key_mapper = :to_s) ⇒ Object
32 33 34 35 36 |
# File 'lib/embargoed/maintenance_file.rb', line 32 def to_yaml(key_mapper = :to_s) to_h.each_with_object({}) { |(key, val), hash| hash[key.send(key_mapper)] = val }.to_yaml end |
#write ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/embargoed/maintenance_file.rb', line 38 def write FileUtils.mkdir_p(dir_path) unless Dir.exist? dir_path File.open(path, 'w') do |file| file.write to_yaml end end |