mirror of
https://github.com/fluent/fluentd-ui.git
synced 2026-05-05 10:56:11 +02:00
Extract common procedures into module.
This commit is contained in:
parent
92134654ea
commit
bb7786499b
36
app/models/concerns/fluentd/setting_archive/archivable.rb
Normal file
36
app/models/concerns/fluentd/setting_archive/archivable.rb
Normal file
@ -0,0 +1,36 @@
|
||||
class Fluentd
|
||||
module SettingArchive
|
||||
module Archivable
|
||||
attr_accessor :file_path
|
||||
|
||||
def file_id
|
||||
@file_id ||= with_file { name.gsub(/#{self.class::FILE_EXTENSION}\Z/,'') }
|
||||
end
|
||||
|
||||
def name
|
||||
@name ||= with_file { File.basename(file_path) }
|
||||
end
|
||||
|
||||
def content
|
||||
@content ||= with_file { File.open(file_path, "r") { |f| f.read } }
|
||||
end
|
||||
|
||||
def ctime
|
||||
with_file { File.ctime(file_path) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def file_path_of(dir, id)
|
||||
file_path = Pathname.new(dir).join("#{id}#{self.class::FILE_EXTENSION}")
|
||||
raise "No such a file #{file_path}" unless File.exist?(file_path)
|
||||
file_path
|
||||
end
|
||||
|
||||
def with_file
|
||||
return nil unless file_path && File.exist?(file_path)
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,41 +1,17 @@
|
||||
class Fluentd
|
||||
module SettingArchive
|
||||
class BackupFile
|
||||
attr_accessor :file_path
|
||||
include Archivable
|
||||
|
||||
FILE_EXTENSION = ".conf".freeze
|
||||
|
||||
def self.find_by_file_id(backup_dir, file_id)
|
||||
file_path = Pathname.new(backup_dir).join("#{file_id}.conf")
|
||||
raise "No such a file #{file_path}" unless File.exist?(file_path)
|
||||
|
||||
new(file_path)
|
||||
new(file_path_of(backup_dir, file_id))
|
||||
end
|
||||
|
||||
def initialize(file_path)
|
||||
@file_path = file_path
|
||||
end
|
||||
|
||||
def file_id
|
||||
@file_id ||= with_file { name.gsub(/.conf\Z/,'') }
|
||||
end
|
||||
|
||||
def name
|
||||
@name ||= with_file { File.basename(file_path) }
|
||||
end
|
||||
|
||||
def content
|
||||
@content ||= with_file { File.open(file_path, "r") { |f| f.read } }
|
||||
end
|
||||
|
||||
def ctime
|
||||
with_file { File.ctime(file_path) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with_file
|
||||
return nil unless file_path && File.exist?(file_path)
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user