Class: Settings::DataSource::File

Inherits:
Settings::DataSource show all
Defined in:
lib/settings/data_source/file.rb

Defined Under Namespace

Modules: Defaults, Directory

Instance Attribute Summary

Attributes inherited from Settings::DataSource

#source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Settings::DataSource

build, #initialize

Constructor Details

This class inherits a constructor from Settings::DataSource

Class Method Details

.canonize(source) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/settings/data_source/file.rb', line 4

def self.canonize(source)
  logger.trace { "Canonizing the file source (Source: #{source})" }

  canonized_filepath = canonize_filepath(source)
  validate(canonized_filepath)

  logger.debug { "Canonized the file source (Source: #{source}, Canonized: #{canonized_filepath})" }

  canonized_filepath
end

.canonize_filepath(source) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/settings/data_source/file.rb', line 15

def self.canonize_filepath(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)

  logger.debug { "Canonized the file source (#{source})" }

  pathname(filepath, dirpath)
end

.default_filepathObject



36
37
38
39
40
41
# File 'lib/settings/data_source/file.rb', line 36

def self.default_filepath
  dirpath = Pathname.new(Directory::Defaults.pathname)
  filepath = Pathname.new(Defaults.filename)

  pathname(filepath, dirpath)
end

.dir?(dirpath) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/settings/data_source/file.rb', line 55

def self.dir?(dirpath)
  ::File.dirname(dirpath) != "."
end

.file?(filepath) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/settings/data_source/file.rb', line 51

def self.file?(filepath)
  ::File.extname(filepath) != ""
end

.full_path?(source) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/settings/data_source/file.rb', line 47

def self.full_path?(source)
  file?(source) && dir?(source)
end

.loggerObject



73
74
75
# File 'lib/settings/data_source/file.rb', line 73

def self.logger
  @logger ||= Log.get(self)
end

.pathname(filepath, dirpath) ⇒ Object



43
44
45
# File 'lib/settings/data_source/file.rb', line 43

def self.pathname(filepath, dirpath)
  (dirpath + filepath).to_s
end

.validate(pathname) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/settings/data_source/file.rb', line 59

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_dataObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/settings/data_source/file.rb', line 77

def get_data
  logger.trace { "Reading file: #{source}" }
  file = ::File.open(source)
  data = JSON.load(file).tap do
    logger.debug { "Read file: #{source}" }
  end

  hash_data_source = Hash.build data
  hash_data_source.get_data
end