Module: Railbow::Config
- Defined in:
- lib/railbow/config.rb
Constant Summary collapse
- GEM_DEFAULTS =
{ "aliases" => { "columns" => { "Status" => "Live" }, "values" => { "Status" => {"up" => "↑↑", "down" => "↓↓"} } }, "since" => "70d", "since_min" => 10, "git" => "author:all,diff,mask:auto", "author_format" => "short", "view" => "calendar,tables", "calendar" => "wticks", "db" => "focus" }.freeze
Class Method Summary collapse
- .column_aliases ⇒ Object
- .config_files ⇒ Object
- .deep_merge(base, override) ⇒ Object
- .global_dir ⇒ Object
- .load ⇒ Object
- .read_yaml(path) ⇒ Object
- .reset! ⇒ Object
-
.root ⇒ Object
Overridable root for config file lookup.
- .root=(path) ⇒ Object
- .table_aliases ⇒ Object
- .value_aliases ⇒ Object
Class Method Details
.column_aliases ⇒ Object
83 84 85 |
# File 'lib/railbow/config.rb', line 83 def column_aliases load.dig("aliases", "columns") || {} end |
.config_files ⇒ Object
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 76 77 |
# File 'lib/railbow/config.rb', line 51 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
105 106 107 108 109 110 111 112 113 |
# File 'lib/railbow/config.rb', line 105 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_dir ⇒ Object
45 46 47 48 49 |
# File 'lib/railbow/config.rb', line 45 def global_dir xdg = ENV["XDG_CONFIG_HOME"] base = (xdg && !xdg.empty?) ? xdg : File.join(Dir.home, ".config") File.join(base, "railbow") end |
.load ⇒ Object
79 80 81 |
# File 'lib/railbow/config.rb', line 79 def load @loaded ||= config_files.reduce(GEM_DEFAULTS.dup) { |acc, path| deep_merge(acc, read_yaml(path)) } end |
.read_yaml(path) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/railbow/config.rb', line 95 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
40 41 42 43 |
# File 'lib/railbow/config.rb', line 40 def reset! @root = nil @loaded = nil end |
.root ⇒ Object
Overridable root for config file lookup. Defaults to Rails.root or Dir.pwd.
31 32 33 |
# File 'lib/railbow/config.rb', line 31 def root @root || ((defined?(Rails) && Rails.respond_to?(:root) && Rails.root) ? Rails.root.to_s : Dir.pwd) end |
.root=(path) ⇒ Object
35 36 37 38 |
# File 'lib/railbow/config.rb', line 35 def root=(path) @root = path @loaded = nil end |
.table_aliases ⇒ Object
91 92 93 |
# File 'lib/railbow/config.rb', line 91 def table_aliases {columns: column_aliases, values: value_aliases} end |
.value_aliases ⇒ Object
87 88 89 |
# File 'lib/railbow/config.rb', line 87 def value_aliases load.dig("aliases", "values") || {} end |