kwattr 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dff8a0f557944d7eb7a901f75edc60091b01094e
4
- data.tar.gz: 6eae305b4eadccfcb92cda575eeaf09c8a71d2b2
3
+ metadata.gz: ef3443445900c18ce80cf33cd00e1b96f9d5dc22
4
+ data.tar.gz: b3296c9d24f5f0b3660cbd98881d4a06bb2db240
5
5
  SHA512:
6
- metadata.gz: 17e68381d077e09fe0d7fd72828b2a1cd8dbab34c8010f43f679df76dac63e43a363f9aea9ee8e339295f8108b6266fff8bd958b782b299590867fd2f135a5fe
7
- data.tar.gz: b82fcba69e07b3b247b9411fd400f5fa28ad9ed8e901b0d61623ec96750214cb5478e76d965af4f50eb4a4f039abc53ff74ea98907e9000f2e12b99f2f7e4941
6
+ metadata.gz: 44bdf502e0a2036bc34563f5ea8d8cd97dbc4843ed563140551002bfa2c8261b3142d9ec16cb289c29334c7eccf9897e0e7ebf24e5ba4e59f42e12c2f019f1df
7
+ data.tar.gz: 088ef1c83c21c54d021b5caa98a234745677600bf6a2d46d7f454f38d329f301359db2cc8cdb6a80c7414b944b9343f0defcc88297d2b6303b7b04cfd75f02ae
@@ -2,4 +2,7 @@ language: ruby
2
2
  before_install:
3
3
  - gem install bundler
4
4
  rvm:
5
- - 2.2.1
5
+ - 2.1.7
6
+ - 2.2.3
7
+ - rbx-2
8
+ - jruby-9.0.1.0
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
@@ -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')
@@ -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')
@@ -1,5 +1,7 @@
1
1
  # coding: utf-8
2
- require_relative 'lib/kwattr'
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kwattr'
3
5
 
4
6
  Gem::Specification.new do |spec|
5
7
  spec.name = "kwattr"
@@ -1,5 +1,5 @@
1
1
  class KWAttr < Module
2
- VERSION = "0.4.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
- method(:initialize).super_method.parameters.each do |type, name|
28
- required << name if type == :keyreq && !kwargs.key?(name)
29
- end
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
- unless kwargs.empty?
34
- arity = method(:initialize).super_method.arity
35
- if arity != -1 && arity == args.size
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
- args << kwargs
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.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-06-23 00:00:00.000000000 Z
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