Class: Fusuma::Config::Searcher
- Inherits:
-
Object
- Object
- Fusuma::Config::Searcher
- Defined in:
- lib/fusuma/config/searcher.rb
Overview
Search config.yml
Class Attribute Summary collapse
-
.context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
- .conditions(&block) ⇒ Hash
-
.fallback(&block) ⇒ Object
switch context for fallback.
- .fallback? ⇒ Boolean
-
.find_condition(&block) ⇒ Array<Symbol, Object>
Execute block with all conditions.
-
.find_context(request_context, &block) ⇒ Hash
Return a matching context from config.
- .skip(&block) ⇒ Object
- .skip? ⇒ Boolean
-
.with_condition(condition, &block) ⇒ Object
Execute block with specified conditions.
-
.with_context(context, &block) ⇒ Object
Search with context from load_streamed Config.
Instance Method Summary collapse
- #cache(key) ⇒ Object
-
#initialize ⇒ Searcher
constructor
A new instance of Searcher.
- #search(index, location:) ⇒ NilClass, ...
- #search_with_cache(index, location:) ⇒ NilClass, ...
- #search_with_context(index, location:, context:) ⇒ Object
Constructor Details
#initialize ⇒ Searcher
Returns a new instance of Searcher.
8 9 10 |
# File 'lib/fusuma/config/searcher.rb', line 8 def initialize @cache = nil end |
Class Attribute Details
.context ⇒ Object (readonly)
Returns the value of attribute context.
138 139 140 |
# File 'lib/fusuma/config/searcher.rb', line 138 def context @context end |
Class Method Details
.conditions(&block) ⇒ Hash
80 81 82 83 84 85 |
# File 'lib/fusuma/config/searcher.rb', line 80 def conditions(&block) { nothing: -> { block.call }, skip: -> { Config::Searcher.skip { block.call } } } end |
.fallback(&block) ⇒ Object
switch context for fallback
149 150 151 152 153 154 |
# File 'lib/fusuma/config/searcher.rb', line 149 def fallback(&block) @fallback = true result = block.call @fallback = false result end |
.fallback? ⇒ Boolean
140 141 142 |
# File 'lib/fusuma/config/searcher.rb', line 140 def fallback? @fallback end |
.find_condition(&block) ⇒ Array<Symbol, Object>
Execute block with all conditions
96 97 98 99 100 101 102 103 |
# File 'lib/fusuma/config/searcher.rb', line 96 def find_condition(&block) conditions(&block).find do |c, l| result = l.call return [c, result] if result nil end end |
.find_context(request_context, &block) ⇒ Hash
Return a matching context from config
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/fusuma/config/searcher.rb', line 118 def find_context(request_context, &block) # Search in blocks in the following order. # 1. complete match config[:context] == request_context # 2. partial match config[:context] =~ request_context # 3. no context Config.instance.keymap.each do |config| next unless config[:context] == request_context return config[:context] if with_context(config[:context]) { block.call } end if request_context.keys.size > 1 Config.instance.keymap.each do |config| next if config[:context].nil? next unless config[:context].all? { |k, v| request_context[k] == v } return config[:context] if with_context(config[:context]) { block.call } end end return {} if with_context({}) { block.call } end |
.skip(&block) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/fusuma/config/searcher.rb', line 156 def skip(&block) @skip = true result = block.call @skip = false result end |
.skip? ⇒ Boolean
144 145 146 |
# File 'lib/fusuma/config/searcher.rb', line 144 def skip? @skip end |
.with_condition(condition, &block) ⇒ Object
Execute block with specified conditions
90 91 92 |
# File 'lib/fusuma/config/searcher.rb', line 90 def with_condition(condition, &block) conditions(&block)[condition].call end |
.with_context(context, &block) ⇒ Object
Search with context from load_streamed Config
108 109 110 111 112 113 |
# File 'lib/fusuma/config/searcher.rb', line 108 def with_context(context, &block) @context = context || {} result = block.call @context = {} result end |
Instance Method Details
#cache(key) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/fusuma/config/searcher.rb', line 56 def cache(key) @cache ||= {} 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, ...
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/fusuma/config/searcher.rb', line 17 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(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, ...
50 51 52 53 54 |
# File 'lib/fusuma/config/searcher.rb', line 50 def search_with_cache(index, location:) cache([index.cache_key, Searcher.context, Searcher.skip?, Searcher.fallback?]) do search_with_context(index, location: location, context: Searcher.context) end end |
#search_with_context(index, location:, context:) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/fusuma/config/searcher.rb', line 34 def search_with_context(index, location:, context:) return nil if location.nil? return search(index, location: location[0]) if context == {} new_location = location.find do |conf| search(index, location: conf) if conf[:context] == context end search(index, location: new_location) end |