kwattr 0.1.0 → 0.2.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 +4 -4
- data/.rspec +2 -0
- data/.travis.yml +3 -1
- data/README.md +33 -31
- data/Rakefile +13 -2
- data/bin/console +1 -0
- data/kwattr.gemspec +1 -0
- data/lib/kwattr/version.rb +1 -1
- data/lib/kwattr.rb +3 -1
- data/test.rb +18 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3f52b74e112e1a8cd7ee56f91323339731eafd4
|
4
|
+
data.tar.gz: a763e6223e15158fe8062a801b2cfe78309adb54
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dc7f95df0dd1ef9dbe54f4d4900194ab11c85b6374811df4157c0a09aeb073a4a34409fcdf2ba4cfad48f373f916fb1cd608ab74ce4a157c0e276078b891251
|
7
|
+
data.tar.gz: d63c3abdfc609d1441847176aac5ee6ee0f8169fe741b68de74c10f2489657602f29d794f2968179c5914a2435ffa4be2614630130c9a3cec714776d7510676a
|
data/.rspec
ADDED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
# KWattr
|
2
2
|
|
3
|
-
Keyword arguments meet
|
3
|
+
Keyword arguments meet `attr_reader` and `initialize`:
|
4
4
|
|
5
5
|
```ruby
|
6
6
|
class FooBar
|
7
|
-
kwattr :foo, :
|
7
|
+
kwattr :foo, bar: 21
|
8
8
|
end
|
9
9
|
|
10
|
-
FooBar.new
|
11
|
-
# =>
|
10
|
+
foobar = FooBar.new(foo: 42) # => #<FooBar @foo=42, @bar=21>
|
11
|
+
foobar.foo # => 42
|
12
|
+
foobar.bar # => 21
|
12
13
|
```
|
13
14
|
|
14
15
|
instead of
|
@@ -17,58 +18,53 @@ instead of
|
|
17
18
|
class FooBar
|
18
19
|
attr_reader :foo, :bar
|
19
20
|
|
20
|
-
def initialize(foo:, bar:)
|
21
|
+
def initialize(foo:, bar: 21)
|
21
22
|
@foo = foo
|
22
23
|
@bar = bar
|
23
24
|
end
|
24
25
|
end
|
25
26
|
```
|
26
27
|
|
27
|
-
###
|
28
|
+
### initialize
|
29
|
+
|
30
|
+
The provided `initialize` is prepended so there's no need to call `super`, and
|
31
|
+
attributes are already set when your code is reached.
|
28
32
|
|
29
33
|
```ruby
|
30
|
-
class
|
34
|
+
class BarInitialize
|
31
35
|
kwattr foo: 42
|
32
|
-
end
|
33
36
|
|
34
|
-
|
35
|
-
|
37
|
+
def initialize(bar: 2)
|
38
|
+
@bar = foo / bar
|
39
|
+
end
|
40
|
+
end
|
41
|
+
BarInitialize.new # => #<BarInitialize @foo=42, @bar=21>
|
36
42
|
```
|
37
43
|
|
38
|
-
###
|
44
|
+
### return value
|
39
45
|
|
40
|
-
|
41
|
-
|
46
|
+
It returns the list of keyword attributes so you can combine with methods like
|
47
|
+
`Module#protected`.
|
42
48
|
|
43
49
|
```ruby
|
44
|
-
|
45
|
-
|
46
|
-
class Over
|
47
|
-
kwattr foo: 42
|
48
|
-
|
49
|
-
def initialize
|
50
|
-
$over << foo
|
51
|
-
end
|
50
|
+
class FooProtected
|
51
|
+
protected *kwattr(foo: 42)
|
52
52
|
end
|
53
|
-
|
54
|
-
|
55
|
-
Over.new foo: 21
|
56
|
-
$over
|
57
|
-
# => [42, 21]
|
53
|
+
FooProtected.new # => #<FooProtected @foo=42>
|
54
|
+
FooProtected.protected_instance_methods # => [:foo]
|
58
55
|
```
|
59
56
|
|
60
|
-
###
|
57
|
+
### subclass
|
61
58
|
|
62
59
|
```ruby
|
63
60
|
class Bar < Foo
|
64
61
|
kwattr :bar
|
65
62
|
end
|
66
63
|
|
67
|
-
Bar.new
|
68
|
-
# => #<Bar @foo=42, @bar=42>
|
64
|
+
Bar.new(bar: 42) # => #<Bar @foo=42, @bar=42>
|
69
65
|
```
|
70
66
|
|
71
|
-
###
|
67
|
+
### include
|
72
68
|
|
73
69
|
```ruby
|
74
70
|
module Mod
|
@@ -79,9 +75,15 @@ class Inc
|
|
79
75
|
include Mod
|
80
76
|
end
|
81
77
|
|
82
|
-
Inc.new
|
78
|
+
Inc.new(mod: 42)
|
83
79
|
```
|
84
80
|
|
81
|
+
## See also
|
82
|
+
|
83
|
+
* https://github.com/mbj/concord
|
84
|
+
* https://github.com/ahoward/fattr
|
85
|
+
* https://github.com/solnic/virtus
|
86
|
+
|
85
87
|
## Installation
|
86
88
|
|
87
89
|
Add this line to your application's Gemfile:
|
data/Rakefile
CHANGED
@@ -1,5 +1,16 @@
|
|
1
|
-
|
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
|
2
11
|
|
3
|
-
task :
|
12
|
+
task :examples do
|
4
13
|
ruby "test.rb"
|
5
14
|
end
|
15
|
+
|
16
|
+
task :default => [:examples, :spec]
|
data/bin/console
CHANGED
data/kwattr.gemspec
CHANGED
data/lib/kwattr/version.rb
CHANGED
data/lib/kwattr.rb
CHANGED
@@ -3,12 +3,14 @@ require "kwattr/version"
|
|
3
3
|
module KWAttr
|
4
4
|
|
5
5
|
def kwattr(*attrs, **opts)
|
6
|
-
|
6
|
+
names = [*attrs, *opts.keys]
|
7
|
+
attr_reader(*names)
|
7
8
|
prepend Initializer
|
8
9
|
extend Heritable
|
9
10
|
required, defaults = kwattrs
|
10
11
|
attrs.each { |attr| required << attr unless required.include?(attr) }
|
11
12
|
defaults.merge!(opts)
|
13
|
+
names
|
12
14
|
end
|
13
15
|
|
14
16
|
def kwattrs
|
data/test.rb
CHANGED
@@ -21,6 +21,24 @@ fail unless raises?(ArgumentError) { Test.new }
|
|
21
21
|
fail unless raises?(ArgumentError) { Test.new(foo: 42) }
|
22
22
|
fail unless raises?(ArgumentError) { Test.new(foo: 42, bar: 21, baz: 43) }
|
23
23
|
|
24
|
+
class Discrete
|
25
|
+
protected *kwattr(:foo, bar: 21)
|
26
|
+
|
27
|
+
def pfoo
|
28
|
+
foo
|
29
|
+
end
|
30
|
+
|
31
|
+
def pbar
|
32
|
+
bar
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
test = Discrete.new(foo: 42)
|
37
|
+
fail unless p(test.pfoo) == 42
|
38
|
+
fail unless p(test.pbar) == 21
|
39
|
+
fail unless raises?(NoMethodError) { test.foo }
|
40
|
+
fail unless raises?(NoMethodError) { test.bar }
|
41
|
+
|
24
42
|
class Test2
|
25
43
|
kwattr :titi
|
26
44
|
kwattr :toto
|
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.2.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-04-
|
11
|
+
date: 2015-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
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.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
41
55
|
description: ''
|
42
56
|
email:
|
43
57
|
- etienne.barrie@gmail.com
|
@@ -46,6 +60,7 @@ extensions: []
|
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
62
|
- ".gitignore"
|
63
|
+
- ".rspec"
|
49
64
|
- ".travis.yml"
|
50
65
|
- CODE_OF_CONDUCT.md
|
51
66
|
- Gemfile
|