Class: Settings::DataSource::File
Defined Under Namespace
Modules: Defaults, Directory
Instance Attribute Summary
#source
Class Method Summary
collapse
Instance Method Summary
collapse
#initialize
Class Method Details
.build(source = nil) ⇒ Object
4
5
6
7
8
9
|
# File 'lib/settings/data_source/file.rb', line 4
def self.build(source=nil)
canonical = canonical(source)
validate(canonical)
new(canonical)
end
|
.canonical(source) ⇒ Object
11
12
13
14
15
16
17
|
# File 'lib/settings/data_source/file.rb', line 11
def self.canonical(source)
logger.trace { "Canonizing the file source (#{source})" }
canonize(source).tap do |instance|
logger.debug { "Canonized the file source (#{source})" }
end
end
|
.canonize(source) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/settings/data_source/file.rb', line 19
def self.canonize(source)
return default_filepath if source.nil?
return source if full_path?(source)
dirpath = nil
filepath = nil
if file?(source)
dirpath = Pathname.new(Directory::Defaults.pathname)
else
filepath = Pathname.new(Defaults.filename)
end
dirpath ||= Pathname.new(source)
filepath ||= Pathname.new(source)
pathname(filepath, dirpath)
end
|
.default_filepath ⇒ Object
38
39
40
41
42
43
|
# File 'lib/settings/data_source/file.rb', line 38
def self.default_filepath
dirpath = Pathname.new(Directory::Defaults.pathname)
filepath = Pathname.new(Defaults.filename)
pathname(filepath, dirpath)
end
|
.dir?(dirpath) ⇒ Boolean
57
58
59
|
# File 'lib/settings/data_source/file.rb', line 57
def self.dir?(dirpath)
::File.dirname(dirpath) != "."
end
|
.file?(filepath) ⇒ Boolean
53
54
55
|
# File 'lib/settings/data_source/file.rb', line 53
def self.file?(filepath)
::File.extname(filepath) != ""
end
|
.full_path?(source) ⇒ Boolean
49
50
51
|
# File 'lib/settings/data_source/file.rb', line 49
def self.full_path?(source)
file?(source) && dir?(source)
end
|
.logger ⇒ Object
75
76
77
|
# File 'lib/settings/data_source/file.rb', line 75
def self.logger
@logger ||= Log.get(self)
end
|
.pathname(filepath, dirpath) ⇒ Object
45
46
47
|
# File 'lib/settings/data_source/file.rb', line 45
def self.pathname(filepath, dirpath)
(dirpath + filepath).to_s
end
|
.validate(pathname) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/settings/data_source/file.rb', line 61
def self.validate(pathname)
logger.trace { "Validating the pathname (#{pathname})" }
pathname = Pathname.new(pathname)
unless pathname.file?
msg = "Settings cannot be read from #{pathname}. The file doesn't exist."
logger.error { msg }
raise Settings::Error, msg
end
logger.trace { "Validated the pathname (#{pathname})" }
end
|
Instance Method Details
#get_data ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/settings/data_source/file.rb', line 79
def get_data
logger.trace { "Reading file: #{source}" }
file = ::File.open(source)
data = JSON.load(file).tap do
logger.debug { "Read file: #{source}" }
end
data = Casing::Underscore.(data)
hash_data_source = Hash.build data
hash_data_source.get_data
end
|