structure 3.0.0 → 3.1.0

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: 7f0f08ca763d757b47a9ff12c51168361a37acdfec5db9d7c45a82c15f9059fb
4
- data.tar.gz: 7ea79c6470d4fabe005474f572bf3f9564217dca45f18b118babf94c6e8ef36f
3
+ metadata.gz: af5b8b6dc3dacef6201847123b04742310441ded70ec7952b42d07a668d83f2f
4
+ data.tar.gz: '00385227416285e5a4d1f2cfcf2cc0e5cb94bb368cf8995b014a7bf3fd91d9c6'
5
5
  SHA512:
6
- metadata.gz: c37505be3556ec54d0277008506868f4fade809f8baa800336f54b32ceca265c61ee232ceca7326b2104793d6af8a6022fb193c61fbb28aaeb9d0493223b6677
7
- data.tar.gz: 725e923fddcb8cca372340d12c9cf67c38d20782dac2a1fc2b1273e06e12d6cc712668c6a634d32af4661d8b70c8369e25fda5109e6e80e97ad65ddc0babf8d7
6
+ metadata.gz: ba80d09a4867a1497133026cc4d4654e03586f677d44d07a7f2387b40fce16e8fc188bc46cc8c768663ded6387b580d3dc210ae884d14530bf62e7e56ec4ace4
7
+ data.tar.gz: 8a0c8e7d5f9585a946817cc31a3cd831ea0332833030798d178ce81ee1737809226321ac105f106559358aa4ae24753368d808007ef6ab37230b6d6fc98ffe69
@@ -5,7 +5,7 @@ require "structure/types"
5
5
  module Structure
6
6
  # Builder class for accumulating attribute definitions
7
7
  class Builder
8
- attr_reader :mappings, :types, :defaults
8
+ attr_reader :mappings, :types, :defaults, :after_parse_callback
9
9
 
10
10
  def initialize
11
11
  @mappings = {}
@@ -41,5 +41,9 @@ module Structure
41
41
  end
42
42
  end.to_h
43
43
  end
44
+
45
+ def after_parse(&block)
46
+ @after_parse_callback = block
47
+ end
44
48
  end
45
49
  end
@@ -11,6 +11,7 @@ module Structure
11
11
  private_constant :BOOLEAN_TRUTHY
12
12
 
13
13
  # Boolean conversion
14
+ # Memoized so predicate method detection works via object identity comparison
14
15
  def boolean
15
16
  @boolean ||= ->(val) { BOOLEAN_TRUTHY.include?(val) }
16
17
  end
@@ -20,8 +21,8 @@ module Structure
20
21
  ->(val) { Kernel.send(type.name, val) }
21
22
  end
22
23
 
23
- # Handler for nested Structure classes
24
- def structure(type)
24
+ # Handler for classes with parse methods (e.g., Date, Time, URI, nested Structure classes)
25
+ def parseable(type)
25
26
  ->(val) { type.parse(val) }
26
27
  end
27
28
 
@@ -36,11 +37,11 @@ module Structure
36
37
  case type
37
38
  when :boolean
38
39
  boolean
39
- when Class
40
+ when Class, Module
40
41
  if type.name && Kernel.respond_to?(type.name)
41
42
  kernel(type)
42
43
  elsif type.respond_to?(:parse)
43
- structure(type)
44
+ parseable(type)
44
45
  else
45
46
  type
46
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Structure
4
- VERSION = "3.0.0"
4
+ VERSION = "3.1.0"
5
5
  end
data/lib/structure.rb CHANGED
@@ -23,6 +23,7 @@ module Structure
23
23
  types = builder.types
24
24
  defaults = builder.defaults
25
25
  attributes = builder.attributes
26
+ after_parse_callback = builder.after_parse_callback
26
27
 
27
28
  data_class.define_singleton_method(:parse) do |data = {}, **kwargs|
28
29
  # Merge kwargs into data - kwargs take priority as overrides
@@ -48,7 +49,10 @@ module Structure
48
49
 
49
50
  final_kwargs[attr] = value
50
51
  end
51
- new(**final_kwargs)
52
+
53
+ instance = new(**final_kwargs)
54
+ after_parse_callback&.call(instance)
55
+ instance
52
56
  end
53
57
 
54
58
  data_class
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
@@ -9,7 +9,8 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
- description: Parse data into immutable Ruby Data objects with type coercion
12
+ description: Provides a DSL for generating immutable Ruby Data objects with type coercion
13
+ and data transformation capabilities.
13
14
  executables: []
14
15
  extensions: []
15
16
  extra_rdoc_files: []
@@ -38,5 +39,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
39
  requirements: []
39
40
  rubygems_version: 3.6.9
40
41
  specification_version: 4
41
- summary: Structure your data!
42
+ summary: Structure your data
42
43
  test_files: []