kwattr 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/README.md +35 -0
- data/bin/rake +16 -0
- data/bin/rspec +16 -0
- data/kwattr.gemspec +3 -1
- data/lib/kwattr.rb +19 -10
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef3443445900c18ce80cf33cd00e1b96f9d5dc22
|
4
|
+
data.tar.gz: b3296c9d24f5f0b3660cbd98881d4a06bb2db240
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44bdf502e0a2036bc34563f5ea8d8cd97dbc4843ed563140551002bfa2c8261b3142d9ec16cb289c29334c7eccf9897e0e7ebf24e5ba4e59f42e12c2f019f1df
|
7
|
+
data.tar.gz: 088ef1c83c21c54d021b5caa98a234745677600bf6a2d46d7f454f38d329f301359db2cc8cdb6a80c7414b944b9343f0defcc88297d2b6303b7b04cfd75f02ae
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -54,6 +54,41 @@ FooProtected.new # => #<FooProtected @foo=42>
|
|
54
54
|
FooProtected.protected_instance_methods # => [:foo]
|
55
55
|
```
|
56
56
|
|
57
|
+
### useful exception messages
|
58
|
+
|
59
|
+
When it can, kwattr tries to extract the required keyword arguments from the
|
60
|
+
super method to show more useful exception messages.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class FooBar2
|
64
|
+
kwattr :foo
|
65
|
+
|
66
|
+
def initialize(bar:)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
FooBar2.new
|
70
|
+
# Ruby 2.2 and later: ArgumentError: missing keywords: bar, foo
|
71
|
+
# Ruby 2.1 and Rubinius: ArgumentError: missing keyword: foo
|
72
|
+
```
|
73
|
+
|
74
|
+
## compatibility
|
75
|
+
|
76
|
+
* **Ruby 2.2** and **JRuby 9000** are fully supported.
|
77
|
+
|
78
|
+
* **Ruby 2.1** is supported, but exceptions don't include keywords from `super`.
|
79
|
+
|
80
|
+
* **Ruby 2.0**: kwattr brings fake required keyword arguments to Ruby 2.0: it
|
81
|
+
gives you the feel of required keyword arguments without needing the syntax
|
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.
|
86
|
+
|
87
|
+
## free features
|
88
|
+
|
89
|
+
*kwattr has no specific code to support those, but they work and are supported
|
90
|
+
use cases.*
|
91
|
+
|
57
92
|
### subclass
|
58
93
|
|
59
94
|
```ruby
|
data/bin/rake
ADDED
@@ -0,0 +1,16 @@
|
|
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
ADDED
@@ -0,0 +1,16 @@
|
|
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/kwattr.gemspec
CHANGED
data/lib/kwattr.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class KWAttr < Module
|
2
|
-
VERSION = "0.
|
2
|
+
VERSION = "0.5.0"
|
3
3
|
|
4
4
|
def initialize
|
5
5
|
@required = []
|
@@ -10,6 +10,7 @@ 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
|
13
14
|
defaults.merge!(opts)
|
14
15
|
iv_cache = Hash.new { |h, k| h[k] = :"@#{k}" }
|
15
16
|
|
@@ -24,22 +25,30 @@ class KWAttr < Module
|
|
24
25
|
end
|
25
26
|
|
26
27
|
unless required.empty?
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
super_required = []
|
29
|
+
initialize = method(:initialize)
|
30
|
+
initialize.super_method.parameters.each do |type, name|
|
31
|
+
super_required << name if type == :keyreq && !kwargs.key?(name)
|
32
|
+
end if super_method
|
33
|
+
required.unshift(*super_required)
|
30
34
|
raise ArgumentError,
|
31
35
|
"missing keyword#{'s' if required.size > 1}: #{required.join(', ')}"
|
32
36
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
37
|
+
|
38
|
+
args << kwargs unless kwargs.empty?
|
39
|
+
|
40
|
+
begin
|
41
|
+
|
42
|
+
super(*args)
|
43
|
+
|
44
|
+
rescue ArgumentError
|
45
|
+
arity = super_method ? method(:initialize).super_method.arity : -1
|
46
|
+
if !kwargs.empty? && arity != -1 && arity == args.size - 1
|
36
47
|
raise ArgumentError,
|
37
48
|
"unknown keyword#{'s' if kwargs.size > 1}: #{kwargs.keys.join(', ')}"
|
38
49
|
end
|
39
|
-
|
50
|
+
raise
|
40
51
|
end
|
41
|
-
|
42
|
-
super(*args)
|
43
52
|
end
|
44
53
|
$VERBOSE = verbose
|
45
54
|
end
|
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: 0.
|
4
|
+
version: 0.5.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: 2015-
|
11
|
+
date: 2015-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -82,6 +82,8 @@ files:
|
|
82
82
|
- Rakefile
|
83
83
|
- benchmark.rb
|
84
84
|
- bin/console
|
85
|
+
- bin/rake
|
86
|
+
- bin/rspec
|
85
87
|
- bin/setup
|
86
88
|
- kwattr.gemspec
|
87
89
|
- lib/kwattr.rb
|
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
107
|
version: '0'
|
106
108
|
requirements: []
|
107
109
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.4.5
|
110
|
+
rubygems_version: 2.4.5.1
|
109
111
|
signing_key:
|
110
112
|
specification_version: 4
|
111
113
|
summary: attr_reader + initialize with keyword arguments
|