ruby_conversations 1.0.0 → 1.0.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3994d5897446552014de346528c5d0c1806fba6c39995fec45d451103e07bece
|
4
|
+
data.tar.gz: c53dceb24ddfa2535d0a3ebe47380bb6e9f79f2ec890335dbc2ec5b13d20a95d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86db4d62c0f983cf59ef3bc61e2e60d632ac45d86998e0dfb6263f390b3632d8477f556824dcfc572c8368ea33694a031cc23bdad00326ac876b67d78c1bee7b
|
7
|
+
data.tar.gz: 2b9a5f579db56d1d66c3cb147d47a282169680223e5e400d745a8543e9beaacef75e2c3ef339beec494741543781090b986d1435236d3c96a807c299013769a9
|
@@ -16,11 +16,9 @@ module RubyConversations
|
|
16
16
|
belongs_to :organization, optional: true
|
17
17
|
|
18
18
|
# Validations
|
19
|
-
validates :message, presence: true
|
20
19
|
validates :name, presence: true, uniqueness: true
|
21
20
|
validates :temperature, numericality: { greater_than_or_equal_to: 0.0 }, allow_nil: true
|
22
21
|
validates :role, presence: true, inclusion: { in: VALID_ROLES }
|
23
|
-
validate :validate_placeholders_format
|
24
22
|
|
25
23
|
# Scopes
|
26
24
|
scope :active, -> { where(active: true) }
|
@@ -50,55 +48,14 @@ module RubyConversations
|
|
50
48
|
end
|
51
49
|
|
52
50
|
def interpolate(inputs = {})
|
53
|
-
return message if placeholders.empty?
|
54
|
-
|
55
|
-
validate_required_variables!(inputs)
|
56
|
-
|
57
51
|
interpolated = message.dup
|
58
52
|
format(interpolated, **inputs)
|
59
53
|
end
|
60
54
|
|
61
|
-
def placeholders
|
62
|
-
return [] if message.nil?
|
63
|
-
|
64
|
-
matches = message.scan(/%<([^>]+)>/)
|
65
|
-
matches&.flatten&.uniq || []
|
66
|
-
end
|
67
|
-
|
68
|
-
def active?
|
69
|
-
active
|
70
|
-
end
|
71
|
-
|
72
|
-
def deactivate!
|
73
|
-
update!(active: false)
|
74
|
-
end
|
75
|
-
|
76
|
-
def activate!
|
77
|
-
update!(active: true)
|
78
|
-
end
|
79
|
-
|
80
55
|
private
|
81
56
|
|
82
57
|
def versioned_attributes
|
83
58
|
%i[content role]
|
84
59
|
end
|
85
|
-
|
86
|
-
def validate_placeholders_format
|
87
|
-
invalid_placeholders = placeholders.grep_v(/\A[a-z_][a-z0-9_]*\z/i)
|
88
|
-
|
89
|
-
return unless invalid_placeholders.any?
|
90
|
-
|
91
|
-
errors.add(:message, "contains invalid placeholders: #{invalid_placeholders.join(', ')}")
|
92
|
-
end
|
93
|
-
|
94
|
-
def validate_required_variables!(variables)
|
95
|
-
missing = placeholders - variables.keys.map(&:to_s)
|
96
|
-
raise ArgumentError, "Missing required variables: #{missing.join(', ')}" if missing.any?
|
97
|
-
|
98
|
-
extra = variables.keys.map(&:to_s) - placeholders
|
99
|
-
return unless extra.any?
|
100
|
-
|
101
|
-
raise ArgumentError, "Unknown variables provided: #{extra.join(', ')}"
|
102
|
-
end
|
103
60
|
end
|
104
61
|
end
|