generated from athena-framework/component-template
/
property_path.cr
23 lines (21 loc) · 1017 Bytes
/
property_path.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Utility type for working with property paths.
module Athena::Validator::PropertyPath
# Appends the provided *sub_path* to the provided *base_path* based on the following rules:
#
# * If the base path is empty, the sub path is returned as is.
# * If the base path is not empty, and the sub path starts with an `[`,
# the concatenation of the two paths is returned.
# * If the base path is not empty, and the sub path does not start with an `[`,
# the concatenation of the two paths is returned, separated by a `.`.
#
# ```
# AVD::PropertyPath.append "", "sub_path" # => "sub_path"
# AVD::PropertyPath.append "base_path", "[0]" # => "base_path[0]"
# AVD::PropertyPath.append "base_path", "sub_path" # => "base_path.sub_path"
# ```
def self.append(base_path : String, sub_path : String) : String
return base_path if sub_path.blank?
return "#{base_path}#{sub_path}" if sub_path.starts_with? '['
!base_path.blank? ? "#{base_path}.#{sub_path}" : sub_path
end
end