trinkets 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/doc/class/init.md +16 -8
- data/lib/trinkets/extend/class/init.rb +5 -5
- data/lib/trinkets/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b68e75535021ee65a9f04019645b52a4ef1a76f1c2dfa2d5d625790bef4823a
|
4
|
+
data.tar.gz: dedc7394900033d803cc47fd212cf1601ea89c874b328963719f500beea9127e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 947b91b9cd3497dce4fe4fa89d840424cb5cc7bde7e69812a84726127f1b650aae25ea6c83b36df1bce27c1b2c85c93decdf7d54512104b411f4ad0924637174
|
7
|
+
data.tar.gz: a0d74ade3287d9bdc31a43556f148a7c82356da2a0c2e82f03d38d27ad9abc7fdf5165424a676cfba20e808c5e8f0ea6076b5e0098ebc9c6a45fc150ce226dda
|
data/doc/class/init.md
CHANGED
@@ -6,7 +6,7 @@ To use it define a class and call `::init` like you would call `::attr` methods:
|
|
6
6
|
* pass the name of the arguments as symbols
|
7
7
|
* pass options at the end:
|
8
8
|
* `attr` : what getters and/or setters to define
|
9
|
-
* can be `:accessor`, `:reader` or `:
|
9
|
+
* can be `:accessor`, `:reader`, `:writer` or `:none`
|
10
10
|
* defaults to `:accessor`
|
11
11
|
* `kw` : if arguments are to be set as keyword arguments
|
12
12
|
* defaults to `false`
|
@@ -65,7 +65,7 @@ test.b
|
|
65
65
|
# 2
|
66
66
|
|
67
67
|
test.a = 3
|
68
|
-
#
|
68
|
+
# => raises NoMethodError
|
69
69
|
```
|
70
70
|
|
71
71
|
## Initialize uses keyword arguments
|
@@ -94,20 +94,25 @@ test.b
|
|
94
94
|
## Individual argument options
|
95
95
|
```ruby
|
96
96
|
class TestIndividualArgsOptions
|
97
|
-
init [:a, kw: true, attr: :reader],
|
97
|
+
init [:a, kw: true, attr: :reader],
|
98
|
+
:b,
|
99
|
+
[:c, kw: true],
|
100
|
+
[:d, attr: :none]
|
98
101
|
end
|
99
102
|
|
100
103
|
# would be the same as
|
101
104
|
class Test
|
102
105
|
attr_reader :a
|
103
106
|
attr_accessor :b, :c
|
104
|
-
def initialize(b, a:, c:)
|
107
|
+
def initialize(b, d, a:, c:)
|
105
108
|
@a = a
|
106
109
|
@b = b
|
110
|
+
@c = c
|
111
|
+
@d = d
|
107
112
|
end
|
108
113
|
end
|
109
114
|
|
110
|
-
test = Test.new(2, a: 1, c: 3)
|
115
|
+
test = Test.new(2, 4, a: 1, c: 3)
|
111
116
|
test.a
|
112
117
|
# 1
|
113
118
|
|
@@ -117,6 +122,9 @@ test.b
|
|
117
122
|
test.c
|
118
123
|
# 3
|
119
124
|
|
120
|
-
test.
|
121
|
-
#
|
122
|
-
|
125
|
+
test.d
|
126
|
+
# => raises NoMethodError
|
127
|
+
|
128
|
+
test.a = 5
|
129
|
+
# => raises NoMethodError
|
130
|
+
```
|
@@ -3,11 +3,11 @@
|
|
3
3
|
module Trinkets
|
4
4
|
module Class
|
5
5
|
module Init
|
6
|
-
ATTR = %i[accessor reader writer].freeze
|
6
|
+
ATTR = %i[accessor reader writer none].freeze
|
7
7
|
|
8
8
|
def init(*attrs, attr: ATTR.first, kw: false)
|
9
9
|
raise ArgumentError, 'At least 1 attribute is required.' if attrs.empty?
|
10
|
-
raise ArgumentError, '`attr` must be one of :accessor (default), :reader or :
|
10
|
+
raise ArgumentError, '`attr` must be one of :accessor (default), :reader, :writer or :none' unless ATTR.include?(attr)
|
11
11
|
|
12
12
|
default_attr_options = { attr: attr, kw: kw }
|
13
13
|
|
@@ -22,7 +22,7 @@ module Trinkets
|
|
22
22
|
h[name] = opts
|
23
23
|
end
|
24
24
|
|
25
|
-
attr_methods = ATTR
|
25
|
+
attr_methods = (ATTR - [:none])
|
26
26
|
.each_with_object({}) do |name, h|
|
27
27
|
h[name] = method("attr_#{name}")
|
28
28
|
end
|
@@ -30,9 +30,9 @@ module Trinkets
|
|
30
30
|
# even though options like `kw` aren't used, they serve here to validate the `attrs` options
|
31
31
|
attr_init = ->(name, attr: ATTR.first, kw: false) do
|
32
32
|
unless ATTR.include?(attr)
|
33
|
-
raise ArgumentError, "attr `#{name}`, option attr` must be one of :accessor (default), :reader or :
|
33
|
+
raise ArgumentError, "attr `#{name}`, option attr` must be one of :accessor (default), :reader, :writer or :none"
|
34
34
|
end
|
35
|
-
attr_methods[attr].call(name)
|
35
|
+
attr_methods[attr].call(name) unless attr == :none
|
36
36
|
end
|
37
37
|
|
38
38
|
attrs.each { |name, opts| attr_init.call(name, **opts) }
|
data/lib/trinkets/version.rb
CHANGED