telegram-bot 0.9.0.alpha1 → 0.9.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 590c2a861f53a992a435eb30c21a1425a1929baf
4
- data.tar.gz: e21c4caa819c6c7328d948c835bc807ed187d864
3
+ metadata.gz: 36cad9c75339c3e1119aec4f14645a7cdcb1c868
4
+ data.tar.gz: 0817596572b5667c47642cb2ad30dd5e51690a32
5
5
  SHA512:
6
- metadata.gz: 358bd9e8f43cf07a6b8a3714570c3dcfd6d59dd5b3066cd4a20b51f754f0315a9fb9db448d5f65fc0098ac45bb18fbb2b538c9480d7ab31b94b21878a093e71f
7
- data.tar.gz: 93846f47f24480df1288f51566df3d28d3be89230e974457fab2f4b4f8b604ce44cee97423173fadda47818e19fde3e14e4b8af029425f6289b90d26a6279e96
6
+ metadata.gz: d488af6c423477bc93fe182d73f9b104c45886097896dbd400bd29c2d5eb1455c6237a1eabd206397f9ba5799798b6d1b83936662e655b2606b0998136c5f228
7
+ data.tar.gz: 21468fbab556f7c31594e0b68be13f75b51465b6418b075e4441613435bb7dc5fe02a77ef6a3c6241475fd026792d02e55a1b106b0ea89e540a522157a47281c
@@ -30,12 +30,18 @@ module Telegram
30
30
  # client.send_message(message)
31
31
  # client.async(false) { client.send_message(other_one) }
32
32
  #
33
+ # `#async=` sets global value for all threads,
34
+ # while `#async(val, &block)` is thread-safe.
35
+ #
33
36
  # It can be set with custom job class or classname. By default it defines
34
37
  # job classes for every client class, inherited from ApplicationRecord, which
35
38
  # can be accessed via `.default_async_job`. You can integrate it with any
36
39
  # other job provider by defining a class with `.perform_later(bot_id, *args)`
37
40
  # method. See Async::Job for implemetation.
38
41
  module Async
42
+ # Used to track missing key in a hash in local variable.
43
+ MISSING_VALUE = Object.new.freeze
44
+
39
45
  module Job
40
46
  class << self
41
47
  def included(base)
@@ -78,6 +84,16 @@ module Telegram
78
84
  def prepare_async_args(*args)
79
85
  args
80
86
  end
87
+
88
+ # Returns default_async_job if `true` is given,
89
+ # treats String as a constant name, or bypasses any other values.
90
+ def prepare_async_val(val)
91
+ case val
92
+ when true then default_async_job
93
+ when String then Object.const_get(val)
94
+ else val
95
+ end
96
+ end
81
97
  end
82
98
 
83
99
  class << self
@@ -91,38 +107,46 @@ module Telegram
91
107
  hash = hash.dup
92
108
  hash.each { |key, val| hash[key] = val.to_s if val.is_a?(Symbol) }
93
109
  end
110
+
111
+ # Thread-local hash to store async config for every client.
112
+ def thread_store
113
+ Thread.current[:telegram_bot_async] ||= {}
114
+ end
94
115
  end
95
116
 
96
117
  attr_reader :id
97
118
 
98
- def initialize(*, id: nil, async: nil, **options)
119
+ def initialize(*, id: nil, async: nil, **)
99
120
  @id = id
100
121
  self.async = async
101
122
  super
102
123
  end
103
124
 
104
- # Sets `@async` to `self.class.default_async_job` if `true` is given
105
- # or uses given value.
106
- # Pass custom job class to perform async calls with.
125
+ # Sets default async value for all threads.
126
+ # Uses `self.class.prepare_async_val` to prepare value.
107
127
  def async=(val)
108
- @async =
109
- case val
110
- when true then self.class.default_async_job
111
- when String then const_get(val)
112
- else val
113
- end
128
+ @async = self.class.prepare_async_val(val)
114
129
  end
115
130
 
116
- # Returns value of `@async` if no block is given. Otherwise sets this value
117
- # for a block.
131
+ # Sets async value in a thread-safe way for the block.
132
+ # Uses `self.class.prepare_async_val` to prepare value.
133
+ #
134
+ # If no block is given returns previously set value or the global one,
135
+ # set by #async=.
118
136
  def async(val = true)
119
- return @async unless block_given?
137
+ thread_key = object_id
138
+ thread_store = Async.thread_store
139
+ return thread_store.fetch(thread_key) { @async } unless block_given?
120
140
  begin
121
- old_val = @async
122
- self.async = val
141
+ old_val = thread_store.fetch(thread_key) { MISSING_VALUE }
142
+ thread_store[thread_key] = self.class.prepare_async_val(val)
123
143
  yield
124
144
  ensure
125
- @async = old_val
145
+ if MISSING_VALUE == old_val
146
+ thread_store.delete(thread_key)
147
+ else
148
+ thread_store[thread_key] = old_val
149
+ end
126
150
  end
127
151
  end
128
152
 
@@ -1,6 +1,6 @@
1
1
  module Telegram
2
2
  module Bot
3
- VERSION = '0.9.0.alpha1'.freeze
3
+ VERSION = '0.9.0.alpha2'.freeze
4
4
 
5
5
  def self.gem_version
6
6
  Gem::Version.new VERSION
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.alpha1
4
+ version: 0.9.0.alpha2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Melentiev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-09 00:00:00.000000000 Z
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  version: 1.3.1
161
161
  requirements: []
162
162
  rubyforge_project:
163
- rubygems_version: 2.5.1
163
+ rubygems_version: 2.4.6
164
164
  signing_key:
165
165
  specification_version: 4
166
166
  summary: Library for building Telegram Bots with Rails integration