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



130
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
# File 'lib/configit/base.rb', line 130

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:



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

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:



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

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.



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

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.



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

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



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

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.



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

def schema
  @schema ||= {}
end

Instance Method Details

#attributesObject

Returns the attributes defined for this class.



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

def attributes
  @attributes ||= {}
end

#clear_errorsObject



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

def clear_errors
  @errors = []
end

#ensure_valid!Object



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

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

#errorsObject



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

def errors
  @errors ||= []
end

#schemaObject



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

def schema
  self.class.schema
end

#valid?Boolean

Returns true if there are no errors, false otherwise

Returns:

  • (Boolean)


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

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