Module: Confiture::Configuration::ClassExtension
- Defined in:
- lib/confiture/configuration.rb
Instance Method Summary collapse
-
#blank?(key) ⇒ Boolean
Checks if a given key is nil or empty.
-
#configure(options = {}, reset = false) ⇒ Object
Rails initializer configuration:.
-
#confiture_allowed_keys(*allowed_keys) ⇒ Object
Set a list of allowed configuration options.
-
#confiture_defaults(defaults) ⇒ Object
Set a hash of defaults.
-
#confiture_mandatory_keys(*mandatory_keys) ⇒ Object
Set a list of mandatory configuration options.
-
#reset! ⇒ Object
Resets configuration to defaults.
-
#valid? ⇒ Boolean
Validates the configuration.
-
#valid_key?(key) ⇒ Boolean
Validates if a given key is valid for configuration.
-
#validate! ⇒ Object
Raises an ArgumentError if the configuration is not valid.
-
#validate_key!(key) ⇒ Object
Raises an ArgumentError if the given key is not allowed as a configuration option.
-
#with_config(options = {}) ⇒ Object
Run a block of code with temporary configuration.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object (private)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/confiture/configuration.rb', line 156 def method_missing(meth, *args) key = "#{meth}" if respond_to?(meth) super elsif key =~ /.+=/ && args.size == 1 key = key[0..-2].to_sym validate_key!(key) data.[key] = args.last elsif args.size == 0 key = key.to_sym validate_key!(key) data.[key] else super end end |
Instance Method Details
#blank?(key) ⇒ Boolean
Checks if a given key is nil or empty.
126 127 128 129 |
# File 'lib/confiture/configuration.rb', line 126 def blank?(key) val = self.send key val.nil? || val.empty? end |
#configure(options = {}, reset = false) ⇒ Object
Rails initializer configuration:
Confiture::Configuration.configure do |config|
config.secret = 'your-secret'
config.key = 'your-key'
end
You may pass options as a hash as well:
Confiture::Configuration.configure secret: 'your-secret', key: 'your-key'
Or configure everything using YAML:
Confiture::Configuration.configure yaml: 'config/asin.yml'
Confiture::Configuration.configure yaml: 'config/asin.yml' do |config, yml|
config.key = yml[Rails.env]['key']
end
Options:
- yaml|yml
-
path to a yaml file with configuration
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/confiture/configuration.rb', line 56 def configure( = {}, reset = false) init_config(reset) if yml_path = [:yaml] || [:yml] yml = File.open(yml_path) { |file| YAML.load(file) } if block_given? yield self, yml else yml.each do |key, value| send(:"#{key}=", value) end end elsif block_given? yield self else .each do |key, value| send(:"#{key}=", value) end end self end |
#confiture_allowed_keys(*allowed_keys) ⇒ Object
Set a list of allowed configuration options. If set, trying to access an option that is not configured will result in an ArgumentError.
21 22 23 |
# File 'lib/confiture/configuration.rb', line 21 def confiture_allowed_keys(*allowed_keys) @allowed_keys = allowed_keys end |
#confiture_defaults(defaults) ⇒ Object
Set a hash of defaults. Defaults will be used while configuring the options, but may be overridden.
14 15 16 |
# File 'lib/confiture/configuration.rb', line 14 def confiture_defaults(defaults) @defaults = defaults end |
#confiture_mandatory_keys(*mandatory_keys) ⇒ Object
Set a list of mandatory configuration options. If set, trying to access an option that is not configured properly will result in an ArgumentError. The validation can be triggered manually by calling validate!
29 30 31 |
# File 'lib/confiture/configuration.rb', line 29 def confiture_mandatory_keys(*mandatory_keys) @mandatory_keys = mandatory_keys end |
#reset! ⇒ Object
Resets configuration to defaults
89 90 91 |
# File 'lib/confiture/configuration.rb', line 89 def reset! init_config(true) end |
#valid? ⇒ Boolean
Validates the configuration. All mandatory keys have to be not blank.
106 107 108 |
# File 'lib/confiture/configuration.rb', line 106 def valid? @mandatory_keys.nil? || @mandatory_keys.none? { |key| blank?(key) } end |
#valid_key?(key) ⇒ Boolean
Validates if a given key is valid for configuration.
120 121 122 |
# File 'lib/confiture/configuration.rb', line 120 def valid_key?(key) @allowed_keys.nil? || @allowed_keys.include?(key) end |
#validate! ⇒ Object
Raises an ArgumentError if the configuration is not valid.
95 96 97 98 99 100 101 102 |
# File 'lib/confiture/configuration.rb', line 95 def validate! if data.nil? raise ArgumentError.new("you are working on an empty configuration. run configuration code first!") end unless valid? raise ArgumentError.new("you are missing mandatory configuration options. please set #{@mandatory_keys}") end end |
#validate_key!(key) ⇒ Object
Raises an ArgumentError if the given key is not allowed as a configuration option.
112 113 114 115 116 |
# File 'lib/confiture/configuration.rb', line 112 def validate_key!(key) unless valid_key?(key) raise ArgumentError.new("#{key} is not allowed, use one of #{@allowed_keys}") end end |
#with_config(options = {}) ⇒ Object
Run a block of code with temporary configuration.
79 80 81 82 83 84 85 |
# File 'lib/confiture/configuration.rb', line 79 def with_config(={}) self.current_data = data configure() yield ensure self.current_data = nil end |