Class: MP::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rake_helpers/config.rb

Direct Known Subclasses

FlexConfig

Defined Under Namespace

Classes: Option, OptionNotGivenError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



84
85
86
# File 'lib/rake_helpers/config.rb', line 84

def initialize
  @attributes = {}
end

Class Method Details

.configure {|instance| ... } ⇒ Object

Yields:

  • (instance)


71
72
73
# File 'lib/rake_helpers/config.rb', line 71

def self.configure
  yield instance
end

.global_settingsObject



80
81
82
# File 'lib/rake_helpers/config.rb', line 80

def self.global_settings
  @global_settings ||= {}
end

.global_settings_file=(file) ⇒ Object



75
76
77
78
# File 'lib/rake_helpers/config.rb', line 75

def self.global_settings_file=(file)
  @global_settings = YAML.load(File.read(file))
  @global_settings
end

Instance Method Details

#[](key) ⇒ Object



116
117
118
# File 'lib/rake_helpers/config.rb', line 116

def [](key)
  fetch(key)
end

#add(attribute, options = {}, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rake_helpers/config.rb', line 88

def add(attribute, options={}, &block)
  option = Option.new(attribute, self, [ENV, configuration_settings], options, &block)
  
  @attributes[option.name] = option
  
  self.class.class_eval do
    define_method("#{option.attribute}=") do |value|
      write(option.name, value)
    end
    
    define_method(option.attribute) do |*args|
      read(option.name)
    end
  end
end

#configuration_settingsObject



130
131
132
# File 'lib/rake_helpers/config.rb', line 130

def configuration_settings
  MP::Config.global_settings[settings_key] ||= {}
end

#expose_as_methodObject



120
121
122
123
124
125
126
127
128
# File 'lib/rake_helpers/config.rb', line 120

def expose_as_method
  method_name = underscore(self.class.name)
  config_class = self
  Object.class_eval do
    define_method(method_name) do
      config_class
    end
  end
end

#fetch(attribute, default = nil) ⇒ Object



108
109
110
# File 'lib/rake_helpers/config.rb', line 108

def fetch(attribute, default=nil)
  @attributes[attribute].fetch(default)
end

#read(attribute, *args) ⇒ Object



112
113
114
# File 'lib/rake_helpers/config.rb', line 112

def read(attribute, *args)
  @attributes[attribute].read(*args)
end

#settings_keyObject



134
135
136
# File 'lib/rake_helpers/config.rb', line 134

def settings_key
  @settings_key ||= underscore(self.class.name).split('_')[0]
end

#underscore(camel_cased_word) ⇒ Object

The reverse of camelize. Makes an underscored, lowercase form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

Examples:

"ActiveRecord".underscore         # => "active_record"
"ActiveRecord::Errors".underscore # => active_record/errors


145
146
147
148
149
150
151
# File 'lib/rake_helpers/config.rb', line 145

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

#write(attribute, value) ⇒ Object



104
105
106
# File 'lib/rake_helpers/config.rb', line 104

def write(attribute, value)
  @attributes[attribute].write(value)
end