class Settings::DataSource::File

Public Class Methods

canonize(source) click to toggle source
# File lib/settings/data_source/file.rb, line 4
def self.canonize(source)
  canonized_filepath = canonize_filepath(source)
  validate(canonized_filepath)

  canonized_filepath
end
canonize_filepath(source) click to toggle source
# File lib/settings/data_source/file.rb, line 11
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)

  pathname(filepath, dirpath)
end
default_filepath() click to toggle source
# File lib/settings/data_source/file.rb, line 30
def self.default_filepath
  dirpath = Pathname.new(Directory::Defaults.pathname)
  filepath = Pathname.new(Defaults.filename)

  pathname(filepath, dirpath)
end
dir?(dirpath) click to toggle source
# File lib/settings/data_source/file.rb, line 49
def self.dir?(dirpath)
  ::File.dirname(dirpath) != "."
end
file?(filepath) click to toggle source
# File lib/settings/data_source/file.rb, line 45
def self.file?(filepath)
  ::File.extname(filepath) != ""
end
full_path?(source) click to toggle source
# File lib/settings/data_source/file.rb, line 41
def self.full_path?(source)
  file?(source) && dir?(source)
end
pathname(filepath, dirpath) click to toggle source
# File lib/settings/data_source/file.rb, line 37
def self.pathname(filepath, dirpath)
  (dirpath + filepath).to_s
end
validate(pathname) click to toggle source
# File lib/settings/data_source/file.rb, line 53
def self.validate(pathname)
  pathname = Pathname.new(pathname)

  unless pathname.file?
    raise Settings::Error, "Settings cannot be read from #{pathname}. The file doesn't exist."
  end
end

Public Instance Methods

get_data() click to toggle source
# File lib/settings/data_source/file.rb, line 61
def get_data
  file = ::File.open(source)

  data = JSON.load(file)

  hash_data_source = Hash.build data
  hash_data_source.get_data
end