class Evertils::Cfg

Constants

REPLACEMENTS

Public Class Methods

new() click to toggle source

default values for initialization

# File lib/evertils/config.rb, line 19
def initialize
  @yml = {}
end

Public Instance Methods

bootstrap!() click to toggle source

Perform first run tasks and create or read config file values

# File lib/evertils/config.rb, line 24
def bootstrap!
  populate_config

  return if valid_config?

  populate_config
end
exist?(name, child = nil) click to toggle source

Checks if a key exists Params:

name

String/symbol key value

# File lib/evertils/config.rb, line 63
def exist?(name, child = nil)
  return @yml[name].key?(child.to_sym) unless child.nil?

  @yml.key?(name.to_sym)
end
get(name, child = nil) click to toggle source

Get a specific value from the config file data Params:

name

String/symbol key value

# File lib/evertils/config.rb, line 54
def get(name, child = nil)
  return @yml[name.to_sym][child.to_sym] unless child.nil?

  @yml[name.to_sym]
end
merge(hash) click to toggle source

Merge a hash into config data Params:

hash

Any arbitrary hash

# File lib/evertils/config.rb, line 72
def merge(hash)
  @yml.merge!(hash)
  self
end
options() click to toggle source

Returns a hash of all module constants and their values

# File lib/evertils/config.rb, line 33
def options
  keys = Evertils.constants.select { |name| constant?(name) }
  hash = {}

  keys.each { |key| hash[key] = Evertils.const_get(key) }
  hash
end
pluck(*args) click to toggle source
# File lib/evertils/config.rb, line 81
def pluck(*args)
  @yml.select do |key, _|
    args.include? key
  end
end
populate_config() click to toggle source

Populates the internal hash which stores any values set in the config file

# File lib/evertils/config.rb, line 42
def populate_config
  file = File.expand_path("~/.evertils/config.yml")
  @yml = Evertils::Helper::Formatting.symbolize_keys(::YAML.load_file(file))

  set_evertils_token

  self
end
symbolize!() click to toggle source
# File lib/evertils/config.rb, line 77
def symbolize!
  @yml = @yml.inject({}) { |h, (k, v)| h[k.to_sym] = v; h }
end
translate_placeholders() click to toggle source
# File lib/evertils/config.rb, line 87
def translate_placeholders
  title_format = @yml[:title].dup

  @yml.map do |item|
    break if item.last.is_a? Hash

    REPLACEMENTS.each_pair do |k, v|
      item.last.gsub!(k.to_s, v.to_s) if item.last.is_a? String
      item.last.map { |i| i.gsub!(k.to_s, v.to_s) } if item.last.is_a? Array
    end
  end

  @yml[:title_format] = title_format unless @yml.key? :title_format

  Evertils::Helper::Formatting.symbolize_keys(@yml)
  self
end

Private Instance Methods

constant?(name) click to toggle source

Checks if string is a constant

# File lib/evertils/config.rb, line 117
def constant?(name)
  name == name.upcase
end
set_evertils_token() click to toggle source
# File lib/evertils/config.rb, line 107
def set_evertils_token
  ENV['EVERTILS_TOKEN'] = @yml[:token]
end
valid_config?() click to toggle source

Check if configuration data exists

# File lib/evertils/config.rb, line 112
def valid_config?
  !@yml.nil?
end