invariable 0.1.5 → 0.1.6

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: 7383243bf91697ac7bf303aacee00e9d05e67ce84f44552c71ec6c1cb5f3f7cd
4
+ data.tar.gz: c76a032018adade6412f3bef21903bbed7ad9c55b1f7d4584ce01621ee31c2d7
5
5
  SHA512:
6
- metadata.gz: 188bd0745fd85c54e7a41c9f0d5efd6e4ae2635791214c86d021791142bf5eacf1311b1839577d7f47e84966a903d86748929ee5efa5b5c0942f127a8fd39cc9
7
- data.tar.gz: 2bbbf4d596a92b8e13f10a445ad5cedd48c989cdd4ceec065f20c5385da7b0240b0748f225f5efb35e747328ba3cc1fad43ab9292bbb9d34607f759200152e3e
6
+ metadata.gz: f46f26ae8e84432bff462a31b686feb84a1d6eeeeb2d47251a6269d6a86bb9189ab766377e0976f80ac1390c898b1a6d68d2daf8d58c32bd7ccca28194f280d0
7
+ data.tar.gz: fa0fecd3fd9add3ea732ac6655c20be6e20acda2232c84d0d959397bddc89e050852a34f8d56822c34a5511712a0e2c14484006b321701947438f73bae15e7ae
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.6'
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,7 +234,7 @@ 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
  #
@@ -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,8 +363,10 @@ 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
 
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.6
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-10-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  An Invariable bundles a number of read-only attributes.