konstructor 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +213 -0
- data/Rakefile +6 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/konstructor.gemspec +39 -0
- data/lib/konstructor.rb +82 -0
- data/lib/konstructor/core_ext.rb +7 -0
- data/lib/konstructor/exceptions.rb +17 -0
- data/lib/konstructor/factory.rb +102 -0
- data/lib/konstructor/konstructor_method_hook.rb +31 -0
- data/lib/konstructor/simple_method_hook.rb +19 -0
- data/lib/konstructor/version.rb +3 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 38789189bfb0b4e7adc76b863632efa957a746ca
|
4
|
+
data.tar.gz: 3f39bd9d6e2a9a5fe687d244d8ffa967681cb6c0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a91c8148355e831f46d1660a682629f232a4cf2aefae16ddf43051ffcef46ed5b5c53ce81cabdc5c6aebe95ffb23c26a46e5038f6279afaa99cd9545144d519d
|
7
|
+
data.tar.gz: 47fa9923f1ff942536926e74c128c294715db18f9c370252b11ed08780c897ea4fc3695a2b7288e6f34436fa640d16d366324c709390f24df6ada94ee41014a8
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at dm.lashkov@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Dima Lashkov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
# Konstructor
|
2
|
+
|
3
|
+
Konstructor is a small gem that gives you multiple
|
4
|
+
constructors in Ruby.
|
5
|
+
|
6
|
+
To define custom constructors use `konstructor` keyword:
|
7
|
+
```ruby
|
8
|
+
class SomeClass
|
9
|
+
konstructor
|
10
|
+
def create(val)
|
11
|
+
@val = val
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :val
|
15
|
+
end
|
16
|
+
|
17
|
+
obj0 = SomeClass.new
|
18
|
+
obj0.val # nil
|
19
|
+
|
20
|
+
obj1 = SomeClass.create(3)
|
21
|
+
obj1.val # 3
|
22
|
+
```
|
23
|
+
It's similar to having overloaded constructors in other languages.
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Via Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'konstructor' #, require: 'konstructor/core_ext'
|
31
|
+
```
|
32
|
+
|
33
|
+
and then execute `bundle`.
|
34
|
+
|
35
|
+
Uncomment require option to skip adding
|
36
|
+
`include Konstructor` every time you want to use `konstructor` keyword.
|
37
|
+
|
38
|
+
You can also install it without Bundler:
|
39
|
+
|
40
|
+
$ gem install konstructor
|
41
|
+
|
42
|
+
## Usage
|
43
|
+
|
44
|
+
When no names are given `konstructor` just affects the next method:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
konstructor
|
48
|
+
def create
|
49
|
+
end
|
50
|
+
|
51
|
+
konstructor
|
52
|
+
def recreate
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
When names are given, it makes those methods konstructors:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
konstructor :create, :recreate
|
60
|
+
|
61
|
+
def create
|
62
|
+
end
|
63
|
+
|
64
|
+
def recreate
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
Call with names can be placed anywhere in class definition:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
def create
|
72
|
+
end
|
73
|
+
konstructor :create
|
74
|
+
|
75
|
+
konstructor
|
76
|
+
def recreate
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
In all above cases the class will have the default constructor
|
81
|
+
and two custom ones:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
obj0 = SomeClass.new
|
85
|
+
obj1 = SomeClass.create
|
86
|
+
obj2 = SomeClass.recreate
|
87
|
+
```
|
88
|
+
|
89
|
+
### Same as default constructor
|
90
|
+
|
91
|
+
Konstructors work exactly the same way as built-in Ruby constructor.
|
92
|
+
You can pass blocks to them:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
konstructor
|
96
|
+
def create(val)
|
97
|
+
@val = yield val
|
98
|
+
end
|
99
|
+
|
100
|
+
obj = SomeClass.create(3) { |v| v*3 }
|
101
|
+
obj.val # 9
|
102
|
+
```
|
103
|
+
|
104
|
+
You can override konstructors in subclasses and call `super`.
|
105
|
+
Once method is a marked as konstructor in hierarchy,
|
106
|
+
it is always a konstructor.
|
107
|
+
|
108
|
+
Methods inherited from superclasses can't become konstructors in
|
109
|
+
subclasses. To achieve the effect, define a new method,
|
110
|
+
mark it as konstructor and call the inherited one.
|
111
|
+
|
112
|
+
### Reserved names
|
113
|
+
|
114
|
+
Using reserved method names `new` and `initialize` for custom
|
115
|
+
constructor definition will raise an error:
|
116
|
+
```ruby
|
117
|
+
konstructor
|
118
|
+
def initialize # raises Konstructor::ReservedNameError
|
119
|
+
end
|
120
|
+
```
|
121
|
+
or
|
122
|
+
```ruby
|
123
|
+
konstructor
|
124
|
+
def new # raises Konstructor::ReservedNameError
|
125
|
+
end
|
126
|
+
```
|
127
|
+
|
128
|
+
### Defining konstructors in Modules
|
129
|
+
|
130
|
+
Modules can't have konstructors. Use `ActiveSupport::Concern` and
|
131
|
+
define konstructor in `included` block.
|
132
|
+
|
133
|
+
### Using with other gems
|
134
|
+
|
135
|
+
Konstructor doesn't affect other gems, including those
|
136
|
+
that depend on metaprogramming, such as rake, thor, contracts, etc.
|
137
|
+
|
138
|
+
For instnace, Konstructor works with contracts gem:
|
139
|
+
```ruby
|
140
|
+
class SomeClass
|
141
|
+
konstructor
|
142
|
+
Contract Num => SomeClass
|
143
|
+
def create(some_number)
|
144
|
+
@number = some_number
|
145
|
+
end
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
If you stumble upon a metaprogramming gem that
|
150
|
+
conflicts with Konstructor, please open an issue.
|
151
|
+
|
152
|
+
### Removing default constructor
|
153
|
+
|
154
|
+
You can effectively remove default Ruby construtor
|
155
|
+
by marking it as private:
|
156
|
+
```ruby
|
157
|
+
class SomeClass
|
158
|
+
private_class_method :new
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
## Performance
|
163
|
+
|
164
|
+
Konstructor does all its work when class is being defined. Once class
|
165
|
+
has been defined, it's just standard Ruby instance creation.
|
166
|
+
Therefore, it's as fast as standard Ruby constructor.
|
167
|
+
|
168
|
+
If there is a slowdown during startup, it should be comparable
|
169
|
+
to the one of `attr_accessor` or `ActiveSupport::Concern`.
|
170
|
+
|
171
|
+
## Thread safety
|
172
|
+
|
173
|
+
Konstructor is thread safe.
|
174
|
+
|
175
|
+
## Details
|
176
|
+
|
177
|
+
Ruby constructor is a pair consisting of public factory method defined
|
178
|
+
on a class and a private instance method. Therefore,
|
179
|
+
`konstructor` marks instance method as private and defines a
|
180
|
+
corresponding public class method with the same name.
|
181
|
+
|
182
|
+
You can check if certain instance method name has been declared as
|
183
|
+
constructor or is a default constructor by running.
|
184
|
+
```ruby
|
185
|
+
Konstructor.is?(SomeClass, :initialize) # true
|
186
|
+
Konstructor.is?(SomeClass, :create) # true
|
187
|
+
Konstructor.is?(SomeClass, :recreate) # true
|
188
|
+
Konstructor.is?(SomeClass, :something_else) # false
|
189
|
+
```
|
190
|
+
|
191
|
+
It will return true even if there is not such constructor has
|
192
|
+
been defined yet. Like:
|
193
|
+
```ruby
|
194
|
+
class SomeClass
|
195
|
+
konstructor :create
|
196
|
+
end
|
197
|
+
```
|
198
|
+
Konstructor body may be supplied in subclasses.
|
199
|
+
|
200
|
+
## Contributing
|
201
|
+
|
202
|
+
Bug reports and pull requests are welcome on GitHub at
|
203
|
+
https://github.com/snovity/konstructor. This project is intended to be
|
204
|
+
a safe, welcoming space for collaboration, and contributors are
|
205
|
+
expected to adhere to the
|
206
|
+
[Contributor Covenant](http://contributor-covenant.org)
|
207
|
+
code of conduct.
|
208
|
+
|
209
|
+
## License
|
210
|
+
|
211
|
+
The gem is available as open source under the terms of the
|
212
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
213
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'konstructor'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require 'pry'
|
11
|
+
Pry.start
|
data/bin/setup
ADDED
data/konstructor.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require_relative 'lib/konstructor/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'konstructor'
|
6
|
+
spec.version = Konstructor::VERSION
|
7
|
+
spec.authors = ['Dima Lashkov']
|
8
|
+
spec.email = ['dm.lashkov@gmail.com']
|
9
|
+
|
10
|
+
spec.summary = %q{Multiple constructors in Ruby.}
|
11
|
+
# few paragraphs with no examples or formatting
|
12
|
+
spec.description = %q{
|
13
|
+
Konstructor is a small gem that gives you multiple constructors in Ruby.
|
14
|
+
It's similar to having overloaded constructors in other languages.
|
15
|
+
}
|
16
|
+
spec.homepage = 'https://github.com/snovity/konstructor'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
spec.required_ruby_version = '>= 2.0.0'
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
# the reason to use exe is to distinguish between gem development binaries
|
24
|
+
# and gem runtime binaries
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
30
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
32
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.4'
|
33
|
+
spec.add_development_dependency 'activesupport', '~> 4.2'
|
34
|
+
|
35
|
+
# to test conflicts, specifying precise versions since below version 1 and no semver guarantees
|
36
|
+
spec.add_development_dependency 'thor', '0.19.4'
|
37
|
+
spec.add_development_dependency 'contracts', '0.14.0'
|
38
|
+
spec.add_development_dependency 'constructor', '2.0.0'
|
39
|
+
end
|
data/lib/konstructor.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'konstructor/version'
|
2
|
+
require 'konstructor/exceptions'
|
3
|
+
require 'konstructor/simple_method_hook'
|
4
|
+
require 'konstructor/factory'
|
5
|
+
|
6
|
+
module Konstructor
|
7
|
+
|
8
|
+
module KonstructorMethod
|
9
|
+
private
|
10
|
+
|
11
|
+
# TODO: ADD DOCS
|
12
|
+
def konstructor(*new_names)
|
13
|
+
Konstructor.declare(self, new_names)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
DEFAULT_NAMES = [:initialize]
|
18
|
+
RESERVED_NAMES = [:new, :initialize]
|
19
|
+
|
20
|
+
class << self
|
21
|
+
def reserved?(name)
|
22
|
+
RESERVED_NAMES.include?(name.to_sym)
|
23
|
+
end
|
24
|
+
|
25
|
+
def default?(name)
|
26
|
+
DEFAULT_NAMES.include?(name.to_sym)
|
27
|
+
end
|
28
|
+
|
29
|
+
def declared?(klass, name)
|
30
|
+
konstructor = get_factory(klass)
|
31
|
+
if konstructor
|
32
|
+
konstructor.declared?(name.to_sym)
|
33
|
+
else
|
34
|
+
false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def declare(klass, new_names)
|
39
|
+
setup_method_added_hook(klass)
|
40
|
+
get_or_init_factory(klass).declare(new_names)
|
41
|
+
end
|
42
|
+
|
43
|
+
def method_added_to_klass(klass, name)
|
44
|
+
get_or_init_factory(klass).method_added_to_klass(name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def is?(klass, name)
|
48
|
+
default?(name) || declared?(klass, name)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def get_factory(klass)
|
54
|
+
klass.instance_variable_get(:@konstructor)
|
55
|
+
end
|
56
|
+
|
57
|
+
def init_factory(klass)
|
58
|
+
# using variable @konstructor to minimize footprint, although saving factory there
|
59
|
+
klass.instance_variable_set(:@konstructor, Factory.new(klass))
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_or_init_factory(klass)
|
63
|
+
get_factory(klass) || init_factory(klass)
|
64
|
+
end
|
65
|
+
|
66
|
+
def setup_method_added_hook(klass)
|
67
|
+
SimpleMethodHook.setup(klass)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Overriden append_features prevents default behavior
|
71
|
+
# of including all the constants, variables to the base class.
|
72
|
+
# It adds only one method 'konstructor'.
|
73
|
+
def append_features(klass)
|
74
|
+
unless klass.is_a? Class
|
75
|
+
raise IncludeInModuleError, klass
|
76
|
+
end
|
77
|
+
|
78
|
+
klass.extend(KonstructorMethod)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Konstructor
|
2
|
+
|
3
|
+
class ReservedNameError < StandardError
|
4
|
+
def initialize(name)
|
5
|
+
super "Custom constructor can't have name '#{name}', "
|
6
|
+
"it is reserved for default constructor."
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class IncludeInModuleError < StandardError
|
11
|
+
def initialize(base)
|
12
|
+
super "Konstructor can't be included in module '#{base.name}' directly, " +
|
13
|
+
"please, use ActiveSupport::Concern or included hook directly."
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Konstructor
|
2
|
+
class Factory
|
3
|
+
|
4
|
+
def initialize(klass)
|
5
|
+
@klass = klass
|
6
|
+
@konstructor_names = []
|
7
|
+
@next_method_is_konstructor = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def declare(new_names)
|
11
|
+
if new_names.empty?
|
12
|
+
@next_method_is_konstructor = true
|
13
|
+
else
|
14
|
+
@next_method_is_konstructor = false
|
15
|
+
process_new_names(new_names)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# once method is a konstructor, it is always a konstructor, this differs
|
20
|
+
# from the way private, protected works, if overriding method isn't repeatedly marked as private
|
21
|
+
# it becomes public
|
22
|
+
def declared?(name)
|
23
|
+
declared_in_self?(name) || declared_in_superclass?(name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_added_to_klass(name)
|
27
|
+
name = name.to_sym
|
28
|
+
|
29
|
+
if @next_method_is_konstructor
|
30
|
+
@next_method_is_konstructor = false
|
31
|
+
@konstructor_names << name
|
32
|
+
define(name)
|
33
|
+
elsif declared?(name)
|
34
|
+
define(name)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def declared_in_self?(name)
|
41
|
+
@konstructor_names.include?(name.to_sym)
|
42
|
+
end
|
43
|
+
|
44
|
+
def declared_in_superclass?(name)
|
45
|
+
current_klass = @klass
|
46
|
+
|
47
|
+
# looking for superclass with Konstructor class instance
|
48
|
+
while current_klass.respond_to?(:superclass) && current_klass.superclass.respond_to?(:konstructor, true)
|
49
|
+
current_klass = current_klass.superclass
|
50
|
+
return true if Konstructor.declared?(current_klass, name)
|
51
|
+
end
|
52
|
+
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
def process_new_names(new_names)
|
57
|
+
new_names = new_names.map(&:to_sym)
|
58
|
+
@konstructor_names.concat(new_names)
|
59
|
+
|
60
|
+
new_names.each do |name|
|
61
|
+
if has_own_method?(name)
|
62
|
+
define(name)
|
63
|
+
else
|
64
|
+
# not sure if konstructor ever will be defined,
|
65
|
+
# but informing about the problem anyway
|
66
|
+
validate_name(name)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def has_own_method?(name)
|
72
|
+
method_defined = @klass.method_defined?(name) || @klass.private_method_defined?(name)
|
73
|
+
superclass_method_defined = @klass.respond_to?(:superclass) && (
|
74
|
+
@klass.superclass.method_defined?(name) || @klass.superclass.private_method_defined?(name)
|
75
|
+
)
|
76
|
+
method_defined && !superclass_method_defined
|
77
|
+
end
|
78
|
+
|
79
|
+
# this method is idempotent
|
80
|
+
def define(name)
|
81
|
+
validate_name(name)
|
82
|
+
|
83
|
+
# defining class method
|
84
|
+
@klass.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
|
85
|
+
def #{name}(*args, &block)
|
86
|
+
instance = allocate
|
87
|
+
instance.__send__(:#{name}, *args, &block)
|
88
|
+
instance
|
89
|
+
end
|
90
|
+
RUBY
|
91
|
+
|
92
|
+
# marking instance method as private
|
93
|
+
@klass.__send__(:private, name)
|
94
|
+
end
|
95
|
+
|
96
|
+
def validate_name(name)
|
97
|
+
if Konstructor.reserved?(name)
|
98
|
+
raise ReservedNameError, name
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Konstructor
|
2
|
+
module KonstructorMethodHook
|
3
|
+
|
4
|
+
# Experimental and currently not used method_added hook approach protecting against method_added
|
5
|
+
# overrides that are not calling super (hopefully, there is no such code in the wild).
|
6
|
+
#
|
7
|
+
# Since method_added hook is idempotent, there would be no harm done even if
|
8
|
+
# overridding method_added actually had super call and Konstructor's hook would be
|
9
|
+
# called twice as a result of this.
|
10
|
+
def self.setup(base)
|
11
|
+
method_added_method = base.method(:method_added)
|
12
|
+
if method_added_method.source_location
|
13
|
+
method_added_file_path = method_added_method.source_location.first
|
14
|
+
return if method_added_file_path.include?('konstructor_method_hook')
|
15
|
+
end
|
16
|
+
|
17
|
+
base.instance_exec do
|
18
|
+
private
|
19
|
+
|
20
|
+
alias konstructor_super_method_added method_added
|
21
|
+
|
22
|
+
def method_added(name)
|
23
|
+
@konstructor ||= Konstructor.new(self)
|
24
|
+
@konstructor.method_added_to_klass(name)
|
25
|
+
konstructor_super_method_added(name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Konstructor
|
2
|
+
module SimpleMethodHook
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def method_added(name)
|
7
|
+
Konstructor.method_added_to_klass(self, name)
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.setup(base)
|
12
|
+
class << base
|
13
|
+
# Ruby itself checks against double include
|
14
|
+
include Konstructor::SimpleMethodHook
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: konstructor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dima Lashkov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: thor
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.19.4
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.19.4
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: contracts
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.14.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.14.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: constructor
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 2.0.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 2.0.0
|
125
|
+
description: "\n Konstructor is a small gem that gives you multiple constructors
|
126
|
+
in Ruby.\n It's similar to having overloaded constructors in other languages.\n
|
127
|
+
\ "
|
128
|
+
email:
|
129
|
+
- dm.lashkov@gmail.com
|
130
|
+
executables: []
|
131
|
+
extensions: []
|
132
|
+
extra_rdoc_files: []
|
133
|
+
files:
|
134
|
+
- ".gitignore"
|
135
|
+
- ".travis.yml"
|
136
|
+
- CODE_OF_CONDUCT.md
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- bin/console
|
142
|
+
- bin/setup
|
143
|
+
- konstructor.gemspec
|
144
|
+
- lib/konstructor.rb
|
145
|
+
- lib/konstructor/core_ext.rb
|
146
|
+
- lib/konstructor/exceptions.rb
|
147
|
+
- lib/konstructor/factory.rb
|
148
|
+
- lib/konstructor/konstructor_method_hook.rb
|
149
|
+
- lib/konstructor/simple_method_hook.rb
|
150
|
+
- lib/konstructor/version.rb
|
151
|
+
homepage: https://github.com/snovity/konstructor
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
metadata: {}
|
155
|
+
post_install_message:
|
156
|
+
rdoc_options: []
|
157
|
+
require_paths:
|
158
|
+
- lib
|
159
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: 2.0.0
|
164
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
requirements: []
|
170
|
+
rubyforge_project:
|
171
|
+
rubygems_version: 2.5.1
|
172
|
+
signing_key:
|
173
|
+
specification_version: 4
|
174
|
+
summary: Multiple constructors in Ruby.
|
175
|
+
test_files: []
|