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 +4 -4
- data/CHANGES.md +8 -0
- data/lib/tins/string_named_placeholders.rb +17 -0
- data/lib/tins/version.rb +1 -1
- data/tests/string_named_placeholders.rb +40 -0
- data/tins.gemspec +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51a0fa48c00468630ba973991e30b4fe09ded6527da24252217dc9759d103600
|
4
|
+
data.tar.gz: 97512d866988df88e2855d79a08aad717cad1ee5c4a94141d3f0b3a869ec7e70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
@@ -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.
|
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.
|
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]
|