Class: Applitools::Selenium::ResourceCache
- Inherits:
-
Object
- Object
- Applitools::Selenium::ResourceCache
- Defined in:
- lib/applitools/selenium/visual_grid/resource_cache.rb
Defined Under Namespace
Classes: ResourceMissing
Instance Attribute Summary collapse
-
#cache_map ⇒ Object
Returns the value of attribute cache_map.
-
#semaphore ⇒ Object
Returns the value of attribute semaphore.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #contains?(url) ⇒ Boolean
- #fetch_and_store(key, &_block) ⇒ Object
-
#initialize ⇒ ResourceCache
constructor
A new instance of ResourceCache.
- #keys ⇒ Object
- #urls_to_skip ⇒ Object
Constructor Details
#initialize ⇒ ResourceCache
Returns a new instance of ResourceCache.
9 10 11 12 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 9 def initialize self.cache_map = {} self.semaphore = Mutex.new end |
Instance Attribute Details
#cache_map ⇒ Object
Returns the value of attribute cache_map.
7 8 9 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 7 def cache_map @cache_map end |
#semaphore ⇒ Object
Returns the value of attribute semaphore.
7 8 9 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 7 def semaphore @semaphore end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 29 def [](key) current_value = semaphore.synchronize do cache_map[key] end raise ResourceMissing, key if current_value.nil? return current_value unless current_value.is_a? Applitools::Future update_cache_map(key, current_value.get) end |
#[]=(key, value) ⇒ Object
38 39 40 41 42 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 38 def []=(key, value) Applitools::ArgumentGuard.is_a?(key, 'key', URI) Applitools::ArgumentGuard.one_of?(value, 'value', [Applitools::Future, Applitools::Selenium::VGResource]) update_cache_map(key, value) end |
#contains?(url) ⇒ Boolean
23 24 25 26 27 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 23 def contains?(url) semaphore.synchronize do check_key(url) end end |
#fetch_and_store(key, &_block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 44 def fetch_and_store(key, &_block) semaphore.synchronize do return cache_map[key] if check_key(key) return unless block_given? cache_map[key] = Applitools::Future.new(semaphore, "ResourceCache - #{key}") do |semaphore| yield(semaphore, key) end return true if cache_map[key].is_a? Applitools::Future false end end |
#keys ⇒ Object
14 15 16 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 14 def keys cache_map.keys end |
#urls_to_skip ⇒ Object
18 19 20 21 |
# File 'lib/applitools/selenium/visual_grid/resource_cache.rb', line 18 def urls_to_skip return nil if keys.empty? '"' + keys.map(&:to_s).join('", "') + '"' end |