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
31
32
33
34
35
36
# File 'lib/evertils/config.rb', line 24

def bootstrap!
  populate_config

  return if valid_config?

  # no config file found, lets create one using the firstrun controller
  require 'client/controller/firstrun'

  controller = Evertils::Controller::Firstrun.new
  controller.default

  populate_config
end

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

Checks if a key exists Params:

name

String/symbol key value

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/evertils/config.rb', line 69

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



60
61
62
63
64
# File 'lib/evertils/config.rb', line 60

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



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

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

#optionsObject

Returns a hash of all module constants and their values



39
40
41
42
43
44
45
# File 'lib/evertils/config.rb', line 39

def options
  keys = Evertils.constants.select { |name| constant?(name) }
  hash = {}

  keys.each { |key| hash[key] = Evertils.const_get(key) }
  hash
end

#pluck(*args) ⇒ Object



87
88
89
90
91
# File 'lib/evertils/config.rb', line 87

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



48
49
50
51
52
53
54
55
# File 'lib/evertils/config.rb', line 48

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



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

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

#translate_placeholdersObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/evertils/config.rb', line 93

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