Class: Configit::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/configit/base.rb

Overview

The base class the custom configuration classes should derive from.

Example

class FooConfig < Configit::Base
  attribute :name, "The name of the user", :required => true
  attribute :port, :required => true, :type => :integer, :default => 80
  attribute :log_level, :type => :symbol, :default => :debug
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, desc = nil, options = {}) ⇒ Object

Defines a new attribute on the config.

The first argument should be the name of the attribute.

If the next argument is a string it will be interpreted as the description of the argument.

The last argument should be a valid options hash.

Valid options

:required

Determines if the option is required or not. Should be either true or false

:type

The type of the attribute. Should be one of :integer, :string :symbol, :float



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/configit/base.rb', line 131

def attribute(name, desc=nil, options={})
  raise AttributeAlreadyDefined, name if schema.has_key? name
  
  if options == {} && Hash === desc
    options = desc
    desc = nil
  end

  attr = AttributeDefinition.new(name, desc, options)
  schema[name] = attr


  @attribute_module ||= begin
    m = Module.new 
    include m
    m
  end

  @attribute_module.class_eval do
    define_method name do
      value = attributes[name] || attr.default
      if value != nil
        @@converters[attr.type].call(value)
      else
        value
      end
    end

    define_method "#{name}=" do |value| 
      attributes[name] = value
      value
    end
  end

  return attr
end

.evaluate_erb=(value) ⇒ Object

Raises:



72
73
74
75
# File 'lib/configit/base.rb', line 72

def evaluate_erb=(value)
  raise ArgumentError unless value == true || value == false
  @evaluate_erb = value
end

.load_from_file(filename) ⇒ Object

Load the config from a file.

Raises:



91
92
93
94
95
96
# File 'lib/configit/base.rb', line 91

def load_from_file(filename)
  raise ArgumentError, "File #{filename} does not exist"  unless File.exists?(filename)
  raise ArgumentError, "File #{filename} is not readable" unless File.readable?(filename)

  return load_from_string(IO.read(filename))
end

.load_from_file!(filename) ⇒ Object

Same as load_from_file except it will raise an ArgumentError if the config is not valid.



108
109
110
111
112
# File 'lib/configit/base.rb', line 108

def load_from_file!(filename)
  config = load_from_file(filename)
  config.ensure_valid!
  config
end

.load_from_string(string) ⇒ Object

Loads the config from a YAML string.

Unrecognized attributes are placed into the errors list.



80
81
82
83
84
85
86
87
88
# File 'lib/configit/base.rb', line 80

def load_from_string(string)
  config = self.new
  string = ERB.new(string).result unless @evaluate_erb == false
  (YAML.load(string) || {}).each do |key,value|
    key = key.to_sym
    config.attributes[key] = value
  end
  return config
end

.load_from_string!(string) ⇒ Object

Same as load_from_string except it will raise an ArgumentError if the config is not valid



100
101
102
103
104
# File 'lib/configit/base.rb', line 100

def load_from_string!(string)
  config = load_from_string(string)
  config.ensure_valid!
  config
end

.schemaObject

Returns a hash of Configit::AttributeDefinition’s keyed by attribute name.



68
69
70
# File 'lib/configit/base.rb', line 68

def schema
  @schema ||= {}
end

Instance Method Details

#attributesObject

Returns the attributes defined for this class.



24
25
26
# File 'lib/configit/base.rb', line 24

def attributes
  @attributes ||= {}
end

#clear_errorsObject



32
33
34
# File 'lib/configit/base.rb', line 32

def clear_errors
  @errors = []
end

#ensure_valid!Object



36
37
38
39
40
41
42
# File 'lib/configit/base.rb', line 36

def ensure_valid!
  if !valid?
    message = "#{self.class.name} config invalid. #{self.errors.first}"
    raise ArgumentError, message
  end
  true
end

#errorsObject



28
29
30
# File 'lib/configit/base.rb', line 28

def errors
  @errors ||= []
end

#schemaObject



62
63
64
# File 'lib/configit/base.rb', line 62

def schema
  self.class.schema
end

#valid?Boolean

Returns true if there are no errors, false otherwise

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/configit/base.rb', line 45

def valid?
  clear_errors

  unknown_attributes = attributes.keys - schema.keys
  unknown_attributes.each do |key|
    errors << "#{key} is not a valid attribute name"
  end

  schema.values.each do |attribute|
    if error = attribute.validate(attributes[attribute.name])
      errors << error
    end
  end
  
  errors.empty?
end