invariable 0.1.5 → 0.1.7

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: 16fb490a9818d273fc7871107dbe7f3e0c6c2bd8c4e3900c4d72e612e2979b53
4
- data.tar.gz: 827067332845002a0a6d2c58782ba664d393a19cf0177160e4ae7f9dfb005357
3
+ metadata.gz: 99bb0d3d7fca01b1a3d5bdbe965a43f4dd5bbc95f90e9278a81eeb7b00448c86
4
+ data.tar.gz: 90adb109963224f9e4b5638e6065ab8366d78e4be3f63f7aa99c7f321feeaeaa
5
5
  SHA512:
6
- metadata.gz: 188bd0745fd85c54e7a41c9f0d5efd6e4ae2635791214c86d021791142bf5eacf1311b1839577d7f47e84966a903d86748929ee5efa5b5c0942f127a8fd39cc9
7
- data.tar.gz: 2bbbf4d596a92b8e13f10a445ad5cedd48c989cdd4ceec065f20c5385da7b0240b0748f225f5efb35e747328ba3cc1fad43ab9292bbb9d34607f759200152e3e
6
+ metadata.gz: 65052fcc48d1ef7259424c0e7abb63d6f47cbf813de785ce39df80aaa0d566175021d0070ffccde09a36c92107acdd93260385f643cc6e07e803c0cee3612a76
7
+ data.tar.gz: 692ba41bae06488d1ada5be7f02aa4a3045538999f38340b1fea583113061363193553885cceddb80813d37543901af59afd093ef849ee7c0f12d1183c56078a
data/.yardopts CHANGED
@@ -3,4 +3,3 @@
3
3
  --charset utf-8
4
4
  --markup markdown
5
5
  lib/**/*.rb - LICENSE
6
-
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Invariable
4
4
  # current version number
5
- VERSION = '0.1.5'
5
+ VERSION = '0.1.7'
6
6
  end
data/lib/invariable.rb CHANGED
@@ -161,6 +161,11 @@ module Invariable
161
161
  @__attr__.values[arg]
162
162
  end
163
163
 
164
+ # @!visibility private
165
+ def deconstruct
166
+ @__attr__.values
167
+ end
168
+
164
169
  # @!visibility private
165
170
  def deconstruct_keys(...)
166
171
  @__attr__.deconstruct_keys(...)
@@ -195,9 +200,7 @@ module Invariable
195
200
  # @return [Enumerator]
196
201
  #
197
202
  def each(&block)
198
- return to_enum(__method__) unless block
199
- @__attr__.each_value(&block)
200
- self
203
+ block ? @__attr__.each_value(&block) : to_enum(__method__)
201
204
  end
202
205
 
203
206
  #
@@ -214,9 +217,7 @@ module Invariable
214
217
  # @return [Enumerator]
215
218
  #
216
219
  def each_pair(&block)
217
- return to_enum(__method__) unless block
218
- @__attr__.each_pair(&block)
219
- self
220
+ block ? @__attr__.each_pair(&block) : to_enum(__method__)
220
221
  end
221
222
 
222
223
  #
@@ -233,17 +234,17 @@ module Invariable
233
234
 
234
235
  # @!visibility private
235
236
  def hash
236
- @__hash__ ||= (to_a << self.class).hash
237
+ @__hash__ ||= (@__attr__.values << self.class).hash
237
238
  end
238
239
 
239
- #
240
- # @return [String] description of itself as a string
241
- #
240
+ alias __to_s to_s
241
+ private :__to_s
242
+
243
+ # @!visibility private
242
244
  def inspect
243
245
  attributes = @__attr__.map { |k, v| "#{k}: #{v.inspect}" }
244
- "<#{self.class}::#{__id__} #{attributes.join(', ')}>"
246
+ "#{__to_s[..-2]} #{attributes.join(', ')}>"
245
247
  end
246
- alias to_s inspect
247
248
 
248
249
  #
249
250
  # @return [Boolean] whether the given name is a valid attribute name
@@ -277,11 +278,6 @@ module Invariable
277
278
  end
278
279
  alias values to_a
279
280
 
280
- # @!visibility private
281
- def deconstruct
282
- @__attr__.values
283
- end
284
-
285
281
  #
286
282
  # @overload to_h
287
283
  # @return [{Symbol => Object}] names and values of all attributes
@@ -300,8 +296,8 @@ module Invariable
300
296
  # @return [{Object => Object}] pairs returned by the `block`
301
297
  #
302
298
  def to_h(compact: false, &block)
303
- return to_compact_h if compact
304
299
  return Hash[@__attr__.map(&block)] if block
300
+ return __to_compact_h if compact
305
301
  @__attr__.transform_values { |v| v.is_a?(Invariable) ? v.to_h : v }
306
302
  end
307
303
 
@@ -326,7 +322,7 @@ module Invariable
326
322
 
327
323
  private
328
324
 
329
- def to_compact_h
325
+ def __to_compact_h
330
326
  result = {}
331
327
  @__attr__.each_pair do |key, value|
332
328
  next if value.nil?
@@ -367,17 +363,18 @@ module Invariable
367
363
  if method_defined?(name)
368
364
  raise(NameError, "attribute already defined - #{name}", caller(4))
369
365
  end
370
- define_method(name) { @__attr__[name] }
371
366
  @__attr__[name] = default.is_a?(Class) ? default : default.dup.freeze
367
+ # accessing "eval-ed" methods is faster than accessing methods defined
368
+ # via #define_method :/
369
+ class_eval("def #{name};@__attr__[:#{name}];end")
372
370
  name
373
371
  end
374
372
 
375
373
  def __attr__init
376
374
  if superclass.instance_variable_defined?(:@__attr__)
377
- Hash[superclass.instance_variable_get(:@__attr__)]
378
- else
379
- {}.compare_by_identity
375
+ return Hash[superclass.instance_variable_get(:@__attr__)]
380
376
  end
377
+ {}.compare_by_identity
381
378
  end
382
379
  end
383
380
  private_constant(:InvariableClassMethods)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: invariable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-31 00:00:00.000000000 Z
11
+ date: 2022-12-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  An Invariable bundles a number of read-only attributes.
@@ -41,14 +41,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: 2.7.0
44
+ version: 3.0.0
45
45
  required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  requirements: []
51
- rubygems_version: 3.3.7
51
+ rubygems_version: 3.4.1
52
52
  signing_key:
53
53
  specification_version: 4
54
54
  summary: The Invariable data class for Ruby.