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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 178bc67e1d1f7cdc032e4e1b55349992fb00869e
4
- data.tar.gz: fd9698a022ccb2c69b6f63f239bc67dc4169b9cf
3
+ metadata.gz: f3f52b74e112e1a8cd7ee56f91323339731eafd4
4
+ data.tar.gz: a763e6223e15158fe8062a801b2cfe78309adb54
5
5
  SHA512:
6
- metadata.gz: 52e4ef1bb5e95c1a9f5a3477cf3fc279cc4f53939c8e4204bd141a4bcb4294b1b017d06bc6b4db2e04a4b2f33fd6c91b4d53e61cf4d3c9d4da2bb8ec0f77a00e
7
- data.tar.gz: c0f7229e9e29e9bf9ae82831cb6b3ce2300684fec79c9c0c2f43b4bbe9fe6d6e6396b1624cfbf23c22e7bbebee0268c148cd9797f8d768051b11f0201f4b02bc
6
+ metadata.gz: 2dc7f95df0dd1ef9dbe54f4d4900194ab11c85b6374811df4157c0a09aeb073a4a34409fcdf2ba4cfad48f373f916fb1cd608ab74ce4a157c0e276078b891251
7
+ data.tar.gz: d63c3abdfc609d1441847176aac5ee6ee0f8169fe741b68de74c10f2489657602f29d794f2968179c5914a2435ffa4be2614630130c9a3cec714776d7510676a
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml CHANGED
@@ -1,3 +1,5 @@
1
1
  language: ruby
2
+ before_install:
3
+ - gem install bundler
2
4
  rvm:
3
- - 2.2.0
5
+ - 2.2.1
data/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  # KWattr
2
2
 
3
- Keyword arguments meet attribute definitions and initialize:
3
+ Keyword arguments meet `attr_reader` and `initialize`:
4
4
 
5
5
  ```ruby
6
6
  class FooBar
7
- kwattr :foo, :bar
7
+ kwattr :foo, bar: 21
8
8
  end
9
9
 
10
- FooBar.new foo: 42, bar: 21
11
- # => #<FooBar @foo=42, @bar=21>
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
- ### Default values
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 Foo
34
+ class BarInitialize
31
35
  kwattr foo: 42
32
- end
33
36
 
34
- Foo.new
35
- # => #<Foo @foo=42>
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
- ### Overriding initialize
44
+ ### return value
39
45
 
40
- The default initialize is prepended so there's no need to call `super` in
41
- initialize, and attributes are already set.
46
+ It returns the list of keyword attributes so you can combine with methods like
47
+ `Module#protected`.
42
48
 
43
49
  ```ruby
44
- $over = []
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
- Over.new
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
- ### Subclass
57
+ ### subclass
61
58
 
62
59
  ```ruby
63
60
  class Bar < Foo
64
61
  kwattr :bar
65
62
  end
66
63
 
67
- Bar.new bar: 42
68
- # => #<Bar @foo=42, @bar=42>
64
+ Bar.new(bar: 42) # => #<Bar @foo=42, @bar=42>
69
65
  ```
70
66
 
71
- ### Include
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 mod: 42
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
- require "bundler/gem_tasks"
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 :default do
12
+ task :examples do
4
13
  ruby "test.rb"
5
14
  end
15
+
16
+ task :default => [:examples, :spec]
data/bin/console CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "kwattr"
5
+ require_relative "../spec/examples"
5
6
 
6
7
  require "irb"
7
8
  IRB.start
data/kwattr.gemspec CHANGED
@@ -18,4 +18,5 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.add_development_dependency "bundler", "~> 1.9"
20
20
  spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "rspec", "~> 3.2"
21
22
  end
@@ -1,3 +1,3 @@
1
1
  module KWattr
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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
- attr_reader(*attrs, *opts.keys)
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.1.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-03 00:00:00.000000000 Z
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