async_data_provider 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/async_data_provider.rb +61 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e32d82650e6a12ff5d34bc6eeac50dd67802c9c
|
4
|
+
data.tar.gz: 831d40dda1a94dcfc3e72262aa739f894d4dacce
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c726a969322d27babed14407dbd0dc978f34da66fd55e1c99cbfb686bd6223f479dd7bb1328ab859468188b1e2f4dd31c6d7ab2f15e95c9a843c47515b022cd8
|
7
|
+
data.tar.gz: 552193bab491fd4614d88644fd66ee497f29ec4079a71b2f68776c23397dfd2a65c6ec32eb6a64b0c72bcd3c5b1cd21850def9faa44d83ddfc3944b36c349371
|
@@ -0,0 +1,61 @@
|
|
1
|
+
##
|
2
|
+
# AsyncDataProvider allows the user to update data in the background on a schedule and return the most recent
|
3
|
+
# data it knows about. The provider will return nil until data is found for the first time
|
4
|
+
#
|
5
|
+
# User of this class should subclass it, implement the time_retrieve_interval, and implment update_data.
|
6
|
+
#
|
7
|
+
#
|
8
|
+
# === Examples
|
9
|
+
#
|
10
|
+
# class SomeSqlData < AsyncDataProvider
|
11
|
+
#
|
12
|
+
# def initialize
|
13
|
+
# super()
|
14
|
+
# @time_retrieve_interval = 120
|
15
|
+
# @query = "\"SELECT some_data FROM some_table WHERE some_condition > 0;\""
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# def update_data(**args)
|
19
|
+
# sql = Sql.new
|
20
|
+
#
|
21
|
+
# return sql.query(@query)
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
require 'thread'
|
25
|
+
require 'singleton'
|
26
|
+
|
27
|
+
class AsyncDataProvider
|
28
|
+
include Singleton
|
29
|
+
|
30
|
+
def initialize
|
31
|
+
@last_time_retrieved = nil
|
32
|
+
@time_retrieve_interval = "must be implemented"
|
33
|
+
@mutex = Mutex.new
|
34
|
+
@data = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_data(**args)
|
38
|
+
if(@last_time_retrieved.nil? || (Time.now - @last_time_retrieved >= @time_retrieve_interval))
|
39
|
+
@last_time_retrieved = Time.now
|
40
|
+
|
41
|
+
Thread.new do
|
42
|
+
if(@mutex.try_lock)
|
43
|
+
@data = update_data(args)
|
44
|
+
@last_time_retrieved = Time.now
|
45
|
+
@mutex.unlock
|
46
|
+
end
|
47
|
+
end.abort_on_exception = true
|
48
|
+
end
|
49
|
+
|
50
|
+
return @data
|
51
|
+
end
|
52
|
+
|
53
|
+
def has_data?
|
54
|
+
return !@data.nil?
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
def update_data(**args)
|
59
|
+
raise "must be implemented"
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: async_data_provider
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brett Sykes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Allows the user to update data in the background on a schedule and return
|
14
|
+
the most recent data
|
15
|
+
email: brettcsykes@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/async_data_provider.rb
|
21
|
+
homepage: https://github.com/bretts/async_data_provider
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.6.8
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Allows the user to update data in the background on a schedule and return
|
45
|
+
the most recent data
|
46
|
+
test_files: []
|