konstructor 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f8f7d3b8b3ecacc95f4337c122cfc4512a77c54
4
- data.tar.gz: 142074c98c848d53e43a813d20306d95f275323d
3
+ metadata.gz: e2e01d5343a1a1b9e7e1ec11b75f657717721964
4
+ data.tar.gz: a399ad13acee21bf15a41a0b19928442b4923c8e
5
5
  SHA512:
6
- metadata.gz: 3ac30ef4daa2c4f8c90470b99012df4e506b7f81e846a8ab93d1f6e7407fcdf9d0887ef4e55db0dc02811c97dfffe0cc5ea58372ee59b18d4c22790689887629
7
- data.tar.gz: c09a1ea90372ed7a4b49facfabdc84a21df96032095dc01ed2947fb9d4bc3b0c2c3d4eeacd3b874c708093c32fa70ad74664f5a944d43ed02311f329c7d9409d
6
+ metadata.gz: 856443c125c10b4b8110bcc4289b8d1c03894a9f8d7152873a386e2bd8a7324062e2765a445db16574f9d06852d28c20ae5676ef8ee0c07927bb450bb54b0485
7
+ data.tar.gz: 182d3b562c86109bf3d7368fbc11ccc426f03c23c7b0d990b1a1d7c6d9950a7ec50c917866a6b7099f6a3afa93f3718d4710d4b5ea001a1b797956db5a1c732c
data/README.md CHANGED
@@ -239,9 +239,9 @@ since all work is done during class definition and then it's just
239
239
  standard Ruby instance creation.
240
240
 
241
241
  Cost of `konstructor` declaration at initial load time is roughly the
242
- same as declaring 3 properties with `attr_accessor`.
242
+ same as declaring 5 properties with `attr_accessor`.
243
243
  ```ruby
244
- attr_accessor :one, :two, :three
244
+ attr_accessor :one, :two, :three, :four, :five
245
245
 
246
246
  # following declaration takes the same time as above declaration
247
247
  konstructor
@@ -254,11 +254,13 @@ for details.
254
254
  #### Dependencies and requirements
255
255
 
256
256
  Konstructor doesn't depend on other gems.
257
- Requires Ruby `1.9.3` or higher.
257
+ Requires Ruby `1.9.3` or higher. Works with JRuby.
258
258
 
259
259
  #### Thread safety
260
260
 
261
- Konstructor is thread safe.
261
+ Konstructor is thread-safe in both CRuby and JRuby,
262
+ see [Thread safety page](https://github.com/snovity/konstructor/wiki/Thread-safety)
263
+ for details.
262
264
 
263
265
  ## Contributing
264
266
 
data/Rakefile CHANGED
@@ -12,25 +12,53 @@ task :benchmark do
12
12
  n = 10000
13
13
 
14
14
  Benchmark.bm(30) do |x|
15
- x.report('def:') do
15
+ x.report('25 def:') do
16
16
  n.times do
17
17
  Class.new do
18
- def one; end
19
- def two; end
20
- def three; end
21
- def four; end
22
- def five; end
18
+ def one1; end
19
+ def two1; end
20
+ def three1; end
21
+ def four1; end
22
+ def five1; end
23
+
24
+ def one2; end
25
+ def two2; end
26
+ def three2; end
27
+ def four2; end
28
+ def five2; end
29
+
30
+ def one3; end
31
+ def two3; end
32
+ def three3; end
33
+ def four3; end
34
+ def five3; end
35
+
36
+ def one4; end
37
+ def two4; end
38
+ def three4; end
39
+ def four4; end
40
+ def five4; end
41
+
42
+ def one5; end
43
+ def two5; end
44
+ def three5; end
45
+ def four5; end
46
+ def five5; end
23
47
  end
24
48
  end
25
49
  end
26
- x.report('attr_accessor:') do
50
+ x.report('25 props via attr_accessor:') do
27
51
  n.times do
28
52
  Class.new do
29
- attr_accessor :one, :two, :three, :four, :five
53
+ attr_accessor :one1, :two1, :three1, :four1, :five1,
54
+ :one2, :two2, :three2, :four2, :five2,
55
+ :one3, :two3, :three3, :four3, :five3,
56
+ :one4, :two4, :three4, :four4, :five4,
57
+ :one5, :two5, :three5, :four5, :five5
30
58
  end
31
59
  end
32
60
  end
33
- x.report('konstructor after:') do
61
+ x.report(' 5 konstructors after:') do
34
62
  n.times do
35
63
  Class.new do
36
64
  def one; end
@@ -43,7 +71,7 @@ task :benchmark do
43
71
  end
44
72
  end
45
73
  end
46
- x.report('konstructor before:') do
74
+ x.report(' 5 konstructors before:') do
47
75
  n.times do
48
76
  Class.new do
49
77
  konstructor :one, :two, :three, :four, :five
@@ -56,7 +84,7 @@ task :benchmark do
56
84
  end
57
85
  end
58
86
  end
59
- x.report('konstructor nameless:') do
87
+ x.report(' 5 konstructors nameless:') do
60
88
  n.times do
61
89
  Class.new do
62
90
  konstructor
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
- require_relative 'lib/konstructor/version'
2
+ # require_relative 'lib/konstructor/version' <- doesn't work with Ruby 1.9.3 in gemspec when building gem
3
+ require File.expand_path('../lib/konstructor/version', __FILE__)
4
+
3
5
 
4
6
  Gem::Specification.new do |spec|
5
7
  spec.name = 'konstructor'
@@ -5,14 +5,16 @@ module Konstructor
5
5
  def initialize(klass)
6
6
  @klass = klass
7
7
  @konstructor_names = []
8
- @next_method_is_konstructor = false
8
+ # converting string to symbol for much quicker hash lookups
9
+ @thread_next_method_var_name = :"konstructor#{object_id}"
10
+ reset_next_method_is_konstructor!
9
11
  end
10
12
 
11
13
  def declare(new_names)
12
14
  if new_names.empty?
13
- @next_method_is_konstructor = true
15
+ next_method_is_konstructor!
14
16
  else
15
- @next_method_is_konstructor = false
17
+ reset_next_method_is_konstructor!
16
18
  process_new_names(new_names)
17
19
  end
18
20
  end
@@ -24,8 +26,8 @@ module Konstructor
24
26
  def method_added_to_klass(name)
25
27
  name = name.to_sym
26
28
 
27
- if @next_method_is_konstructor
28
- @next_method_is_konstructor = false
29
+ if next_method_is_konstructor?
30
+ reset_next_method_is_konstructor!
29
31
  @konstructor_names << name
30
32
  process_declaration(name)
31
33
  elsif declared?(name)
@@ -35,6 +37,18 @@ module Konstructor
35
37
 
36
38
  private
37
39
 
40
+ def next_method_is_konstructor!
41
+ Thread.current[@thread_next_method_var_name] = true
42
+ end
43
+
44
+ def reset_next_method_is_konstructor!
45
+ Thread.current[@thread_next_method_var_name] = false
46
+ end
47
+
48
+ def next_method_is_konstructor?
49
+ Thread.current[@thread_next_method_var_name]
50
+ end
51
+
38
52
  def declared_in_self?(name)
39
53
  @konstructor_names.include?(name.to_sym)
40
54
  end
@@ -67,6 +67,8 @@ module Konstructor
67
67
  DEFAULT_NAMES = [:initialize]
68
68
  RESERVED_NAMES = [:new, :initialize]
69
69
 
70
+ extend MonitorMixin
71
+
70
72
  class << self
71
73
  def reserved?(name)
72
74
  RESERVED_NAMES.include?(name.to_sym)
@@ -90,11 +92,15 @@ module Konstructor
90
92
 
91
93
  def declare(klass, new_method_names)
92
94
  setup_method_added_hook(klass)
93
- get_or_init_factory(klass).declare(new_method_names)
95
+ synchronize do
96
+ get_or_init_factory(klass).declare(new_method_names)
97
+ end
94
98
  end
95
99
 
96
100
  def method_added_to_klass(klass, method_name)
97
- get_or_init_factory(klass).method_added_to_klass(method_name)
101
+ synchronize do
102
+ get_or_init_factory(klass).method_added_to_klass(method_name)
103
+ end
98
104
  end
99
105
 
100
106
  def is?(klass, method_name)
@@ -1,3 +1,3 @@
1
1
  module Konstructor
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konstructor
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dima Lashkov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-26 00:00:00.000000000 Z
11
+ date: 2017-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler