Class: Traject::Indexer::Settings
- Inherits:
-
Hash
- Object
- Hash
- Traject::Indexer::Settings
- Includes:
- Hashie::Extensions::IndifferentAccess, Hashie::Extensions::MergeInitializer
- Defined in:
- lib/traject/indexer/settings.rb
Overview
A Hash of settings for a Traject::Indexer, which also ends up passed along to other objects Traject::Indexer interacts with.
Enhanced with a few features from Hashie, to make it for instance string/symbol indifferent
method #provide(key, value) is added, to do like settings[key] ||= value, set only if not already set (but unlike ||=, nil or false can count as already set) provide WILL overwrite defaults.
Or you can use standard Hash store
which will overwrite already set values as well
as defaults.
Has kind of a weird 'defaults' system, where you tell the hash what it's defaults
are, but they aren't actually loaded until asked for (or you can call fill_in_defaults!
to load em all for inspection), to accomodate the provide
API, where a caller wants to set
only if not already set, but DO overwrite defaults.
Defined Under Namespace
Classes: DefaultsHash
Instance Method Summary collapse
-
#fill_in_defaults! ⇒ Object
Normally defaults are filled in on-demand, but you can trigger it here -- but if you later try to load traject config,
provide
will no longer overwrite defaults!. -
#initialize(*args) ⇒ Settings
constructor
A new instance of Settings.
- #inspect ⇒ Object
- #keys ⇒ Object
-
#provide(key, value) ⇒ Object
a cautious store, which only saves key=value if there was not already a value for #key.
-
#reverse_merge(other_hash) ⇒ Object
reverse_merge copied from ActiveSupport, pretty straightforward, modified to make sure we return a Settings.
- #reverse_merge!(other_hash) ⇒ Object
- #with_defaults(defaults) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Settings
Returns a new instance of Settings.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/traject/indexer/settings.rb', line 34 def initialize(*args) super @defaults = {} self.default_proc = lambda do |hash, key| if @defaults.has_key?(key) return hash[key] = @defaults[key] else return nil end end @defaults_filled = Concurrent::AtomicBoolean.new(false) end |
Instance Method Details
#fill_in_defaults! ⇒ Object
Normally defaults are filled in on-demand, but you can trigger it here --
but if you later try to load traject config, provide
will no longer
overwrite defaults!
82 83 84 |
# File 'lib/traject/indexer/settings.rb', line 82 def fill_in_defaults! self.reverse_merge!(@defaults) end |
#inspect ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/traject/indexer/settings.rb', line 86 def inspect # Keep any key ending in password out of the inspect self.inject({}) do |hash, (key, value)| if /password\Z/.match(key) hash[key] = "[hidden]" else hash[key] = value end hash end.inspect end |
#keys ⇒ Object
55 56 57 |
# File 'lib/traject/indexer/settings.rb', line 55 def keys super + @defaults.keys end |
#provide(key, value) ⇒ Object
a cautious store, which only saves key=value if there was not already a value for #key. Can be used to set settings that can be overridden on command line, or general first-set-wins settings. DOES set over defaults.
63 64 65 66 67 |
# File 'lib/traject/indexer/settings.rb', line 63 def provide(key, value) unless has_key? key store(key, value) end end |
#reverse_merge(other_hash) ⇒ Object
reverse_merge copied from ActiveSupport, pretty straightforward, modified to make sure we return a Settings
71 72 73 |
# File 'lib/traject/indexer/settings.rb', line 71 def reverse_merge(other_hash) self.class.new(other_hash).merge(self) end |
#reverse_merge!(other_hash) ⇒ Object
75 76 77 |
# File 'lib/traject/indexer/settings.rb', line 75 def reverse_merge!(other_hash) replace(reverse_merge(other_hash)) end |
#with_defaults(defaults) ⇒ Object
50 51 52 53 |
# File 'lib/traject/indexer/settings.rb', line 50 def with_defaults(defaults) @defaults = DefaultsHash.new(defaults).freeze self end |