tins 1.17.0 → 1.18.0

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
  SHA256:
3
- metadata.gz: cb26b5aa4d1854350b2a520c593a0085a2a1097f9058f5635357189c83671dca
4
- data.tar.gz: 650ee7963f5b40ab1820becdabc32810309657c55a1774a0c97a5bfa74823a1d
3
+ metadata.gz: 78a0c250276797f603fe2ae06f3ce7344d08a8c5d85155c8de3c0604c2440bd9
4
+ data.tar.gz: 4fc9a4a4cdb05ac38be0b80d384e042e5e2a99c8334cd58c21af5aaa6728de7f
5
5
  SHA512:
6
- metadata.gz: 37fbc2ffb339c99c81622c9fc1303f22f200993e3a99d77e10b9b6f6fbfd90ee20f1814091f3d0ca49323a52ada93ecbce9e231b37820c07e15bad111517f8ad
7
- data.tar.gz: 798701836440f59d8d39a448935651ec5ed86a8b7375717b09d1611db19ff323361e8ac2052cf4c221b65b0ed1d861309b8eea82d61d18298e68388c7be9768f
6
+ metadata.gz: eae62a0aff396940e442ed9d00b1e566e9dff432713884e75b4012c2a73e913a316e68d06c1f04ef8825c2379f100ec45ef733cc626697d4ebaec47075981618
7
+ data.tar.gz: d05e7cdeebcc16a2f968e24b722ed32ee0dd88e67d6d838863211fbacaee639344efc1a999fa735e26744abb81314903983f183961d75c2806437bed22a97b8e
data/README.md CHANGED
@@ -12,7 +12,9 @@ Non yet.
12
12
 
13
13
  ## Changes
14
14
 
15
- * 2018-10-15 RElease 1.17.0
15
+ * 2018-11-02 Release 1.18.0
16
+ - `thread_local` and `thread_global` with default block
17
+ * 2018-10-15 Release 1.17.0
16
18
  - Add Tins::TempIO::Enum class to wrap `temp_io` into Enumerator.
17
19
  * 2017-12-05 Release 1.16.3
18
20
  - Improve `temp_io` method by not changing working dir.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.17.0
1
+ 1.18.0
data/lib/tins/dslkit.rb CHANGED
@@ -51,9 +51,16 @@ module Tins
51
51
  module ThreadGlobal
52
52
  # Define a thread global variable named _name_ in this module/class. If the
53
53
  # value _value_ is given, it is used to initialize the variable.
54
- def thread_global(name, default_value = nil)
54
+ def thread_global(name, default_value = nil, &default)
55
55
  is_a?(Module) or raise TypeError, "receiver has to be a Module"
56
56
 
57
+ default_value && default and raise ArgumentError,
58
+ "require either default_falue or default block"
59
+
60
+ if default_value
61
+ default = -> * { default_value }
62
+ end
63
+
57
64
  name = name.to_s
58
65
  var_name = "@__#{name}_#{__id__.abs}__"
59
66
 
@@ -61,14 +68,18 @@ module Tins
61
68
  modul = self
62
69
 
63
70
  define_method(name) do
64
- lock.synchronize { modul.instance_variable_get var_name }
71
+ lock.synchronize {
72
+ if default && !modul.instance_variable_defined?(var_name)
73
+ modul.instance_variable_set var_name, default.call
74
+ end
75
+ modul.instance_variable_get var_name
76
+ }
65
77
  end
66
78
 
67
79
  define_method(name + "=") do |value|
68
80
  lock.synchronize { modul.instance_variable_set var_name, value }
69
81
  end
70
82
 
71
- modul.instance_variable_set var_name, default_value if default_value
72
83
  self
73
84
  end
74
85
 
@@ -13,17 +13,27 @@ module Tins
13
13
 
14
14
  # Define a thread local variable named _name_ in this module/class. If the
15
15
  # value _value_ is given, it is used to initialize the variable.
16
- def thread_local(name, default_value = nil)
16
+ def thread_local(name, default_value = nil, &default)
17
17
  is_a?(Module) or raise TypeError, "receiver has to be a Module"
18
18
 
19
+ default_value && default and raise ArgumentError,
20
+ "require either default_falue or default block"
21
+
22
+ if default_value
23
+ default = -> * { default_value }
24
+ end
25
+
19
26
  name = name.to_s
20
27
  my_id = "__thread_local_#{__id__}__"
21
28
 
22
29
  ObjectSpace.define_finalizer(self, @@cleanup)
23
30
 
24
31
  define_method(name) do
25
- Thread.current[my_id] ||= {}
26
- Thread.current[my_id][name]
32
+ values = Thread.current[my_id] ||= {}
33
+ if default && !values.key?(name)
34
+ values[name] = default.call
35
+ end
36
+ values[name]
27
37
  end
28
38
 
29
39
  define_method("#{name}=") do |value|
@@ -31,20 +41,16 @@ module Tins
31
41
  Thread.current[my_id][name] = value
32
42
  end
33
43
 
34
- if default_value
35
- Thread.current[my_id] = {}
36
- Thread.current[my_id][name] = default_value
37
- end
38
44
  self
39
45
  end
40
46
 
41
47
  # Define a thread local variable for the current instance with name _name_.
42
48
  # If the value _value_ is given, it is used to initialize the variable.
43
- def instance_thread_local(name, value = nil)
49
+ def instance_thread_local(name, default_value = nil, &default)
44
50
  class << self
45
51
  extend Tins::ThreadLocal
46
52
  self
47
- end.thread_local name, value
53
+ end.thread_local name, default_value, &default
48
54
  self
49
55
  end
50
56
  end
data/lib/tins/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Tins
2
2
  # Tins version
3
- VERSION = '1.17.0'
3
+ VERSION = '1.18.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
data/tests/dslkit_test.rb CHANGED
@@ -9,6 +9,9 @@ class TL
9
9
  extend Tins::ThreadLocal
10
10
  thread_local :foo
11
11
 
12
+ extend Tins::ThreadLocal
13
+ thread_local :foo2 do {} end
14
+
12
15
  include Tins::ThreadLocal
13
16
  def make_baz
14
17
  instance_thread_local :baz
@@ -16,6 +19,8 @@ class TL
16
19
 
17
20
  extend Tins::ThreadGlobal
18
21
  thread_global :bar
22
+
23
+ thread_global :bar2 do {} end
19
24
  end
20
25
 
21
26
  class IE
@@ -122,6 +127,19 @@ class PoliteTest < Test::Unit::TestCase
122
127
  assert_equal @tl.baz, @tl2.baz
123
128
  end
124
129
 
130
+ def test_thread_local_with_default
131
+ assert_kind_of Hash, @tl.foo2
132
+ @tl.foo2[:hi] = 1
133
+ assert_equal 1, @tl.foo2[:hi]
134
+ thread = Thread.new do
135
+ assert_kind_of Hash, @tl.foo2
136
+ assert_nil @tl.foo2[:hi]
137
+ @tl.foo2[:hi] = 2
138
+ end
139
+ thread.join
140
+ assert_equal 1, @tl.foo2[:hi]
141
+ end
142
+
125
143
  def test_instance_thread_local
126
144
  assert_nil @tl.baz
127
145
  @tl.baz = 1
@@ -151,6 +169,18 @@ class PoliteTest < Test::Unit::TestCase
151
169
  assert_equal 2, @tl.bar
152
170
  end
153
171
 
172
+ def test_thread_global_with_default
173
+ assert_kind_of Hash, @tl.bar2
174
+ @tl.bar2[:hi] = 1
175
+ assert_equal 1, @tl.bar2[:hi]
176
+ thread = Thread.new do
177
+ assert_kind_of Hash, @tl.bar2
178
+ assert_equal 1, @tl.bar2[:hi]
179
+ end
180
+ thread.join
181
+ assert_equal 1, @tl.bar2[:hi]
182
+ end
183
+
154
184
  def test_instance_exec
155
185
  assert_equal :foo, @ie.foo
156
186
  assert_equal :foo, @ie.exec
data/tests/find_test.rb CHANGED
@@ -110,7 +110,7 @@ module Tins
110
110
  Tins::Find::Finder.new(visit: :foo, suffix: 'bla')
111
111
  end
112
112
  finder = Tins::Find::Finder.new(visit: -> path { path.stat.file? })
113
- f = File.open(fpath = File.join(@work_dir, 'foo.bar'), 'w')
113
+ File.new(fpath = File.join(@work_dir, 'foo.bar'), 'w').close
114
114
  mkdir_p(gpath = File.join(@work_dir, 'dir'))
115
115
  fpath = finder.prepare_path fpath
116
116
  gpath = finder.prepare_path gpath
data/tins.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: tins 1.17.0 ruby lib
2
+ # stub: tins 1.18.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "tins".freeze
6
- s.version = "1.17.0"
6
+ s.version = "1.18.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["Florian Frank".freeze]
11
- s.date = "2018-10-15"
11
+ s.date = "2018-11-02"
12
12
  s.description = "All the stuff that isn't good/big enough for a real library.".freeze
13
13
  s.email = "flori@ping.de".freeze
14
14
  s.extra_rdoc_files = ["README.md".freeze, "lib/dslkit.rb".freeze, "lib/dslkit/polite.rb".freeze, "lib/dslkit/rude.rb".freeze, "lib/spruz.rb".freeze, "lib/tins.rb".freeze, "lib/tins/alias.rb".freeze, "lib/tins/annotate.rb".freeze, "lib/tins/ask_and_send.rb".freeze, "lib/tins/attempt.rb".freeze, "lib/tins/bijection.rb".freeze, "lib/tins/case_predicate.rb".freeze, "lib/tins/complete.rb".freeze, "lib/tins/concern.rb".freeze, "lib/tins/count_by.rb".freeze, "lib/tins/date_dummy.rb".freeze, "lib/tins/date_time_dummy.rb".freeze, "lib/tins/deep_const_get.rb".freeze, "lib/tins/deep_dup.rb".freeze, "lib/tins/dslkit.rb".freeze, "lib/tins/duration.rb".freeze, "lib/tins/expose.rb".freeze, "lib/tins/extract_last_argument_options.rb".freeze, "lib/tins/file_binary.rb".freeze, "lib/tins/find.rb".freeze, "lib/tins/generator.rb".freeze, "lib/tins/go.rb".freeze, "lib/tins/hash_symbolize_keys_recursive.rb".freeze, "lib/tins/hash_union.rb".freeze, "lib/tins/if_predicate.rb".freeze, "lib/tins/implement.rb".freeze, "lib/tins/limited.rb".freeze, "lib/tins/lines_file.rb".freeze, "lib/tins/memoize.rb".freeze, "lib/tins/method_description.rb".freeze, "lib/tins/minimize.rb".freeze, "lib/tins/module_group.rb".freeze, "lib/tins/named_set.rb".freeze, "lib/tins/null.rb".freeze, "lib/tins/once.rb".freeze, "lib/tins/p.rb".freeze, "lib/tins/partial_application.rb".freeze, "lib/tins/proc_compose.rb".freeze, "lib/tins/proc_prelude.rb".freeze, "lib/tins/range_plus.rb".freeze, "lib/tins/require_maybe.rb".freeze, "lib/tins/responding.rb".freeze, "lib/tins/secure_write.rb".freeze, "lib/tins/sexy_singleton.rb".freeze, "lib/tins/string_byte_order_mark.rb".freeze, "lib/tins/string_camelize.rb".freeze, "lib/tins/string_underscore.rb".freeze, "lib/tins/string_version.rb".freeze, "lib/tins/subhash.rb".freeze, "lib/tins/temp_io.rb".freeze, "lib/tins/temp_io_enum.rb".freeze, "lib/tins/terminal.rb".freeze, "lib/tins/thread_local.rb".freeze, "lib/tins/time_dummy.rb".freeze, "lib/tins/to.rb".freeze, "lib/tins/to_proc.rb".freeze, "lib/tins/token.rb".freeze, "lib/tins/uniq_by.rb".freeze, "lib/tins/unit.rb".freeze, "lib/tins/version.rb".freeze, "lib/tins/write.rb".freeze, "lib/tins/xt.rb".freeze, "lib/tins/xt/annotate.rb".freeze, "lib/tins/xt/ask_and_send.rb".freeze, "lib/tins/xt/attempt.rb".freeze, "lib/tins/xt/blank.rb".freeze, "lib/tins/xt/case_predicate.rb".freeze, "lib/tins/xt/complete.rb".freeze, "lib/tins/xt/concern.rb".freeze, "lib/tins/xt/count_by.rb".freeze, "lib/tins/xt/date_dummy.rb".freeze, "lib/tins/xt/date_time_dummy.rb".freeze, "lib/tins/xt/deep_const_get.rb".freeze, "lib/tins/xt/deep_dup.rb".freeze, "lib/tins/xt/dslkit.rb".freeze, "lib/tins/xt/expose.rb".freeze, "lib/tins/xt/extract_last_argument_options.rb".freeze, "lib/tins/xt/file_binary.rb".freeze, "lib/tins/xt/full.rb".freeze, "lib/tins/xt/hash_symbolize_keys_recursive.rb".freeze, "lib/tins/xt/hash_union.rb".freeze, "lib/tins/xt/if_predicate.rb".freeze, "lib/tins/xt/implement.rb".freeze, "lib/tins/xt/irb.rb".freeze, "lib/tins/xt/method_description.rb".freeze, "lib/tins/xt/named.rb".freeze, "lib/tins/xt/null.rb".freeze, "lib/tins/xt/p.rb".freeze, "lib/tins/xt/partial_application.rb".freeze, "lib/tins/xt/proc_compose.rb".freeze, "lib/tins/xt/proc_prelude.rb".freeze, "lib/tins/xt/range_plus.rb".freeze, "lib/tins/xt/require_maybe.rb".freeze, "lib/tins/xt/responding.rb".freeze, "lib/tins/xt/secure_write.rb".freeze, "lib/tins/xt/sexy_singleton.rb".freeze, "lib/tins/xt/string.rb".freeze, "lib/tins/xt/string_byte_order_mark.rb".freeze, "lib/tins/xt/string_camelize.rb".freeze, "lib/tins/xt/string_underscore.rb".freeze, "lib/tins/xt/string_version.rb".freeze, "lib/tins/xt/subhash.rb".freeze, "lib/tins/xt/temp_io.rb".freeze, "lib/tins/xt/time_dummy.rb".freeze, "lib/tins/xt/time_freezer.rb".freeze, "lib/tins/xt/to.rb".freeze, "lib/tins/xt/uniq_by.rb".freeze, "lib/tins/xt/write.rb".freeze]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tins
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.17.0
4
+ version: 1.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-15 00:00:00.000000000 Z
11
+ date: 2018-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar