Module: Railbow::Config

Defined in:
lib/railbow/config.rb

Constant Summary collapse

GEM_DEFAULTS =
{
  "aliases" => {
    "columns" => {
      "Status" => "Live"
    },
    "values" => {
      "Status" => {"up" => "↑↑", "down" => "↓↓"}
    }
  },
  "since" => "70d",
  "git" => "author:all,diff,mask:auto",
  "author_format" => "short",
  "view" => "calendar,tables",
  "calendar" => "wticks"
}.freeze

Class Method Summary collapse

Class Method Details

.column_aliasesObject



81
82
83
# File 'lib/railbow/config.rb', line 81

def column_aliases
  load.dig("aliases", "columns") || {}
end

.config_filesObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/railbow/config.rb', line 49

def config_files
  files = []

  # 1. Global
  global = File.join(global_dir, "config.yml")
  files << global if File.exist?(global)

  # 2. Project .railbow.yml / .railbow.yaml
  %w[.railbow.yml .railbow.yaml].each do |name|
    full = File.join(root, name)
    if File.exist?(full)
      files << full
      break
    end
  end

  # 3. Local .railbow.local.yml / .railbow.local.yaml
  %w[.railbow.local.yml .railbow.local.yaml].each do |name|
    full = File.join(root, name)
    if File.exist?(full)
      files << full
      break
    end
  end

  files
end

.deep_merge(base, override) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/railbow/config.rb', line 103

def deep_merge(base, override)
  base.merge(override) do |_key, old_val, new_val|
    if old_val.is_a?(Hash) && new_val.is_a?(Hash)
      deep_merge(old_val, new_val)
    else
      new_val
    end
  end
end

.global_dirObject



43
44
45
46
47
# File 'lib/railbow/config.rb', line 43

def global_dir
  xdg = ENV["XDG_CONFIG_HOME"]
  base = (xdg && !xdg.empty?) ? xdg : File.join(Dir.home, ".config")
  File.join(base, "railbow")
end

.loadObject



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

def load
  @loaded ||= config_files.reduce(GEM_DEFAULTS.dup) { |acc, path| deep_merge(acc, read_yaml(path)) }
end

.read_yaml(path) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/railbow/config.rb', line 93

def read_yaml(path)
  content = begin
    YAML.safe_load_file(path)
  rescue Psych::SyntaxError, Psych::DisallowedClass
    warn "  Warning: failed to parse #{path}, using defaults"
    {}
  end
  content.is_a?(Hash) ? content : {}
end

.reset!Object



38
39
40
41
# File 'lib/railbow/config.rb', line 38

def reset!
  @root = nil
  @loaded = nil
end

.rootObject

Overridable root for config file lookup. Defaults to Rails.root or Dir.pwd.



29
30
31
# File 'lib/railbow/config.rb', line 29

def root
  @root || ((defined?(Rails) && Rails.respond_to?(:root) && Rails.root) ? Rails.root.to_s : Dir.pwd)
end

.root=(path) ⇒ Object



33
34
35
36
# File 'lib/railbow/config.rb', line 33

def root=(path)
  @root = path
  @loaded = nil
end

.table_aliasesObject



89
90
91
# File 'lib/railbow/config.rb', line 89

def table_aliases
  {columns: column_aliases, values: value_aliases}
end

.value_aliasesObject



85
86
87
# File 'lib/railbow/config.rb', line 85

def value_aliases
  load.dig("aliases", "values") || {}
end