l43_open_object 0.1.1 → 0.2.1

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: 22850fd9e80553bee925ca5a0c43c6deb8468bad02a9d54a04defece69881565
4
- data.tar.gz: ac0a953bcfa8226a563fd4bab9fc23191bd22aa649e8f4216eec5a71473faf1d
3
+ metadata.gz: 43825e89f8a1bfb592c302ccfe57afc7ab6b73a35c6bcd6ce2bd2740df9f1e2f
4
+ data.tar.gz: c963406af910e5068933b5862f64e151ee1d2fd9c5a91f2d33361d6c847c27aa
5
5
  SHA512:
6
- metadata.gz: 3d605403bd858a5b718b478bac3878ec30eeb7d74c08e0167bff02038bb93aa56af40f9ffa7f5d35e3c229772f6a644060d7001a72604a6d0dcd8c4a0005aec5
7
- data.tar.gz: 9205bd62f6aa011fc1f3846f0f29f08135602ead4e7d8f12a116940401acfb87cad44b8c00ae9d3291b32bea662b2c154f7e5df77dd955fb66645a8f8ae3668c
6
+ metadata.gz: 554b5cbeddb5e20eb72ff281b930891445a3562183a0a44e3a31f872dcc57829c9ebc5dd83957ccee4a1a22b221d345c5d39494f0676246f3488efa11611f2c0
7
+ data.tar.gz: dc9c9f9d5a7f345336171e87f6b5ef964c9b86135b764e3c9ff756597d893be4df0a48ab2939dd84e006df5258100a33030cd0135129ee375baff3e6bf9e7a53
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module L43
4
+ module OpenObject
5
+ module Behavior
6
+
7
+ def ==(other)
8
+ return false unless self.class === other
9
+ other.to_h == to_h
10
+ end
11
+
12
+ def update(**kwds)
13
+ if frozen?
14
+ new_values = to_h.merge(**kwds)
15
+ self.class.new(**new_values)
16
+ else
17
+ kwds.each do |k, v|
18
+ instance_variable_set("@#{k}", v)
19
+ end
20
+ self
21
+ end
22
+ end
23
+
24
+ def update_attribute(attribute, &blk)
25
+ raise ArgumentError, "cannot update attribute #{attribute.inspect} without a block" unless blk
26
+ update(attribute => blk.(to_h[attribute]))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module L43
4
+ module OpenObject
5
+ module Initializer extend self
6
+
7
+ def define_initialize(klass, atts, defaults, blk)
8
+ klass.define_method :initialize do |**kwds|
9
+ missing = atts - kwds.keys
10
+ raise ArgumentError, "missing required keyword parameters: #{missing.inspect}" unless missing.empty?
11
+ spurious = kwds.keys - atts - defaults.keys
12
+ raise ArgumentError, "spurious keyword parameters: #{spurious.inspect}" unless spurious.empty?
13
+
14
+ values = defaults.merge(kwds)
15
+ values.each do |key, value|
16
+ instance_variable_set("@#{key}", value)
17
+ end
18
+ if respond_to?(:_init)
19
+ $stderr.puts("Automatic invocation the `_init` method is deprected and will be removed in v0.3")
20
+ _init
21
+ end
22
+ do_we_freeze = instance_exec(&blk) if blk
23
+ if do_we_freeze == :stay_warm
24
+ Initializer.define_dynamic_to_h(klass, values.keys)
25
+ else
26
+ Initializer.define_to_h(klass, values.keys)
27
+ to_h
28
+ freeze
29
+ end
30
+ end
31
+ end
32
+
33
+ def define_dynamic_to_h(klass, keys)
34
+ klass.define_method :to_h do |*|
35
+ keys.inject({}) do |r, k|
36
+ r.update(k => instance_variable_get("@#{k}"))
37
+ end
38
+ end
39
+ klass.alias_method :deconstruct_keys, :to_h
40
+ end
41
+
42
+ def define_to_h(klass, keys)
43
+ klass.define_method :to_h do |*|
44
+ @__to_h__ ||= keys.inject({}) do |r, k|
45
+ r.update(k => instance_variable_get("@#{k}"))
46
+ end
47
+ end
48
+ klass.alias_method :deconstruct_keys, :to_h
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -1,40 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'open_object/initializer'
4
+ require_relative 'open_object/behavior'
3
5
  module L43
4
6
  module OpenObject
5
- VERSION = "0.1.1"
7
+ VERSION = "0.2.1"
6
8
 
7
- def attributes(*atts, **defaults)
9
+ def attributes(*atts, **defaults, &blk)
8
10
  attr_reader(*atts)
9
11
  attr_reader(*defaults.keys)
10
12
 
11
- define_method :== do |other|
12
- return false unless self.class === other
13
- other.to_h == to_h
14
- end
13
+ Initializer.define_initialize(self, atts, defaults, blk)
14
+ include Behavior
15
15
 
16
- define_method :initialize do |**kwds|
17
- missing = atts - kwds.keys
18
- raise ArgumentError, "missing required keyword parameters: #{missing.inspect}" unless missing.empty?
19
- spurious = kwds.keys - atts - defaults.keys
20
- raise ArgumentError, "spurious keyword parameters: #{spurious.inspect}" unless spurious.empty?
16
+ # define_method :update do |**kwds|
17
+ # if frozen?
18
+ # new_values = to_h.merge(**kwds)
19
+ # self.class.new(**new_values)
20
+ # else
21
+ # kwds.each do |k, v|
22
+ # instance_variable_set("@#{k}", v)
23
+ # end
24
+ # self
25
+ # end
26
+ # end
21
27
 
22
- values = defaults.merge(kwds)
23
- values.each do |key, value|
24
- instance_variable_set("@#{key}", value)
25
- end
26
- _init if respond_to?(:_init)
27
- end
28
-
29
- define_method :to_h do |*|
30
- first = atts.inject Hash.new do |h, attribute|
31
- h.update(attribute => send(attribute))
32
- end
33
- defaults.keys.inject first do |h, attribute|
34
- h.update(attribute => send(attribute))
35
- end
36
- end
37
- alias_method :deconstruct_keys, :to_h
28
+ # define_method :update_attribute do |attribute, &blk|
29
+ # raise ArgumentError, "cannot update attribute #{attribute.inspect} without a block" unless blk
30
+ # update(attribute => blk.(to_h[attribute]))
31
+ # end
38
32
  end
39
33
  end
40
34
  end
metadata CHANGED
@@ -1,19 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: l43_open_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-25 00:00:00.000000000 Z
11
+ date: 2024-04-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  When using the class method `attributes` in a class you get:
15
15
  - an initialize method with attributes as kwd params and the respective defaults
16
+ - immutable instances
16
17
  - a to_h method using all attributes
18
+ - an update method to create new imutable instances
17
19
  - a destruct_keys method aliased to to_h for pattern matching
18
20
  email: robert.dober@gmail.com
19
21
  executables: []
@@ -23,6 +25,8 @@ files:
23
25
  - LICENSE
24
26
  - lib/l43.rb
25
27
  - lib/l43/open_object.rb
28
+ - lib/l43/open_object/behavior.rb
29
+ - lib/l43/open_object/initializer.rb
26
30
  homepage: https://codeberg.org/lab419/l43_open_object
27
31
  licenses:
28
32
  - AGPL-3.0-or-later