tins 1.40.0 → 1.41.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: a66e94fcc54524299e535c56b267687c56d5ac3a453bb568d7cfd0cf25741ab2
4
- data.tar.gz: 8f15d1bb74a3a603c777198d8d4cf621c89872a6b03cd10db2cab54e68ba92cd
3
+ metadata.gz: 51a0fa48c00468630ba973991e30b4fe09ded6527da24252217dc9759d103600
4
+ data.tar.gz: 97512d866988df88e2855d79a08aad717cad1ee5c4a94141d3f0b3a869ec7e70
5
5
  SHA512:
6
- metadata.gz: 6186ed2a25db87a92138dbf685b59c4d86234a3eca252b40fd4a6828a869e0e95573ca2949c724085657b13355767b0c71a309d6d21c31f6ecd85c8486ef7d37
7
- data.tar.gz: 44382be24e42acca56c3992dd816334c77acd2dba000144523e95e67c264240823115bbd4c2265e1803981b7cac369b918cae883a31ff5c3f86de9dab5105cec
6
+ metadata.gz: 31075326ab8bb69b9e4f5b33426336cfa97a16fc683bf9a620007eff42aa640ae1526629c6076bd3c9e8e0e283dfe4c19d27ca5dd043733a32fc3c308826c2fe
7
+ data.tar.gz: 65d1a0424696cdfd87e8deeea3952791d8387f4cdd9e4a4358e79bc56e0a722c15f5d436964527af1f3a779cc042f2375cba1cbbc806d18a991c96560fec5c2a
data/CHANGES.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changes
2
2
 
3
+ ## 2025-08-18 v1.41.0
4
+
5
+ - Added new `named_placeholders_interpolate` method for template substitution
6
+ - Method supports both static and dynamic default values via Proc
7
+ - Maintains backward compatibility with existing `named_placeholders_assign` method
8
+ - Includes comprehensive tests for all functionality and error handling
9
+ - Uses `named_placeholders_assign` internally for consistent implementation
10
+
3
11
  ## 2025-08-18 v1.40.0
4
12
 
5
13
  - Added `Tins::StringNamedPlaceholders` module with `named_placeholders` and
@@ -49,5 +49,22 @@ module Tins
49
49
  (default.is_a?(Proc) ? default.(placeholder) : default)
50
50
  end
51
51
  end
52
+
53
+ # Interpolate named placeholders in the string with values from a hash.
54
+ #
55
+ # This method takes a hash of placeholder values and substitutes the named
56
+ # placeholders found in the string with their corresponding values.
57
+ # Placeholders that are not present in the input hash will be replaced with
58
+ # the provided default value.
59
+ #
60
+ # @param hash [Hash] A hash mapping placeholder names to their corresponding values
61
+ # @param default [Object, Proc] The default value to use for placeholders not present in the hash
62
+ # If a proc is provided, it will be called with the placeholder symbol
63
+ #
64
+ # @return [String] A new string with named placeholders replaced by their values
65
+ def named_placeholders_interpolate(hash, default: nil)
66
+ values = named_placeholders_assign(hash, default:)
67
+ self % values
68
+ end
52
69
  end
53
70
  end
data/lib/tins/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Tins
2
2
  # Tins version
3
- VERSION = '1.40.0'
3
+ VERSION = '1.41.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -65,5 +65,45 @@ module Tins
65
65
  result = template % values
66
66
  assert_equal "Hello Charlie, you are 30 years old and live in NYC", result
67
67
  end
68
+
69
+ def test_named_placeholders_interpolate
70
+ # Basic interpolation with defaults
71
+ result = "Hello %{name}, you are %{age} years old".named_placeholders_interpolate({name: 'Alice'}, default: '[n/a]')
72
+ assert_equal "Hello Alice, you are [n/a] years old", result
73
+
74
+ # All values provided - should work without defaults
75
+ result = "Hello %{name}, you are %{age} years old".named_placeholders_interpolate({name: 'Bob', age: 30})
76
+ assert_equal "Hello Bob, you are 30 years old", result
77
+
78
+ # Dynamic defaults via Proc
79
+ result = "Hello %{name}, you are %{age} years old".named_placeholders_interpolate(
80
+ {name: 'Charlie'},
81
+ default: ->(key) { "[missing_#{key}]" }
82
+ )
83
+ assert_equal "Hello Charlie, you are [missing_age] years old", result
84
+
85
+ # Key conversion from string keys
86
+ result = "Hello %{name}, you are %{age} years old".named_placeholders_interpolate(
87
+ {'name' => 'David'},
88
+ default: '[n/a]'
89
+ )
90
+ assert_equal "Hello David, you are [n/a] years old", result
91
+
92
+ # No placeholders in template
93
+ result = "Hello World".named_placeholders_interpolate({some_key: 'value'})
94
+ assert_equal "Hello World", result
95
+
96
+ # Empty string values
97
+ result = "Hello %{name}".named_placeholders_interpolate({name: ''}, default: '[n/a]')
98
+ assert_equal "Hello ", result
99
+
100
+ # Raise custom expcetion if missing
101
+ template = "Hello %{name}, you are %{age} years old and live in %{city}"
102
+ assert_raise(ArgumentError, "Required placeholder age not provided") do
103
+ template.named_placeholders_interpolate(
104
+ {name: 'Alice'},
105
+ default: ->(key) { raise ArgumentError, "Required placeholder #{key} not provided" })
106
+ end
107
+ end
68
108
  end
69
109
  end
data/tins.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: tins 1.40.0 ruby lib
2
+ # stub: tins 1.41.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "tins".freeze
6
- s.version = "1.40.0".freeze
6
+ s.version = "1.41.0".freeze
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tins
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.40.0
4
+ version: 1.41.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank