Class: Ahnnotate::Config

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Config

Returns a new instance of Config.



51
52
53
54
55
56
57
58
# File 'lib/ahnnotate/config.rb', line 51

def initialize(config)
  @config =
    if config.is_a?(Hash)
      config
    else
      {}
    end
end

Class Method Details

.defaultObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ahnnotate/config.rb', line 16

def self.default
  @default ||= {
    "boot" => nil,
    "rake_db_autorun" => false,
    "annotate" => {
      "models" => {
        "enabled" => true,
        "path" => "app/models",
        "extension" => ".rb",
      },
    },
  }
end

.effective_defaultObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ahnnotate/config.rb', line 37

def self.effective_default
  if @effective_default
    return @effective_default
  end

  @effective_default ||= YAML.load(YAML.dump(default)) # deep dup

  if Gem.loaded_specs.key?("rails")
    @effective_default.merge!(rails_additions)
  end

  @effective_default
end

.load(root:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/ahnnotate/config.rb', line 5

def self.load(root:)
  config_path = root.join(".ahnnotate.yml")

  if !config_path.exist?
    return new({})
  end

  loaded_config = YAML.safe_load(File.read(config_path))
  new(loaded_config)
end

.rails_additionsObject



30
31
32
33
34
35
# File 'lib/ahnnotate/config.rb', line 30

def self.rails_additions
  @rails_additions ||= {
    "boot" => %(require File.expand_path("config/environment").to_s),
    "rake_db_autorun" => true,
  }
end

Instance Method Details

#[](*args) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/ahnnotate/config.rb', line 60

def [](*args)
  specified_config = @config.dig(*args)

  if specified_config.nil?
    self.class.effective_default.dig(*args)
  else
    specified_config
  end
end