with_ease 0.0.2 → 0.0.4

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
  SHA256:
3
- metadata.gz: 1acaa86f823eeec13d846b6af27f39e94ec5bffa9d19fa81ce78994680efbfa5
4
- data.tar.gz: 2c23c88589ed075d3289e27528e9d8ceb1acda6eeee0a45e6c21cae6a95240ef
3
+ metadata.gz: 5e01a06e7d3664b42c554856f9f14a424c739e15fa7463cb3afb59b975d71095
4
+ data.tar.gz: 9375604296c49a99b23e7b504ed0c98cff36a9c0269519294ab5bf815726ac21
5
5
  SHA512:
6
- metadata.gz: a6cb7b523b7481b4696242a2addd533cd08c158f6652e8e8ab11b2aa6cbeb262abfbde383a14ad51e3dda8013ff45ba431901fdcd075636aaf48d73c10aac6f8
7
- data.tar.gz: 2f5c8b59bf12285100c0cabf7a592a55117a8b6c0e0e9d946141d14c76290c24c2770a8bc519210ac4b58cf3bf7c5b45a73292d570aec7e6f79d97a87f46937e
6
+ metadata.gz: 52a5632e7e4de8255098a3b82dfe158b6bc518b7e3a64ecc69b39d186a7a15bca82b18ba6aea8b4d3afb80ee57e9470e49f4f4f8658a714e8152fcf8f0ba66a8
7
+ data.tar.gz: c9009ed9fd714345ffac18bf42a97a574ec5753939b4a2f48edc6068efe17d36e61f3b271b4d7140384b0ea4656251d5e1db9577500687618fdcad85d3a8ff9a
@@ -9,7 +9,7 @@ module WithEase
9
9
  extend Forward
10
10
  include HashApi
11
11
 
12
- forward :deconstruct_keys, :fetch, :to_a, :to_h, to: :@data
12
+ forward :[], :[]=, :deconstruct_keys, :fetch, :to_a, :to_h, to: :@data
13
13
  forward :deconstruct, to: :to_a
14
14
 
15
15
  # def fetch(key, *defaults, &blk)
@@ -28,6 +28,12 @@ module WithEase
28
28
  def initialize(**data, &blk)
29
29
  @data = data.dup
30
30
  @keys = Set.new(data.keys)
31
+ (class << self; self; end).module_eval do
32
+ data.each do |k,|
33
+ define_method k do @data.fetch(k) end
34
+ define_method "#{k}=" do |val| @data[k] = val end
35
+ end
36
+ end
31
37
  end
32
38
 
33
39
  end
@@ -6,5 +6,5 @@ require_relative 'open_struct'
6
6
 
7
7
  def ClosedStruct(**k) = WithEase::ClosedStruct.new(**k)
8
8
  def Iterator(init, &blk) = WithEase::Iterator.new(init, &blk)
9
- def OpenStruct(**k) = WithEase::OpenStruct.new(**k)
9
+ def OpenStruct(**k) = OpenStruct.new(**k)
10
10
  # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -16,15 +16,18 @@ module WithEase
16
16
  end
17
17
  end
18
18
 
19
- def update(hash_like=nil, **keys, &blk)
19
+ def update(*positionals, **keys, &blk)
20
20
  if blk
21
- return _update_with_blk(hash_like, **keys, &blk)
21
+ return _update_with_blk(*positionals, **keys, &blk)
22
22
  end
23
23
 
24
- if hash_like
24
+ case positionals
25
+ in []
26
+ _update(keys)
27
+ in [hash_like]
25
28
  _update(hash_like.to_h.merge(**keys))
26
29
  else
27
- _update(keys)
30
+ raise ArgumentError, "must not provide more than one postional argument"
28
31
  end
29
32
  end
30
33
 
@@ -47,10 +50,23 @@ module WithEase
47
50
  self
48
51
  end
49
52
 
50
- def _update_with_blk(key, **keys, &blk)
51
- raise ArgumentError, "no **keys arguments allowed in block form" unless keys.empty?
52
- value = fetch(key)
53
- _update(key => blk.(value))
53
+ def _update_with_blk(*positionals, **keys, &blk)
54
+ case positionals
55
+ in []
56
+ new_hash = to_h.update(**keys, &blk)
57
+ _update(new_hash)
58
+ in [key]
59
+ if keys.has_key?(:default)
60
+ value = fetch(key, keys.delete(:default))
61
+ raise ArgumentError, "only :default key is allowed with positional key" unless keys.empty?
62
+ _update(key => blk.(value))
63
+ else
64
+ value = fetch(key)
65
+ raise ArgumentError, "only :default key is allowed with positional key" unless keys.empty?
66
+ _update(key => blk.(value))
67
+ end
68
+ in [_, _, *]
69
+ end
54
70
  end
55
71
  end
56
72
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WithEase
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.4"
5
5
  end
6
6
  # SPDX-License-Identifier: AGPL-3.0-or-later
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: with_ease
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober