Class: DotLocal::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/dot_local/configuration.rb

Constant Summary collapse

SettingsFileName =
'settings.yml'
LocalSuffix =
'local'
ReservedKeys =
%w(env path file_name local_file_name raw)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



11
12
13
14
15
16
17
18
19
20
# File 'lib/dot_local/configuration.rb', line 11

def initialize(options={}) 
  @path = options.delete :path
  @file_name = options.delete :file_name
  @local_file_name = options.delete :local_file_name
  @env = options.delete :env

  @path = File.expand_path('..', __FILE__) if @path.nil?
  @file_name ||= SettingsFileName
  @local_file_name ||= interpolate_local_filename
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/dot_local/configuration.rb', line 22

def method_missing(*args)
  super unless args.size == 1
  key = args.first.to_s 
  if Mapper.key_is_hash?(@raw, key) 
    Mapper.new(self, key)
  else 
    Mapper.fetch(@raw, key)
  end
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



9
10
11
# File 'lib/dot_local/configuration.rb', line 9

def env
  @env
end

#file_nameObject

Returns the value of attribute file_name.



9
10
11
# File 'lib/dot_local/configuration.rb', line 9

def file_name
  @file_name
end

#local_file_nameObject

Returns the value of attribute local_file_name.



9
10
11
# File 'lib/dot_local/configuration.rb', line 9

def local_file_name
  @local_file_name
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/dot_local/configuration.rb', line 9

def path
  @path
end

#rawObject

Returns the value of attribute raw.



9
10
11
# File 'lib/dot_local/configuration.rb', line 9

def raw
  @raw
end

Instance Method Details

#load!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dot_local/configuration.rb', line 37

def load!
  raise DotLocal::DoubleLoad if @loaded 
  @loaded = true
  @raw = parse(file_name)
  @parsed = @raw
  @raw = @raw.fetch(@env.to_s) unless @env.nil?

  merge_with_local! if local_exists?
  
  validate_blank_values! 
  validate_reserved_keys! 
  
  @raw.freeze
end

#reload!Object



32
33
34
35
# File 'lib/dot_local/configuration.rb', line 32

def reload!
  @loaded = false
  self.load!
end