kwattr 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +21 -0
- data/README.md +31 -16
- data/lib/kwattr.rb +6 -6
- metadata +4 -16
- data/.gitignore +0 -9
- data/.rspec +0 -2
- data/.travis.yml +0 -12
- data/CODE_OF_CONDUCT.md +0 -13
- data/Gemfile +0 -4
- data/Rakefile +0 -20
- data/benchmark.rb +0 -124
- data/bin/console +0 -8
- data/bin/rake +0 -16
- data/bin/rspec +0 -16
- data/bin/setup +0 -5
- data/kwattr.gemspec +0 -23
- data/test.rb +0 -221
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9ced9e0ee4d57097a23c8e2a32b6844e590031a
|
4
|
+
data.tar.gz: 3aba633d536806dfb842125e8a41b29c12d16844
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23bdd697aa7329a1b0dcad3352fc5ef0946634d204f0d4e35a131a9732ae15d6047a7efb37deb1f292478021ae35486e6cd0abcac854e6fff95dbbdf220c24e6
|
7
|
+
data.tar.gz: d1d49b97510a5751992cdf0441eaa1edc112d8f0fe108f1611d476f10d5098a846d81d877d1124e0d4027a2324634e22a72b5cd8c8bfb0698204c9ab48f4d3df
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Étienne Barrié
|
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
CHANGED
@@ -36,11 +36,21 @@ class BarInitialize
|
|
36
36
|
|
37
37
|
def initialize(bar: 2)
|
38
38
|
@bar = foo / bar
|
39
|
+
yield @bar if block_given?
|
39
40
|
end
|
40
41
|
end
|
41
42
|
BarInitialize.new # => #<BarInitialize @foo=42, @bar=21>
|
42
43
|
```
|
43
44
|
|
45
|
+
The prepended `initialize` passes on any block it receives, so you can yield
|
46
|
+
from your `initialize` as usual.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
default_bar = nil
|
50
|
+
BarInitialize.new { |bar| default_bar = bar }
|
51
|
+
default_bar # => 21
|
52
|
+
```
|
53
|
+
|
44
54
|
### return value
|
45
55
|
|
46
56
|
It returns the list of keyword attributes so you can combine with methods like
|
@@ -67,22 +77,17 @@ class FooBar2
|
|
67
77
|
end
|
68
78
|
end
|
69
79
|
FooBar2.new
|
70
|
-
# Ruby 2.2 and later:
|
71
|
-
#
|
80
|
+
# Ruby 2.2 and later: ArgumentError: missing keywords: bar, foo
|
81
|
+
# Rubinius: ArgumentError: missing keyword: foo
|
72
82
|
```
|
73
83
|
|
74
84
|
## compatibility
|
75
85
|
|
76
|
-
* **Ruby 2.3**, **Ruby 2.2** and **JRuby 9000** are
|
77
|
-
|
78
|
-
* **Ruby 2.1** is supported, but exceptions don't include keywords from `super`.
|
86
|
+
* **Ruby 2.5**, **Ruby 2.4**,**Ruby 2.3**, **Ruby 2.2** and **JRuby 9000** are
|
87
|
+
fully supported.
|
79
88
|
|
80
|
-
* **
|
81
|
-
|
82
|
-
that came in 2.1.
|
83
|
-
|
84
|
-
* **Rubinius** is supported, starting at least version 2.5.8, possibly before.
|
85
|
-
A similar but not exactly the same limitation as 2.1 applies to exceptions.
|
89
|
+
* **Rubinius** is supported, but exceptions don't include keywords from
|
90
|
+
`super`.
|
86
91
|
|
87
92
|
## free features
|
88
93
|
|
@@ -146,8 +151,18 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
146
151
|
|
147
152
|
## Contributing
|
148
153
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
+
Bug reports and pull requests are welcome on GitHub at
|
155
|
+
https://github.com/etiennebarrie/kwattr. This project is intended to be a safe,
|
156
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
157
|
+
the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
158
|
+
|
159
|
+
## License
|
160
|
+
|
161
|
+
The gem is available as open source under the terms of the [MIT
|
162
|
+
License](https://opensource.org/licenses/MIT).
|
163
|
+
|
164
|
+
## Code of Conduct
|
165
|
+
|
166
|
+
Everyone interacting in the KWattr project’s codebases, issue trackers, chat
|
167
|
+
rooms and mailing lists is expected to follow the [code of
|
168
|
+
conduct](https://github.com/etiennebarrie/kwattr/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/kwattr.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class KWAttr < Module
|
2
|
-
VERSION = "1.
|
2
|
+
VERSION = "1.1.0"
|
3
3
|
|
4
4
|
def initialize
|
5
5
|
@required = []
|
@@ -10,12 +10,11 @@ class KWAttr < Module
|
|
10
10
|
required_attrs = @required
|
11
11
|
defaults = @defaults
|
12
12
|
required_attrs.concat(attrs).uniq!
|
13
|
-
super_method = Method.instance_methods.include? :super_method
|
14
13
|
defaults.merge!(opts)
|
15
14
|
iv_cache = Hash.new { |h, k| h[k] = :"@#{k}" }
|
16
15
|
|
17
16
|
verbose, $VERBOSE = $VERBOSE, false
|
18
|
-
define_method :initialize do |*args, **kwargs|
|
17
|
+
define_method :initialize do |*args, **kwargs, &block|
|
19
18
|
required = required_attrs.dup
|
20
19
|
|
21
20
|
defaults.merge(kwargs).each_pair do |key, value|
|
@@ -29,7 +28,7 @@ class KWAttr < Module
|
|
29
28
|
initialize = method(:initialize)
|
30
29
|
initialize.super_method.parameters.each do |type, name|
|
31
30
|
super_required << name if type == :keyreq && !kwargs.key?(name)
|
32
|
-
end
|
31
|
+
end
|
33
32
|
required.unshift(*super_required)
|
34
33
|
raise ArgumentError,
|
35
34
|
"missing keyword#{'s' if required.size > 1}: #{required.join(', ')}"
|
@@ -39,10 +38,11 @@ class KWAttr < Module
|
|
39
38
|
|
40
39
|
begin
|
41
40
|
|
42
|
-
|
41
|
+
|
42
|
+
super(*args, &block)
|
43
43
|
|
44
44
|
rescue ArgumentError
|
45
|
-
arity =
|
45
|
+
arity = method(:initialize).super_method.arity
|
46
46
|
if !kwargs.empty? && arity != -1 && arity == args.size - 1
|
47
47
|
raise ArgumentError,
|
48
48
|
"unknown keyword#{'s' if kwargs.size > 1}: #{kwargs.keys.join(', ')}"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kwattr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Étienne Barrié
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-12-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,21 +73,9 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
-
-
|
77
|
-
- ".rspec"
|
78
|
-
- ".travis.yml"
|
79
|
-
- CODE_OF_CONDUCT.md
|
80
|
-
- Gemfile
|
76
|
+
- LICENSE.txt
|
81
77
|
- README.md
|
82
|
-
- Rakefile
|
83
|
-
- benchmark.rb
|
84
|
-
- bin/console
|
85
|
-
- bin/rake
|
86
|
-
- bin/rspec
|
87
|
-
- bin/setup
|
88
|
-
- kwattr.gemspec
|
89
78
|
- lib/kwattr.rb
|
90
|
-
- test.rb
|
91
79
|
homepage: https://github.com/etiennebarrie/kwattr
|
92
80
|
licenses: []
|
93
81
|
metadata: {}
|
@@ -107,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
95
|
version: '0'
|
108
96
|
requirements: []
|
109
97
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.6.13
|
111
99
|
signing_key:
|
112
100
|
specification_version: 4
|
113
101
|
summary: attr_reader + initialize with keyword arguments
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/CODE_OF_CONDUCT.md
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# Contributor Code of Conduct
|
2
|
-
|
3
|
-
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
-
|
5
|
-
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
-
|
7
|
-
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
-
|
9
|
-
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
-
|
11
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
-
|
13
|
-
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
DELETED
data/Rakefile
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require "bundler/gem_tasks"
|
3
|
-
rescue LoadError
|
4
|
-
end
|
5
|
-
|
6
|
-
begin
|
7
|
-
require 'rspec/core/rake_task'
|
8
|
-
RSpec::Core::RakeTask.new(:spec)
|
9
|
-
rescue LoadError
|
10
|
-
end
|
11
|
-
|
12
|
-
task :examples do
|
13
|
-
ruby "test.rb"
|
14
|
-
end
|
15
|
-
|
16
|
-
task :benchmark do
|
17
|
-
ruby "benchmark.rb"
|
18
|
-
end
|
19
|
-
|
20
|
-
task :default => [:examples, :spec, :benchmark]
|
data/benchmark.rb
DELETED
@@ -1,124 +0,0 @@
|
|
1
|
-
require 'benchmark/ips'
|
2
|
-
require 'kwattr'
|
3
|
-
require_relative 'spec/examples'
|
4
|
-
|
5
|
-
class ROneAttr
|
6
|
-
attr_reader :foo
|
7
|
-
|
8
|
-
def initialize(foo:)
|
9
|
-
@foo = foo
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class ROneAttrDefault
|
14
|
-
DEFAULT_FOO = 42
|
15
|
-
|
16
|
-
attr_reader :foo
|
17
|
-
|
18
|
-
def initialize(foo: DEFAULT_FOO)
|
19
|
-
@foo = foo
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class ROneAttrOnePositional
|
24
|
-
attr_reader :foo, :bar
|
25
|
-
|
26
|
-
def initialize(bar, foo:)
|
27
|
-
@foo = foo
|
28
|
-
@bar = bar
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class RTwoAttrs
|
33
|
-
attr_reader :foo, :bar
|
34
|
-
|
35
|
-
def initialize(foo:, bar:)
|
36
|
-
@foo = foo
|
37
|
-
@bar = bar
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class RTwoAttrsDefaults
|
42
|
-
DEFAULT_FOO = 42
|
43
|
-
DEFAULT_BAR = 21
|
44
|
-
|
45
|
-
attr_reader :foo, :bar
|
46
|
-
|
47
|
-
def initialize(foo: DEFAULT_FOO, bar: DEFAULT_BAR)
|
48
|
-
@foo = foo
|
49
|
-
@bar = bar
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
class RTwoAttrsOneDefault
|
54
|
-
DEFAULT_BAR = 21
|
55
|
-
|
56
|
-
attr_reader :foo, :bar
|
57
|
-
|
58
|
-
def initialize(foo:, bar: DEFAULT_BAR)
|
59
|
-
@foo = foo
|
60
|
-
@bar = bar
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
class RTwoAttrsOnePos
|
65
|
-
attr_reader :foo, :bar, :baz
|
66
|
-
|
67
|
-
def initialize(baz, foo:, bar:)
|
68
|
-
@foo = foo
|
69
|
-
@bar = bar
|
70
|
-
@baz = baz
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
class RTwoAttrsInheritsOneAttr < ROneAttr
|
75
|
-
attr_reader :bar
|
76
|
-
|
77
|
-
def initialize(foo:, bar:)
|
78
|
-
super(foo: foo)
|
79
|
-
@bar = bar
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
module ROneAttrModule
|
84
|
-
attr_reader :foo
|
85
|
-
|
86
|
-
def initialize(foo:)
|
87
|
-
@foo = foo
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
class ROneAttrViaModule
|
92
|
-
include ROneAttrModule
|
93
|
-
end
|
94
|
-
|
95
|
-
Benchmark.ips do |b|
|
96
|
-
b.warmup = ENV['CONTINUOUS_INTEGRATION'] ? 1 : 0.1
|
97
|
-
b.time = ENV['CONTINUOUS_INTEGRATION'] ? 5 : 1
|
98
|
-
|
99
|
-
b.report "KWAttr classes" do
|
100
|
-
OneAttr.new foo: 42
|
101
|
-
OneAttrDefault.new
|
102
|
-
OneAttrOnePositional.new 42, foo: 21
|
103
|
-
TwoAttrs.new foo: 42, bar: 21
|
104
|
-
TwoAttrsDefaults.new
|
105
|
-
TwoAttrsOneDefault.new foo: 42
|
106
|
-
TwoAttrsOnePos.new 42, foo: 21, bar: 14
|
107
|
-
TwoAttrsInheritsOneAttr.new foo: 42, bar: 21
|
108
|
-
OneAttrViaModule.new foo: 42
|
109
|
-
end
|
110
|
-
|
111
|
-
b.report "Plain classes" do
|
112
|
-
ROneAttr.new foo: 42
|
113
|
-
ROneAttrDefault.new
|
114
|
-
ROneAttrOnePositional.new 42, foo: 21
|
115
|
-
RTwoAttrs.new foo: 42, bar: 21
|
116
|
-
RTwoAttrsDefaults.new
|
117
|
-
RTwoAttrsOneDefault.new foo: 42
|
118
|
-
RTwoAttrsOnePos.new 42, foo: 21, bar: 14
|
119
|
-
RTwoAttrsInheritsOneAttr.new foo: 42, bar: 21
|
120
|
-
ROneAttrViaModule.new foo: 42
|
121
|
-
end
|
122
|
-
|
123
|
-
b.compare!
|
124
|
-
end
|
data/bin/console
DELETED
data/bin/rake
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
# This file was generated by Bundler.
|
4
|
-
#
|
5
|
-
# The application 'rake' is installed as part of a gem, and
|
6
|
-
# this file is here to facilitate running it.
|
7
|
-
#
|
8
|
-
|
9
|
-
require 'pathname'
|
10
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
-
Pathname.new(__FILE__).realpath)
|
12
|
-
|
13
|
-
require 'rubygems'
|
14
|
-
require 'bundler/setup'
|
15
|
-
|
16
|
-
load Gem.bin_path('rake', 'rake')
|
data/bin/rspec
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
# This file was generated by Bundler.
|
4
|
-
#
|
5
|
-
# The application 'rspec' is installed as part of a gem, and
|
6
|
-
# this file is here to facilitate running it.
|
7
|
-
#
|
8
|
-
|
9
|
-
require 'pathname'
|
10
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
-
Pathname.new(__FILE__).realpath)
|
12
|
-
|
13
|
-
require 'rubygems'
|
14
|
-
require 'bundler/setup'
|
15
|
-
|
16
|
-
load Gem.bin_path('rspec-core', 'rspec')
|
data/bin/setup
DELETED
data/kwattr.gemspec
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'kwattr'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "kwattr"
|
8
|
-
spec.version = KWAttr::VERSION
|
9
|
-
spec.authors = ["Étienne Barrié"]
|
10
|
-
spec.email = ["etienne.barrie@gmail.com"]
|
11
|
-
|
12
|
-
spec.summary = %q{attr_reader + initialize with keyword arguments}
|
13
|
-
spec.description = %q{}
|
14
|
-
spec.homepage = "https://github.com/etiennebarrie/kwattr"
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
-
spec.require_paths = ["lib"]
|
18
|
-
|
19
|
-
spec.add_development_dependency "bundler", "~> 1.9"
|
20
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
21
|
-
spec.add_development_dependency "rspec", "~> 3.2"
|
22
|
-
spec.add_development_dependency "benchmark-ips", "~> 2.2"
|
23
|
-
end
|
data/test.rb
DELETED
@@ -1,221 +0,0 @@
|
|
1
|
-
require 'kwattr'
|
2
|
-
puts "kwattr v#{KWAttr::VERSION}"
|
3
|
-
|
4
|
-
def p(value)
|
5
|
-
super if $DEBUG
|
6
|
-
value
|
7
|
-
end
|
8
|
-
|
9
|
-
class Test
|
10
|
-
kwattr :foo, :bar
|
11
|
-
end
|
12
|
-
|
13
|
-
test = Test.new(foo: 42, bar: 21)
|
14
|
-
fail unless p(test.foo) == 42
|
15
|
-
fail unless p(test.bar) == 21
|
16
|
-
|
17
|
-
def raises?(exception_class)
|
18
|
-
yield
|
19
|
-
return
|
20
|
-
rescue exception_class
|
21
|
-
return p $!
|
22
|
-
end
|
23
|
-
|
24
|
-
fail unless raises?(ArgumentError) { Test.new }
|
25
|
-
fail unless raises?(ArgumentError) { Test.new(foo: 42) }
|
26
|
-
fail unless raises?(ArgumentError) { Test.new(foo: 42, bar: 21, baz: 43) }
|
27
|
-
|
28
|
-
class Discrete
|
29
|
-
protected *kwattr(:foo, bar: 21)
|
30
|
-
|
31
|
-
def pfoo
|
32
|
-
foo
|
33
|
-
end
|
34
|
-
|
35
|
-
def pbar
|
36
|
-
bar
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
test = Discrete.new(foo: 42)
|
41
|
-
fail unless p(test.pfoo) == 42
|
42
|
-
fail unless p(test.pbar) == 21
|
43
|
-
fail unless raises?(NoMethodError) { test.foo }
|
44
|
-
fail unless raises?(NoMethodError) { test.bar }
|
45
|
-
|
46
|
-
class Test2
|
47
|
-
kwattr :titi
|
48
|
-
kwattr :toto
|
49
|
-
end
|
50
|
-
|
51
|
-
test = Test2.new(titi: 1, toto: 2)
|
52
|
-
fail unless p(test.titi) == 1
|
53
|
-
fail unless p(test.toto) == 2
|
54
|
-
|
55
|
-
class TestDescendant < Test
|
56
|
-
kwattr :baz
|
57
|
-
end
|
58
|
-
|
59
|
-
test = TestDescendant.new(foo: 42, bar: 21, baz: 14)
|
60
|
-
fail unless p(test.foo) == 42
|
61
|
-
fail unless p(test.bar) == 21
|
62
|
-
fail unless p(test.baz) == 14
|
63
|
-
|
64
|
-
class TestInitialize
|
65
|
-
kwattr :foo, :bar
|
66
|
-
attr :baz
|
67
|
-
|
68
|
-
def initialize(baz: 42)
|
69
|
-
@baz = baz / 3
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
test = TestInitialize.new(foo: 42, bar: 21)
|
74
|
-
fail unless p(test.foo) == 42
|
75
|
-
fail unless p(test.bar) == 21
|
76
|
-
fail unless p(test.baz) == 14
|
77
|
-
test = TestInitialize.new(foo: 42, bar: 21, baz: 21)
|
78
|
-
fail unless p(test.baz) == 7
|
79
|
-
|
80
|
-
class TestPositional
|
81
|
-
kwattr :foo, :bar
|
82
|
-
attr :x, :y
|
83
|
-
|
84
|
-
def initialize(x, y)
|
85
|
-
@x = x
|
86
|
-
@y = y
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
test = TestPositional.new(1, 2, foo: 42, bar: 21)
|
91
|
-
fail unless p(test.x) == 1
|
92
|
-
fail unless p(test.y) == 2
|
93
|
-
fail unless p(test.foo) == 42
|
94
|
-
fail unless p(test.bar) == 21
|
95
|
-
|
96
|
-
class TestOptionalKeyword
|
97
|
-
kwattr foo: 42
|
98
|
-
kwattr :bar
|
99
|
-
end
|
100
|
-
|
101
|
-
test = TestOptionalKeyword.new(bar: 21)
|
102
|
-
fail unless p(test.foo) == 42
|
103
|
-
fail unless p(test.bar) == 21
|
104
|
-
test = TestOptionalKeyword.new(foo: 21, bar: 42)
|
105
|
-
fail unless p(test.foo) == 21
|
106
|
-
|
107
|
-
class TestPositionalDescendant < TestPositional
|
108
|
-
kwattr baz: 14
|
109
|
-
end
|
110
|
-
|
111
|
-
test = TestPositionalDescendant.new(1, 2, foo: 42, bar: 21)
|
112
|
-
fail unless p(test.x) == 1
|
113
|
-
fail unless p(test.y) == 2
|
114
|
-
fail unless p(test.foo) == 42
|
115
|
-
fail unless p(test.bar) == 21
|
116
|
-
fail unless p(test.baz) == 14
|
117
|
-
|
118
|
-
class TestPositionalDescendantMore < TestPositionalDescendant
|
119
|
-
attr :z
|
120
|
-
|
121
|
-
def initialize(x, y, z, **options)
|
122
|
-
super(x, y, **options)
|
123
|
-
@z = z
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
test = TestPositionalDescendantMore.new(1, 2, 3, foo: 42, bar: 21)
|
128
|
-
fail unless p(test.x) == 1
|
129
|
-
fail unless p(test.y) == 2
|
130
|
-
fail unless p(test.z) == 3
|
131
|
-
fail unless p(test.foo) == 42
|
132
|
-
fail unless p(test.bar) == 21
|
133
|
-
fail unless p(test.baz) == 14
|
134
|
-
|
135
|
-
class TestPositionalDescendantFull < TestPositionalDescendant
|
136
|
-
attr :z
|
137
|
-
kwattr opt: 42
|
138
|
-
kwattr :req
|
139
|
-
|
140
|
-
def initialize(x, y, z, **options)
|
141
|
-
super(x, y, **options)
|
142
|
-
@z = z
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
test = TestPositionalDescendantFull.new(1, 2, 3, foo: 42, bar: 21, opt: 7, req: 6)
|
147
|
-
fail unless p(test.x) == 1
|
148
|
-
fail unless p(test.y) == 2
|
149
|
-
fail unless p(test.z) == 3
|
150
|
-
fail unless p(test.foo) == 42
|
151
|
-
fail unless p(test.bar) == 21
|
152
|
-
fail unless p(test.baz) == 14
|
153
|
-
fail unless p(test.opt) == 7
|
154
|
-
fail unless p(test.req) == 6
|
155
|
-
|
156
|
-
class TestDefaults
|
157
|
-
kwattr foo: 42, bar: 21
|
158
|
-
end
|
159
|
-
|
160
|
-
test = TestDefaults.new
|
161
|
-
fail unless p(test.foo) == 42
|
162
|
-
fail unless p(test.bar) == 21
|
163
|
-
|
164
|
-
class TestKeywordArgs
|
165
|
-
kwattr foo: 42
|
166
|
-
attr :bar
|
167
|
-
|
168
|
-
def initialize(bar: 21)
|
169
|
-
@bar = bar * 3
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
test = TestKeywordArgs.new(foo: 42, bar: 14)
|
174
|
-
fail unless p(test.foo) == 42
|
175
|
-
fail unless p(test.bar) == 42
|
176
|
-
|
177
|
-
class TestInit
|
178
|
-
kwattr foo: 42, bar: 21
|
179
|
-
|
180
|
-
def initialize
|
181
|
-
@inits ||= []
|
182
|
-
@inits << 1
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
class TestInitDescendant < TestInit
|
187
|
-
kwattr baz: 14
|
188
|
-
attr :inits
|
189
|
-
|
190
|
-
def initialize(*)
|
191
|
-
super
|
192
|
-
@inits << 2
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
test = TestInit.new(bar: 42)
|
197
|
-
test = TestInitDescendant.new(baz: 7)
|
198
|
-
fail unless p(test.inits) == [1, 2]
|
199
|
-
|
200
|
-
module Foo
|
201
|
-
kwattr :foo
|
202
|
-
end
|
203
|
-
|
204
|
-
class TestModule
|
205
|
-
include Foo
|
206
|
-
end
|
207
|
-
|
208
|
-
test = TestModule.new(foo: 42)
|
209
|
-
fail unless p(test.foo) == 42
|
210
|
-
|
211
|
-
module Bar
|
212
|
-
kwattr :bar
|
213
|
-
end
|
214
|
-
|
215
|
-
class TestModules
|
216
|
-
include Foo, Bar
|
217
|
-
end
|
218
|
-
|
219
|
-
test = TestModules.new(foo: 42, bar: 21)
|
220
|
-
fail unless p(test.foo) == 42
|
221
|
-
fail unless p(test.bar) == 21
|