pollbooth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 81091c1fefc46ceeb0fafbefe905788926588110
4
+ data.tar.gz: 7b26e4a95429cc84c54d6f8f6793420267b5c23a
5
+ SHA512:
6
+ metadata.gz: 7cfa5fd1757ba7b98e14343bc05ba32cef1504325af270189a5666682d934fd16086c11adcfc0d9933e179bafe24413686427e7c62e381e99e2ac4eacef61a0e
7
+ data.tar.gz: 5c054e683a2906db7b8e428a06e249479033bcb3c41b5eea24a520ba3281256b39a54945ca5c2432f43dbd029222af8a16c9af078f9f70472286e97a1c6f433c
data/lib/pollbooth.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'active_support/core_ext'
2
+ require 'bigben'
3
+
4
+ module PollBooth
5
+ extend ActiveSupport::Concern
6
+
7
+ require 'pollbooth/poller'
8
+ require 'pollbooth/railtie' if defined?(Rails)
9
+
10
+ mattr_accessor :pollers
11
+
12
+ included do
13
+ class << self
14
+ attr_accessor :poller
15
+ attr_accessor :ttl
16
+ attr_accessor :cache_block
17
+
18
+ @@pollbooth_mutex = Mutex.new
19
+ end
20
+ end
21
+
22
+ module ClassMethods
23
+ def start(cache_on=true)
24
+ raise "you must provide cache block before starting" if self.cache_block.nil?
25
+
26
+ self.class_variable_get(:@@pollbooth_mutex).synchronize do
27
+ self.stop
28
+ self.poller = Poller.new(self, cache_on)
29
+ end
30
+
31
+ PollBooth.pollers ||= []
32
+ PollBooth.pollers << self.poller
33
+ end
34
+
35
+ def stop
36
+ self.poller.stop if self.poller
37
+ end
38
+
39
+ def lookup(value)
40
+ self.start unless self.poller.try(:started?)
41
+
42
+ self.poller.lookup(value)
43
+ end
44
+
45
+ def cache(ttl, &block)
46
+ self.cache_block = block
47
+ self.ttl = ttl
48
+ end
49
+
50
+ def started?
51
+ self.poller.try(:started?) || false
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,42 @@
1
+ class PollBooth::Poller
2
+ def initialize(klass, cache_on)
3
+ @klass = klass
4
+ @instance = klass.new
5
+ @cache_on = cache_on
6
+ @ttl = @klass.ttl.seconds
7
+ @lock = Mutex.new
8
+
9
+ load_data # synchronous, blocking request so lookup always find something
10
+
11
+ if @cache_on
12
+ @timer = BigBen.new("PollBooth", @ttl) { load_data }
13
+ @timer.start
14
+ end
15
+ @started = true
16
+ end
17
+
18
+ def started?
19
+ @started == true
20
+ end
21
+
22
+ def load
23
+ raise "load must be defined in a subclass"
24
+ end
25
+
26
+ def lookup(value)
27
+ load_data unless @cache_on
28
+ @lock.synchronize { @cached_data.with_indifferent_access[value] }
29
+ end
30
+
31
+ def stop
32
+ @timer.reset if @cache_on
33
+ @started = false
34
+ end
35
+
36
+ private
37
+
38
+ def load_data
39
+ data = @instance.instance_eval(&@klass.cache_block)
40
+ @lock.synchronize { @cached_data = data }
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ module PollBooth
2
+ class PollBoothRailtie < ::Rails::Railtie
3
+ initializer 'pollbooth' do
4
+ ActionDispatch::Reloader.to_cleanup do
5
+ PollBooth.pollers.each(&:stop)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module PollBooth
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pollbooth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kareem Kouddous
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bigben
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ description: Data cache refreshed regularly
28
+ email:
29
+ - kareeknyc@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/pollbooth.rb
35
+ - lib/pollbooth/poller.rb
36
+ - lib/pollbooth/railtie.rb
37
+ - lib/pollbooth/version.rb
38
+ homepage: http://github.com/crowdtap/pollbooth
39
+ licenses: []
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.0
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Data cache refreshed regularly
61
+ test_files: []