Class: Fusuma::Config::Searcher
- Inherits:
-
Object
- Object
- Fusuma::Config::Searcher
- Defined in:
- lib/fusuma/config/searcher.rb
Overview
Search config.yml
Constant Summary collapse
- CONTEXT_SEARCH_ORDER =
[:no_context, :complete_match_context, :partial_match_context]
Class Attribute Summary collapse
-
.context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
-
.find_context(request_context, fallbacks = CONTEXT_SEARCH_ORDER, &block) ⇒ Hash
Return a matching context from config : (Hash[untyped, untyped], ?Array) { () -> untyped } -> Hash[untyped, untyped]?.
-
.with_context(context = {}, &block) ⇒ Object
Search with context from load_streamed Config : (?Hash[untyped, untyped]) { () -> untyped } -> untyped.
Instance Method Summary collapse
-
#cache(key) ⇒ Object
: (Array | String) { () -> untyped } -> untyped.
-
#initialize ⇒ Searcher
constructor
: () -> void.
-
#search(index, location:) ⇒ NilClass, ...
: (Fusuma::Config::Index, location: untyped) -> untyped.
-
#search_with_cache(index, location:) ⇒ NilClass, ...
: (Fusuma::Config::Index, location: Array).
-
#search_with_context(index, location:, context:) ⇒ Object
: (Fusuma::Config::Index, location: Array, context: Hash[untyped, untyped] | nil) -> untyped.
Constructor Details
#initialize ⇒ Searcher
: () -> void
9 10 11 |
# File 'lib/fusuma/config/searcher.rb', line 9 def initialize @cache = {} end |
Class Attribute Details
.context ⇒ Object (readonly)
Returns the value of attribute context.
85 86 87 |
# File 'lib/fusuma/config/searcher.rb', line 85 def context @context end |
Class Method Details
.find_context(request_context, fallbacks = CONTEXT_SEARCH_ORDER, &block) ⇒ Hash
Return a matching context from config : (Hash[untyped, untyped], ?Array) { () -> untyped } -> Hash[untyped, untyped]?
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/fusuma/config/searcher.rb', line 104 def find_context(request_context, fallbacks = CONTEXT_SEARCH_ORDER, &block) # Search in blocks in the following order. # 1. primary context(no context) # 2. complete match config[:context] == request_context # 3. partial match config[:context] =~ request_context # no_context?(&block) || # complete_match_context(request_context, &block) || # partial_match_context(request_context, &block) fallbacks.find do |method| result = send(method, request_context, &block) return result if result end end |
.with_context(context = {}, &block) ⇒ Object
Search with context from load_streamed Config : (?Hash[untyped, untyped]) { () -> untyped } -> untyped
91 92 93 94 95 96 97 |
# File 'lib/fusuma/config/searcher.rb', line 91 def with_context(context = {}, &block) before = @context @context = context block.call ensure # NOTE: ensure is called even if return in block @context = before end |
Instance Method Details
#cache(key) ⇒ Object
: (Array | String) { () -> untyped } -> untyped
62 63 64 65 66 67 68 69 |
# File 'lib/fusuma/config/searcher.rb', line 62 def cache(key) key = key.join(",") if key.is_a? Array if @cache.key?(key) @cache[key] else @cache[key] = block_given? ? yield : nil end end |
#search(index, location:) ⇒ NilClass, ...
: (Fusuma::Config::Index, location: untyped) -> untyped
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fusuma/config/searcher.rb', line 19 def search(index, location:) key = index.keys.first return location if key.nil? return nil if location.nil? return nil unless location.is_a?(Hash) next_index = Index.new(Array(index.keys[1..-1])) value = nil next_location_cadidates(location, key).find do |next_location| value = search(next_index, location: next_location) end value end |
#search_with_cache(index, location:) ⇒ NilClass, ...
: (Fusuma::Config::Index, location: Array)
55 56 57 58 59 |
# File 'lib/fusuma/config/searcher.rb', line 55 def search_with_cache(index, location:) cache([index.cache_key, Searcher.context]) do search_with_context(index, location: location, context: Searcher.context) end end |
#search_with_context(index, location:, context:) ⇒ Object
: (Fusuma::Config::Index, location: Array, context: Hash[untyped, untyped] | nil) -> untyped
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fusuma/config/searcher.rb', line 37 def search_with_context(index, location:, context:) return nil if location.nil? return search(index, location: location[0]) if context == {} value = nil location.find do |conf| value = search(index, location: conf) if conf[:context] == context end value end |