konstructor 0.3.0 → 0.4.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
  SHA1:
3
- metadata.gz: 1a639335de2f050fdd01c8504178cc934204ada8
4
- data.tar.gz: d6a81f610da6159060b39b9209a4a0913b2fe7c2
3
+ metadata.gz: 8450249cb7a42cc6608c42e20d66d897c73779b6
4
+ data.tar.gz: cf0e8b2bdb6643b4debcb2cc67c11ba5c07b159f
5
5
  SHA512:
6
- metadata.gz: ae74c9aedf9971bd55fd2d709014631f39d1cff931d3d460b8084f30de57fe881599b264c657aa5431178d2aea5fa900d6820bc32e76c9b6f91422788f0e7707
7
- data.tar.gz: eb8be3d92e5c96bd2a15580f8ec1dde0bf5d0a0648f58eabec393d321e27ffc74f3871aa62e931074f2e52cec1f58bb065dec898390c5b3e23dae5a9595bd6a7
6
+ metadata.gz: 23c32f2916a283b9ce2d26098dc2d13159bea35f3db0b109d01e4d92a43c7b50471b3b5a5c7f7d128fc1d821b5945ca5155f1f2095e1f4569dd88518a2b1f821
7
+ data.tar.gz: 47987108eb3206829bb5cb26f4d38c692edc1ced8da0a64cbbe46e66adeb833ec871b899a665838cb59a330713e0b526771f8a1be28287760e6756dc48623517
data/README.md CHANGED
@@ -8,10 +8,11 @@
8
8
 
9
9
  # Konstructor
10
10
 
11
- Konstructor is a small gem that gives you multiple
11
+ This is a small gem that gives you multiple
12
12
  constructors in Ruby.
13
13
 
14
- Use `konstructor` keyword to declare constructors additional to the defaul one:
14
+ Use `konstructor` keyword to declare constructors additional
15
+ to the defaul one:
15
16
  ```ruby
16
17
  class SomeClass
17
18
  konstructor
@@ -158,8 +159,8 @@ for details.
158
159
 
159
160
  #### Using with other gems
160
161
 
161
- Konstructor doesn't affect other gems, including those
162
- that depend on metaprogramming, such as
162
+ Konstructor doesn't affect other gems depending on metaprogramming,
163
+ such as
163
164
  [rake](https://github.com/ruby/rake),
164
165
  [thor](https://github.com/erikhuda/thor),
165
166
  [contracts](https://github.com/egonSchiele/contracts.ruby), etc.
@@ -192,6 +193,19 @@ Konstructor does all its work when class is being defined. Once class
192
193
  has been defined, it's just standard Ruby instance creation.
193
194
  Therefore, there is no runtime performance penalty.
194
195
 
196
+ As for the cost of declaring a constructor at initial load time,
197
+ it's roughly the same as declaring 3 properties with `attr_accessor`.
198
+ ```ruby
199
+ attr_accessor :one, :two, :three
200
+
201
+ # following declaration take the same time as above declaration
202
+ konstructor
203
+ def create
204
+ end
205
+ ```
206
+ See [Benchmarks page](https://github.com/snovity/konstructor/wiki/Benchmarks)
207
+ for details.
208
+
195
209
  Konstructor doesn't depend on other gems.
196
210
 
197
211
  #### Thread safety
data/Rakefile CHANGED
@@ -4,3 +4,73 @@ require "rspec/core/rake_task"
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
6
  task :default => :spec
7
+
8
+ task :benchmark do
9
+ require 'konstructor'
10
+ require 'benchmark'
11
+
12
+ n = 10000
13
+
14
+ Benchmark.bm(30) do |x|
15
+ x.report('def:') do
16
+ n.times do
17
+ Class.new do
18
+ def one; end
19
+ def two; end
20
+ def three; end
21
+ def four; end
22
+ def five; end
23
+ end
24
+ end
25
+ end
26
+ x.report('attr_accessor:') do
27
+ n.times do
28
+ Class.new do
29
+ attr_accessor :one, :two, :three, :four, :five
30
+ end
31
+ end
32
+ end
33
+ x.report('konstructor after:') do
34
+ n.times do
35
+ Class.new do
36
+ def one; end
37
+ def two; end
38
+ def three; end
39
+ def four; end
40
+ def five; end
41
+
42
+ konstructor :one, :two, :three, :four, :five
43
+ end
44
+ end
45
+ end
46
+ x.report('konstructor before:') do
47
+ n.times do
48
+ Class.new do
49
+ konstructor :one, :two, :three, :four, :five
50
+
51
+ def one; end
52
+ def two; end
53
+ def three; end
54
+ def four; end
55
+ def five; end
56
+ end
57
+ end
58
+ end
59
+ x.report('konstructor nameless:') do
60
+ n.times do
61
+ Class.new do
62
+ konstructor
63
+ def one; end
64
+ konstructor
65
+ def two; end
66
+ konstructor
67
+ def three; end
68
+ konstructor
69
+ def four; end
70
+ konstructor
71
+ def five; end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -93,13 +93,21 @@ module Konstructor
93
93
  end
94
94
 
95
95
  def define_factory(name)
96
- @klass.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
97
- def #{name}(*args, &block)
98
- instance = allocate
99
- instance.__send__(:#{name}, *args, &block)
100
- instance
101
- end
102
- RUBY
96
+ # this works roughly 5 times slower on Ruby 2.3 than block version,
97
+ # probably because block version is converted to byte code
98
+ # @klass.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
99
+ # def #{name}(*args, &block)
100
+ # instance = allocate
101
+ # instance.__send__(:#{name}, *args, &block)
102
+ # instance
103
+ # end
104
+ # RUBY
105
+
106
+ @klass.define_singleton_method(name) do |*args, &block|
107
+ instance = allocate
108
+ instance.send(name, *args, &block)
109
+ instance
110
+ end
103
111
  end
104
112
 
105
113
  def mark_as_private(name)
@@ -1,3 +1,3 @@
1
1
  module Konstructor
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konstructor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dima Lashkov