Module: A9n

Extended by:
SingleForwardable
Defined in:
lib/a9n/ext/string_inquirer.rb,
lib/a9n.rb,
lib/a9n/scope.rb,
lib/a9n/loader.rb,
lib/a9n/struct.rb,
lib/a9n/version.rb,
lib/a9n/ext/hash.rb,
lib/a9n/exceptions.rb,
lib/a9n/yaml_loader.rb

Overview

Defined Under Namespace

Classes: Hash, Loader, Scope, StringInquirer, Struct, YamlLoader

Constant Summary collapse

EXTENSION_LIST =
'{yml,yml.erb,yml.example,yml.erb.example}'.freeze
STRICT_MODE =
'strict'.freeze
DEFAULT_LOG_LEVEL =
'info'.freeze
VERSION =
'1.5.1'.freeze
Error =
Class.new(StandardError)
ConfigurationNotLoadedError =
Class.new(Error)
MissingConfigurationDataError =
Class.new(Error)
MissingConfigurationVariablesError =
Class.new(Error)
NoSuchConfigurationVariableError =
Class.new(Error)
KeyNotFoundError =
Class.new(NoSuchConfigurationVariableError)
MissingEnvVariableError =
Class.new(Error)
UnknownEnvError =
Class.new(Error)
RootNotSetError =
Class.new(Error)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject



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

def logger
  @logger ||= ::Logger.new($stdout, level: fetch(:log_level, DEFAULT_LOG_LEVEL))
end

Class Method Details

.appObject



44
45
46
# File 'lib/a9n.rb', line 44

def app
  @app ||= rails_app
end

.app=(app_instance) ⇒ Object



48
49
50
# File 'lib/a9n.rb', line 48

def app=(app_instance)
  @app = app_instance
end

.app_envObject



40
41
42
# File 'lib/a9n.rb', line 40

def app_env
  app.env if app.respond_to?(:env)
end

.app_rootObject



56
57
58
# File 'lib/a9n.rb', line 56

def app_root
  app.root if app.respond_to?(:root)
end

.default_filesObject



92
93
94
95
96
# File 'lib/a9n.rb', line 92

def default_files
  files  = Dir[root.join("config/{#{A9n::Scope::ROOT_NAMES.join(',')}}.#{EXTENSION_LIST}").to_s]
  files += Dir[root.join("config/a9n/*.#{EXTENSION_LIST}")]
  files.map { |f| f.sub(/.example$/, '') }.uniq.sort
end

.envObject



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

def env
  @env ||= ::A9n::StringInquirer.new(
    app_env ||
    env_var('APP_ENV') ||
    env_var('RACK_ENV') ||
    env_var('RAILS_ENV') ||
    raise(UnknownEnvError)
  )
end

.env=(value) ⇒ Object



36
37
38
# File 'lib/a9n.rb', line 36

def env=(value)
  @env = ::A9n::StringInquirer.new(value)
end

.env_var(name, strict: false) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/a9n.rb', line 82

def env_var(name, strict: false)
  raise A9n::MissingEnvVariableError, name if strict && !ENV.key?(name)

  if ENV[name].is_a?(::String)
    ENV[name].dup.force_encoding(Encoding::UTF_8).freeze
  else
    ENV[name].dup.freeze
  end
end

.groupsObject



74
75
76
# File 'lib/a9n.rb', line 74

def groups
  ['default', env].compact.freeze
end

.load(*files) ⇒ Object



98
99
100
101
102
# File 'lib/a9n.rb', line 98

def load(*files)
  require_local_extension
  files = files.empty? ? default_files : absolute_paths_for(files)
  files.map { |file| load_file(file) }
end

.method_missing(name, *args) ⇒ Object



120
121
122
123
# File 'lib/a9n.rb', line 120

def method_missing(name, *args)
  load if storage.empty?
  storage.send(name, *args)
end

.modeObject



108
109
110
# File 'lib/a9n.rb', line 108

def mode
  ENV['A9N_MODE'] || STRICT_MODE
end

.rails_appObject



78
79
80
# File 'lib/a9n.rb', line 78

def rails_app
  defined?(Rails) ? Rails : nil
end

.rootObject



52
53
54
# File 'lib/a9n.rb', line 52

def root
  @root ||= app_root || root_from_bundle_env || raise(RootNotSetError)
end

.root=(path) ⇒ Object



70
71
72
# File 'lib/a9n.rb', line 70

def root=(path)
  @root = path.to_s.empty? ? nil : Pathname.new(path.to_s).freeze
end

.root_from_bundle_envObject



60
61
62
63
64
65
66
67
68
# File 'lib/a9n.rb', line 60

def root_from_bundle_env
  return nil unless ENV['BUNDLE_GEMFILE']

  dir = File.dirname(ENV.fetch('BUNDLE_GEMFILE', nil))

  return nil unless File.directory?(dir)

  Pathname.new(dir)
end

.storageObject



104
105
106
# File 'lib/a9n.rb', line 104

def storage
  @storage ||= A9n::Struct.new
end

.strict?Boolean

Returns:

  • (Boolean)


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

def strict?
  mode == STRICT_MODE
end