Class: GitHooks::Utils::Config

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

Defined Under Namespace

Classes: HookWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Config

Returns a new instance of Config.



25
26
27
28
29
30
31
32
33
34
# File 'lib/git_hooks/utils/config.rb', line 25

def initialize(config_file)
  @config = if config_file.respond_to?(:read)
    YAML.load(config_file.read)
  else
    YAML.load_file(File.expand_path(config_file))
  end
rescue Errno::ENOENT
  GitHooks::Logger.error("Config File '#{config_file}' couldn't be loaded!")
  raise "Config File '#{config_file}' couldn't be loaded!"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(hook_name, *args, &blk) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/git_hooks/utils/config.rb', line 36

def method_missing(hook_name, *args, &blk)
  if hook_definitions = config[hook_name]

    hooks = hook_definitions.map { |hook_definition| HookWrapper.new(hook_definition) }
    instance_variable_set("@#{hook_name}", hooks)
    
    instance_eval <<-RUBY
    def #{hook_name}
      @#{hook_name}
    end
    RUBY
    
    send(hook_name)
  else
    super
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/git_hooks/utils/config.rb', line 23

def config
  @config
end