Class: Appfuel::Config::DefinitionDsl
- Inherits:
-
Object
- Object
- Appfuel::Config::DefinitionDsl
- Includes:
- FileLoader, Populate, Search
- Defined in:
- lib/appfuel/config/definition_dsl.rb
Overview
A configuration definition holds the methods that are exposed in Config dsl. This definition allows you to define a given configuration as it would exist in a hash. The dsl collects information like where the file that holds the config data is stored, validation for that data and default values. Just like hashes can have nested hashes you can have nested definitions using the “define” method
NOTE: currently we only support yaml config files
Appfuel::Config.define :foo do
file /etc/startplus/offers.yml
defaults bar: 'bif',
baz: 'biz'
env FOO_BAR: :bar,
FOO_BAZ: :baz
unsafe :some_key, :other_key
validator {
required(:name).filled
}
define :bam do
defaults bat: 'hit',
rat: 'cheese'
validator {
required(:cheese_type).filled
}
end
end
Results in something like this
hash = {
foo: {
bar: 'bif',
baz: 'baz',
name: <user supplied>,
bam: {
bat: 'hit',
rat: 'cheese',
cheese_type: <user supplied>
}
}
}
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Attributes included from FileLoader
#file_module, #json_module, #yaml_module
Instance Method Summary collapse
-
#<<(definitions) ⇒ Object
Append a definition to this definition’s children.
-
#defaults(settings = nil) ⇒ Object
Dsl command used to set default values.
-
#define(key, &block) ⇒ Object
Dsl to add a configuration definition as a child of another definition.
- #delete(name) ⇒ Object
-
#delete_file ⇒ Object
Dsl used when you expected to manually pass in the configuration data and ignore the configuration in the file.
-
#env(settings = nil) ⇒ Object
Dsl command used to define what env variables will me mapped to config keys.
-
#file(path = nil) ⇒ Object
Dsl command used to set the file path.
- #file? ⇒ Boolean
-
#initialize(key) ⇒ Object
constructor
A definition must be created with a key that will be used in the resulting configuration hash that is built.
-
#validator(&block) ⇒ Object
Dsl to assign validator.
- #validator? ⇒ Boolean
Methods included from Populate
Methods included from Search
Methods included from FileLoader
#load_file, #parse_json, #parse_yaml
Constructor Details
#initialize(key) ⇒ Object
A definition must be created with a key that will be used in the resulting configuration hash that is built
64 65 66 67 68 69 70 71 |
# File 'lib/appfuel/config/definition_dsl.rb', line 64 def initialize(key) @key = key.to_sym @defaults = {} @file = [] @validator = nil @children = {} @env = {} end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
57 58 59 |
# File 'lib/appfuel/config/definition_dsl.rb', line 57 def key @key end |
Instance Method Details
#<<(definitions) ⇒ Object
Append a definition to this definition’s children
165 166 167 168 |
# File 'lib/appfuel/config/definition_dsl.rb', line 165 def <<(definitions) list = definitions.is_a?(Array) ? definitions : [definitions] list.each {|item| children[item.key] = item} end |
#defaults(settings = nil) ⇒ Object
Dsl command used to set default values. When used without params it returns the full default hash
105 106 107 108 109 110 111 112 |
# File 'lib/appfuel/config/definition_dsl.rb', line 105 def defaults(settings = nil) return @defaults if settings.nil? unless settings.is_a?(Hash) fail ArgumentError, 'defaults must be a hash' end @defaults = settings end |
#define(key, &block) ⇒ Object
Dsl to add a configuration definition as a child of another definition
152 153 154 155 156 |
# File 'lib/appfuel/config/definition_dsl.rb', line 152 def define(key, &block) definition = self.class.new(key) definition.instance_eval(&block) self << definition end |
#delete(name) ⇒ Object
158 159 160 |
# File 'lib/appfuel/config/definition_dsl.rb', line 158 def delete(name) @children.delete(name.to_sym) end |
#delete_file ⇒ Object
Dsl used when you expected to manually pass in the configuration data and ignore the configuration in the file
96 97 98 |
# File 'lib/appfuel/config/definition_dsl.rb', line 96 def delete_file @file = [] end |
#env(settings = nil) ⇒ Object
Dsl command used to define what env variables will me mapped to config keys
121 122 123 124 125 126 127 128 |
# File 'lib/appfuel/config/definition_dsl.rb', line 121 def env(settings = nil) return @env if settings.nil? unless settings.is_a?(Hash) fail ArgumentError, 'config env settings must be a hash' end @env = settings end |
#file(path = nil) ⇒ Object
Dsl command used to set the file path. When used without params it returns the file path set.
78 79 80 81 82 83 84 85 86 |
# File 'lib/appfuel/config/definition_dsl.rb', line 78 def file(path = nil) return @file if path.nil? path = [path] if path.is_a?(String) unless path.is_a?(Array) fail "file path must be a String or Array of Strings" end @file = path end |
#file? ⇒ Boolean
88 89 90 |
# File 'lib/appfuel/config/definition_dsl.rb', line 88 def file? !@file.empty? end |
#validator(&block) ⇒ Object
Dsl to assign validator. When no params are given then it returns the assigned validator. We use the validation library dry-validation dry-rb.org/gems/dry-validation/. We will consider any object that implements ‘call` method a validator.
137 138 139 140 141 |
# File 'lib/appfuel/config/definition_dsl.rb', line 137 def validator(&block) return @validator unless block_given? @validator = Dry::Validation.Schema(&block) end |
#validator? ⇒ Boolean
143 144 145 |
# File 'lib/appfuel/config/definition_dsl.rb', line 143 def validator? !@validator.nil? end |