Class: YAMLConfig

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/yamlconfig.rb

Overview

This class represents a configuration, using a YAML file, by providing “accessor” methods for each item in the YAML file

Constant Summary collapse

VERSION =
"0.2.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ YAMLConfig

Returns a new instance of YAMLConfig.



13
14
15
16
17
18
# File 'lib/yamlconfig.rb', line 13

def initialize(resource)
  @specialmethods = Array.new
  @resource = resource

  reload_from_file()
end

Instance Attribute Details

#resourceObject

Returns the value of attribute resource.



10
11
12
# File 'lib/yamlconfig.rb', line 10

def resource
  @resource
end

Instance Method Details

#+(other) ⇒ Object

this should take a [] capable object, add it (keys and values) to self, and return self



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/yamlconfig.rb', line 54

def +(other)
  if !@con.nil?
    other.each do |k, v|
      # add it to con
      @con[k] = v

      # add it as a method
      (class << self; self; end).class_eval { define_method(k) { v }}

      #add it to the @specialmethods array
      @specialmethods.push k
    end
  end
  self
end

#[](key) ⇒ Object

This method is provided for keys and other structures where getting at the method call is impossible.

This will return ‘nil’ for all values if the YAML you have loaded is improperly formed.



28
29
30
31
32
33
34
# File 'lib/yamlconfig.rb', line 28

def [](key)
  if !@con.nil?
    return @con[key]
  end

  nil
end

#[]=(key, value) ⇒ Object

This is a writer in the same spirit as the [] method. Please read the documentation for that.



41
42
43
44
45
46
47
# File 'lib/yamlconfig.rb', line 41

def []=(key, value)
  if !@con.nil?
    return @con[key] = value
  end

  nil
end

#eachObject

standard each usage, iterates over the config keys returning key,value



114
115
116
117
118
# File 'lib/yamlconfig.rb', line 114

def each
  @specialmethods.each do |key|
    yield key, self.send(key)
  end
end

#reload_from_fileObject

Remove all current (if any) attr_readers, then load the resource named in @resource into psuedo attr_readers



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/yamlconfig.rb', line 88

def reload_from_file
  
  # first remove all the previous methods from this instance
  if @specialmethods
    @specialmethods.each do |meth|
        (class << self; self; end).class_eval { undef_method(meth) }
    end
  end

  @specialmethods = []

  # then load the new methods
  # add a method to the Singleton class of this instance of Config.
  # giving a private readonly accessor named "key" that returns "value" for each
  # key value pair of the YAML file in @resource
  @con = loadresource()
  if @con
    @con.each do |key,value|
      (class << self; self; end).class_eval { define_method(key) { value }}
      @specialmethods.push key
    end
  end
end

#write(io = @resource) ⇒ Object

Rewrite and reload the yaml file. Note, that this will overwrite your current yaml file and reload your configuration. If you want to avoid doing this, provide the path to a filename for it to work with.



76
77
78
79
80
81
82
83
84
# File 'lib/yamlconfig.rb', line 76

def write(io=@resource)
  File.open(io, 'w+') do |f|
    f << YAML::dump(@con)
  end
  
  if io == @resource
    reload_from_file
  end
end