konstructor 0.4.0 → 0.4.1
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 +4 -4
- data/README.md +37 -22
- data/lib/konstructor/exceptions.rb +16 -9
- data/lib/konstructor/main.rb +52 -4
- data/lib/konstructor/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44c51074d47f8b7b6fef07e36772b81c4fc5de71
|
4
|
+
data.tar.gz: 840b1dea7dee69bceb9595d73d76a9722bc87455
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b245d765c1dde3f18b88e4efe57f597cb9057d63eaebf100c04d94fa22cfbb2dd2890260590fed106a3a88a301f83b1a5bbca659fb2d86367ee977313291e9d
|
7
|
+
data.tar.gz: f7e2d3ff8a3382fdf764829e32cda532a27257c57f12d6391fe5eaebf1852b493bd12db5afdf6d6f129528639bb5c0e856fc162b7d1d52e776db3834db588b77
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
[][gem]
|
2
|
-
[][travis]
|
3
|
+
[][coveralls]
|
4
4
|
|
5
5
|
[gem]: https://rubygems.org/gems/konstructor
|
6
6
|
[travis]: http://travis-ci.org/snovity/konstructor
|
7
|
-
[coveralls]: https://coveralls.io/
|
7
|
+
[coveralls]: https://coveralls.io/github/snovity/konstructor
|
8
8
|
|
9
9
|
# Konstructor
|
10
10
|
|
@@ -54,7 +54,7 @@ keyword in your classes only when you need it, see
|
|
54
54
|
In its simplest form `konstructor` declaration creates a
|
55
55
|
constructor from the next method.
|
56
56
|
|
57
|
-
|
57
|
+
```ruby
|
58
58
|
konstructor
|
59
59
|
def create
|
60
60
|
end
|
@@ -62,15 +62,15 @@ constructor from the next method.
|
|
62
62
|
konstructor
|
63
63
|
def recreate
|
64
64
|
end
|
65
|
-
|
65
|
+
```
|
66
66
|
|
67
67
|
When method names are given, it creates constructors from
|
68
68
|
those methods without affecting the next method.
|
69
69
|
|
70
|
-
|
70
|
+
```ruby
|
71
71
|
konstructor :create, :recreate
|
72
72
|
|
73
|
-
def
|
73
|
+
def not_a_constructor
|
74
74
|
end
|
75
75
|
|
76
76
|
def create
|
@@ -78,12 +78,12 @@ those methods without affecting the next method.
|
|
78
78
|
|
79
79
|
def recreate
|
80
80
|
end
|
81
|
-
|
81
|
+
```
|
82
82
|
|
83
|
-
|
84
|
-
|
83
|
+
Declaration with method names can be placed anywhere in
|
84
|
+
class definition.
|
85
85
|
|
86
|
-
|
86
|
+
```ruby
|
87
87
|
def create
|
88
88
|
end
|
89
89
|
konstructor :create
|
@@ -91,26 +91,39 @@ those methods without affecting the next method.
|
|
91
91
|
konstructor
|
92
92
|
def recreate
|
93
93
|
end
|
94
|
-
|
94
|
+
```
|
95
|
+
|
96
|
+
Several declarations may be used,
|
97
|
+
all declarations add up without overwriting each other.
|
98
|
+
```ruby
|
99
|
+
def create
|
100
|
+
end
|
101
|
+
|
102
|
+
konstructor :recreate
|
103
|
+
konstructor :create
|
104
|
+
|
105
|
+
def recreate
|
106
|
+
end
|
107
|
+
```
|
95
108
|
|
96
|
-
|
97
|
-
|
109
|
+
In all above cases `SomeClass` will have the default constructor
|
110
|
+
and two additional ones.
|
98
111
|
|
99
|
-
|
112
|
+
```ruby
|
100
113
|
obj0 = SomeClass.new
|
101
114
|
obj1 = SomeClass.create
|
102
115
|
obj2 = SomeClass.recreate
|
103
|
-
|
116
|
+
```
|
104
117
|
|
105
|
-
|
106
|
-
|
107
|
-
|
118
|
+
If you decide to remove the default Ruby constructor for some reason,
|
119
|
+
you can effectively do it by marking it as private using Ruby
|
120
|
+
method `private_class_method`:
|
108
121
|
|
109
|
-
|
122
|
+
```ruby
|
110
123
|
class SomeClass
|
111
124
|
private_class_method :new
|
112
125
|
end
|
113
|
-
|
126
|
+
```
|
114
127
|
|
115
128
|
#### Same as default constructor
|
116
129
|
|
@@ -198,7 +211,7 @@ it's roughly the same as declaring 3 properties with `attr_accessor`.
|
|
198
211
|
```ruby
|
199
212
|
attr_accessor :one, :two, :three
|
200
213
|
|
201
|
-
# following declaration
|
214
|
+
# following declaration takes the same time as above declaration
|
202
215
|
konstructor
|
203
216
|
def create
|
204
217
|
end
|
@@ -206,6 +219,8 @@ it's roughly the same as declaring 3 properties with `attr_accessor`.
|
|
206
219
|
See [Benchmarks page](https://github.com/snovity/konstructor/wiki/Benchmarks)
|
207
220
|
for details.
|
208
221
|
|
222
|
+
#### Dependencies
|
223
|
+
|
209
224
|
Konstructor doesn't depend on other gems.
|
210
225
|
|
211
226
|
#### Thread safety
|
@@ -1,24 +1,31 @@
|
|
1
1
|
module Konstructor
|
2
2
|
|
3
|
-
class
|
3
|
+
class Error < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
# Raised if reserved names <code>new</code> or <code>initialize</code>
|
7
|
+
# are used in declaration.
|
8
|
+
class ReservedNameError < Error
|
4
9
|
def initialize(name)
|
5
10
|
super "Custom constructor can't have name '#{name}', "
|
6
11
|
"it is reserved for default constructor."
|
7
12
|
end
|
8
13
|
end
|
9
14
|
|
10
|
-
|
11
|
-
|
12
|
-
super "Konstructor can't be included in module '#{base.name}' directly, " +
|
13
|
-
"please, use ActiveSupport::Concern or standard included hook."
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class DeclaringInheritedError < StandardError
|
15
|
+
# Raised if declaring inherited method as constructor.
|
16
|
+
class DeclaringInheritedError < Error
|
18
17
|
def initialize(name)
|
19
18
|
super "You are declaring an inherited method '#{name}' as konstructor, "
|
20
19
|
"this is not allowed."
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
23
|
+
# Raised if <code>konstructor</code> is used inside module.
|
24
|
+
class IncludingInModuleError < Error
|
25
|
+
def initialize(base)
|
26
|
+
super "Konstructor can't be included in module '#{base.name}' directly, " +
|
27
|
+
"please, use ActiveSupport::Concern or standard included hook."
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
24
31
|
end
|
data/lib/konstructor/main.rb
CHANGED
@@ -8,9 +8,57 @@ module Konstructor
|
|
8
8
|
module KonstructorMethod
|
9
9
|
private
|
10
10
|
|
11
|
-
#
|
12
|
-
|
13
|
-
|
11
|
+
# konstructor -> nil
|
12
|
+
# konstructor(symbol, ...) -> nil
|
13
|
+
# konstructor(string, ...) -> nil
|
14
|
+
#
|
15
|
+
# If used without params, declares next method as constructor.
|
16
|
+
#
|
17
|
+
# module SomeClass
|
18
|
+
# attr_reader :val
|
19
|
+
#
|
20
|
+
# konstructor
|
21
|
+
# def create(val)
|
22
|
+
# @val = val
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# If names are given, call can be placed anywhere, only methods with
|
27
|
+
# those names will be declared as constructors.
|
28
|
+
#
|
29
|
+
# module SomeClass
|
30
|
+
# attr_reader :val
|
31
|
+
#
|
32
|
+
# def create(val)
|
33
|
+
# @val = val
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# konstructor :create, :recreate
|
37
|
+
#
|
38
|
+
# def recreate(val)
|
39
|
+
# @val = val * 2
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# <em>then:</em>
|
44
|
+
#
|
45
|
+
# SomeClass.new.val
|
46
|
+
# => nil
|
47
|
+
# SomeClass.create(3).val
|
48
|
+
# => 3
|
49
|
+
# SomeClass.recreate(3).val
|
50
|
+
# => 6
|
51
|
+
#
|
52
|
+
# Can be used multiple times with various arguments,
|
53
|
+
# all calls add up without overwriting each other.
|
54
|
+
#
|
55
|
+
# Can raise several errors inheriting from <code>Konstructor::Error</code>
|
56
|
+
# ReservedNameError
|
57
|
+
# DeclaringInheritedError
|
58
|
+
# IncludingInModuleError
|
59
|
+
def konstructor(*several_variants)
|
60
|
+
Konstructor.declare(self, several_variants)
|
61
|
+
nil
|
14
62
|
end
|
15
63
|
end
|
16
64
|
|
@@ -72,7 +120,7 @@ module Konstructor
|
|
72
120
|
# It adds only one method 'konstructor'.
|
73
121
|
def append_features(klass)
|
74
122
|
unless klass.is_a? Class
|
75
|
-
raise
|
123
|
+
raise IncludingInModuleError, klass
|
76
124
|
end
|
77
125
|
|
78
126
|
klass.extend(KonstructorMethod)
|
data/lib/konstructor/version.rb
CHANGED