Class: RGovData::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rgovdata/config/config.rb

Defined Under Namespace

Classes: ConfigError, ConfigurationFileInitialized, ConfigurationFileNotFound

Constant Summary collapse

BASE_NAME =
'rgovdata.conf'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



17
18
19
# File 'lib/rgovdata/config/config.rb', line 17

def initialize
  load_default_config
end

Instance Attribute Details

#credentialsetsObject

Returns the value of attribute credentialsets.



15
16
17
# File 'lib/rgovdata/config/config.rb', line 15

def credentialsets
  @credentialsets
end

#default_realmObject

Returns the value of attribute default_realm.



15
16
17
# File 'lib/rgovdata/config/config.rb', line 15

def default_realm
  @default_realm
end

Class Method Details

.default_config_file(override = nil) ⇒ Object



112
113
114
# File 'lib/rgovdata/config/config.rb', line 112

def default_config_file(override = nil)
  File.expand_path(override || BASE_NAME)
end

.default_loader_optionsObject



124
125
126
# File 'lib/rgovdata/config/config.rb', line 124

def default_loader_options
  @@default_loader_options ||= {:generate_default => false,:required => false}
end

.require_config_fileObject



128
129
130
# File 'lib/rgovdata/config/config.rb', line 128

def require_config_file
  @@default_loader_options = {:generate_default => true,:required => true}
end

.reset_configfile(configfilepath) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/rgovdata/config/config.rb', line 104

def reset_configfile(configfilepath)
  file = File.new(configfilepath,'w')
  template.each_line do | line|
    file.puts line
  end
  file.close
end

.templateObject



116
117
118
# File 'lib/rgovdata/config/config.rb', line 116

def template
  RGovData::Template.get('config_template.yml')
end

.template_pathObject



120
121
122
# File 'lib/rgovdata/config/config.rb', line 120

def template_path
  RGovData::Template.path('config_template.yml')
end

Instance Method Details

#clearObject

Clears the current configuration



73
74
75
76
# File 'lib/rgovdata/config/config.rb', line 73

def clear
  @credentialsets = {}
  @default_realm = nil
end

#load_config(configfilepath, options = {}) ⇒ Object



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

def load_config(configfilepath, options = {})
  options.reverse_merge!(:generate_default => true,:required => true)
  unless File.exists?(configfilepath)
    self.class.reset_configfile(configfilepath) if options[:generate_default]
    if File.exists?(configfilepath)
      raise ConfigurationFileInitialized.new("\n
No configuration file found.
A new file has been initialized at: #{configfilepath}
Please review the configuration and retry..\n\n\n")
    elsif options[:required]
      raise ConfigurationFileNotFound.new("cannot load config file #{configfilepath}")
    else
      return
    end
  end
  update_settings(OpenStruct.new(YAML::load(File.read(configfilepath))))
end

#load_default_configObject

Reloads configuraiton from default config provider

  • rails config file (if used in Rails)

  • current directory

  • environment settings



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rgovdata/config/config.rb', line 25

def load_default_config
  clear
  # load default config
  if rails_root
    # if rails env, load from Rails.root.join('config',BASE_NAME)
    load_config(rails_root.join('config',BASE_NAME),self.class.default_loader_options)
  elsif
    # else load from pwd
    load_config(self.class.default_config_file,self.class.default_loader_options)
  else
    # else just refresh_from_env
    refresh_from_env
  end
end

#show_statusObject

Prints current status



79
80
81
82
83
84
85
86
# File 'lib/rgovdata/config/config.rb', line 79

def show_status
  if credentialsets
    puts "credential sets available: #{credentialsets.keys.join(',')}"
    puts credentialsets.inspect
  else
    puts "credential sets available: none"
  end
end