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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68ed0792dc692c54ba0b00f781bc49b0baf2ce0d79b98fca94c2807c5532e8d7
4
- data.tar.gz: f5724dff46a3b64e7ee40ac72fb852bc8e47e0b6683fc95704c901bbd19c7f0b
3
+ metadata.gz: 7b68e75535021ee65a9f04019645b52a4ef1a76f1c2dfa2d5d625790bef4823a
4
+ data.tar.gz: dedc7394900033d803cc47fd212cf1601ea89c874b328963719f500beea9127e
5
5
  SHA512:
6
- metadata.gz: 9d2738f9ce51557a84ae9c70896556d20cb321feb7ea31e9eee2be023dc25fa66bae7f85db4e720829b9d0338611b6c9c75e8fe7ac2f93df7db3728856c4bbf2
7
- data.tar.gz: 2b0bb1da03c91f7eebff9a9f1994b5590c52cee0ab11b8643b1155805be00c79017092fcf49c017d52f95f1a8d08fc59e84659d913d4e35c6784b8bd7b541cba
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 `:writer`
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
- # > raises ArgumentError
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], :b, [:c, kw: true]
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.a = 4
121
- # > raises ArgumentError
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 :writer' unless ATTR.include?(attr)
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 :writer"
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) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Trinkets
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trinkets
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
  - SilverPhoenix99