Module: ConfigContext

Extended by:
ConfigContext
Included in:
ConfigContext
Defined in:
lib/config_context.rb

Defined Under Namespace

Classes: ConfigError

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/config_context.rb', line 25

def method_missing(method, *arguments, &block)
  
  @config || init
  
  case method.to_s
    when /(.+)=$/  then
      property_key = method.to_s.delete('=').to_sym
      
      @config[property_key] = (arguments.size == 1) ? arguments[0] : arguments
    
    when /(.+)\?$/ then 
      property_key = method.to_s.delete('?').to_sym

      @config.keys.include?(property_key) #true/false
  else

    if @config.keys.include?(method) # any type
      @config[method]
   else
      super
    end
  end    
end

Instance Method Details

#[](key) ⇒ Object

Backward compability…



101
102
103
104
105
# File 'lib/config_context.rb', line 101

def [](key)
  
  @config || init 
  @config[key]
end

#configure(source = nil, options = {}, &block) ⇒ Object



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
# File 'lib/config_context.rb', line 53

def configure(source=nil, options={}, &block)

  @config || init
  
  options = {:source=>nil, :context=>:root}.merge(options)
  
  if options[:context] == :root

    case source
      when /\.(yml|yaml)/i then @config.merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading file: #{source}")
      when /\.json/i       then @config.merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading file: #{source}")
      when Hash            then @config.merge!(source)
    else 
      yield self if block_given?
    end
  else

    context          = options[:context]
    @config[context] ||= { } # New context
    
    case source
      when /\.(yml|yaml)/i then @config[context].merge!(YAML.load_file(source)) rescue raise ConfigError.new("Problems loading file: #{source}")
      when /\.json/i       then @config[context].merge!(JSON.parse(File.read(source))) rescue raise ConfigError.new("Problems loading file: #{source}")
      when Hash            then @config[context].merge!(source)
    else 
      yield self if block_given?
    end
  end
  
  self
end

#erase!Object



49
50
51
# File 'lib/config_context.rb', line 49

def erase!()
  @config.clear
end

#fetch(key, default = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/config_context.rb', line 107

def fetch(key,default=nil)
  
  @config || init
  if @config.include?(key)
    
    @config[key]
  else
    default ? default : nil
  end 
end

#fetch!(key, default) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/config_context.rb', line 118

def fetch!(key,default)
  
  @config || init
  
  @config[key] = default unless @config.include?(key)
  @config[key]
end

#inspectObject



89
90
91
92
93
# File 'lib/config_context.rb', line 89

def inspect
  
  @config || init
  @config.inspect
end

#to_hashObject



85
86
87
# File 'lib/config_context.rb', line 85

def to_hash 
  @config || init
end

#to_sObject



95
96
97
# File 'lib/config_context.rb', line 95

def to_s
  "#{@config.inspect}"
end