Class: Evertils::Cfg

Inherits:
Object
  • Object
show all
Defined in:
lib/evertils/config.rb

Constant Summary collapse

REPLACEMENTS =
{
  '%DOY%': Date.today.yday,
  '%MONTH_NAME%': Date.today.strftime('%B'),
  '%MONTH%': Date.today.month,
  '%DAY%': Date.today.day,
  '%DOW%': Date.today.wday,
  '%DOW_NAME%': Date.today.strftime('%a'),
  '%YEAR%': Date.today.year,
  '%WEEK%': Date.today.cweek,
  '%WEEK_START%': Date.commercial(Date.today.year, Date.today.cweek, 1),
  '%WEEK_END%': Date.commercial(Date.today.year, Date.today.cweek, 5)
}

Instance Method Summary collapse

Constructor Details

#initializeCfg

default values for initialization



19
20
21
# File 'lib/evertils/config.rb', line 19

def initialize
  @yml = {}
end

Instance Method Details

#bootstrap!Object

Perform first run tasks and create or read config file values



24
25
26
27
28
29
30
# File 'lib/evertils/config.rb', line 24

def bootstrap!
  populate_config

  return if valid_config?

  populate_config
end

#exist?(name, child = nil) ⇒ Boolean

Checks if a key exists Params:

name

String/symbol key value

Returns:

  • (Boolean)


63
64
65
66
67
# 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) ⇒ Object

Get a specific value from the config file data Params:

name

String/symbol key value



54
55
56
57
58
# 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) ⇒ Object

Merge a hash into config data Params:

hash

Any arbitrary hash



72
73
74
75
# File 'lib/evertils/config.rb', line 72

def merge(hash)
  @yml.merge!(hash)
  self
end

#optionsObject

Returns a hash of all module constants and their values



33
34
35
36
37
38
39
# 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) ⇒ Object



81
82
83
84
85
# File 'lib/evertils/config.rb', line 81

def pluck(*args)
  @yml.select do |key, _|
    args.include? key
  end
end

#populate_configObject

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



42
43
44
45
46
47
48
49
# 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!Object



77
78
79
# File 'lib/evertils/config.rb', line 77

def symbolize!
  @yml = @yml.inject({}) { |h, (k, v)| h[k.to_sym] = v; h }
end

#translate_placeholdersObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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