summaryrefslogtreecommitdiff
path: root/lib/ohai/config.rb
blob: bc9ac2f03f3f1530bf787644e8a85f07b95e3d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 OpsCode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'ohai/mixin/from_file'

# Ohai::Config[:variable]
# @config = Ohai::Config.new()
# Ohai::Config.from_file(foo)
# Ohai::Config[:cookbook_path]
# Ohai::Config.cookbook_path
# Ohai::Config.cookbook_path "one", "two"

module Ohai
  class Config
  
    @configuration = {
      :log_level => :info,
      :log_location => STDOUT,
      :plugin_path => [ File.expand_path(File.join(File.dirname(__FILE__), 'plugins')) ]
    }
    
    class << self
      include Ohai::Mixin::FromFile
      
      # Pass Ohai::Config.configure() a block, and it will yield @configuration.
      #
      # === Parameters
      # <block>:: A block that takes @configure as it's argument
      def configure(&block)
        yield @configuration
      end
      
      # Get the value of a configuration option
      #
      # === Parameters
      # config_option<Symbol>:: The configuration option to return
      #
      # === Returns
      # value:: The value of the configuration option
      #
      # === Raises
      # <ArgumentError>:: If the configuration option does not exist
      def [](config_option)
        if @configuration.has_key?(config_option.to_sym)
          @configuration[config_option.to_sym]
        else
          raise ArgumentError, "Cannot find configuration option #{config_option.to_s}"
        end
      end
      
      # Set the value of a configuration option
      #
      # === Parameters
      # config_option<Symbol>:: The configuration option to set (within the [])
      # value:: The value for the configuration option
      #
      # === Returns
      # value:: The new value of the configuration option
      def []=(config_option, value)
        @configuration[config_option.to_sym] = value
      end
      
      # Check if Ohai::Config has a configuration option.
      #
      # === Parameters
      # key<Symbol>:: The configuration option to check for
      #
      # === Returns
      # <True>:: If the configuration option exists
      # <False>:: If the configuration option does not exist
      def has_key?(key)
        @configuration.has_key?(key.to_sym)
      end
      
      # Allows for simple lookups and setting of configuration options via method calls
      # on Ohai::Config.  If there any arguments to the method, they are used to set
      # the value of the configuration option.  Otherwise, it's a simple get operation.
      #
      # === Parameters
      # method_symbol<Symbol>:: The method called.  Must match a configuration option.
      # *args:: Any arguments passed to the method
      #
      # === Returns
      # value:: The value of the configuration option.
      #
      # === Raises
      # <ArgumentError>:: If the method_symbol does not match a configuration option.
      def method_missing(method_symbol, *args)
        if @configuration.has_key?(method_symbol)
          if args.length == 1
            @configuration[method_symbol] = args[0]
          elsif args.length > 1
            @configuration[method_symbol] = args
          end
          return @configuration[method_symbol]
        else
          raise ArgumentError, "Cannot find configuration option #{method_symbol.to_s}"
        end
      end
      
    end # class << self
  end
end