class NRSER::Types::HashType
A type who's members simply are {Hash} instances.
Implements {#from_s} to provide JSON/YAML detection, as well as “simple” loading aimed at CLI option values.
@note
Construct {HashType} types using the {.Hash} factory.
Public Class Methods
new(**options)
click to toggle source
Instantiate a new `HashType`.
Calls superclass method
# File lib/nrser/types/hashes.rb, line 37 def initialize **options super ::Hash, **options end
Public Instance Methods
default_symbolic()
click to toggle source
# File lib/nrser/types/hashes.rb, line 61 def default_symbolic "{#{ keys.symbolic }#{ ASSOC }#{ values.symbolic }}" end
keys()
click to toggle source
In order to provide the same interface as {HashOfType}, this method always returns {NRSER::Types.any}.
@return [NRSER::Types::Type]
# File lib/nrser/types/hashes.rb, line 50 def keys; NRSER::Types.Top; end
values()
click to toggle source
In order to provide the same interface as {HashOfType}, this method always returns {NRSER::Types.any}.
@return [NRSER::Types::Type]
# File lib/nrser/types/hashes.rb, line 58 def values; NRSER::Types.Top; end
Protected Instance Methods
custom_from_s(string)
click to toggle source
Hook to provide custom loading from strings, which will be called by {NRSER::Types::Type#from_s}, unless a `@from_s`
# File lib/nrser/types/hashes.rb, line 72 def custom_from_s string # Does it looks like a JSON / inline-YAML object? if NRSER.looks_like_json_object? string # It does! Load it begin return YAML.load string rescue # pass - if we failed to load as JSON, it may just not be JSON, and # we can try the split approach below. end end # Try parsing as a "simple string", aimed at CLI option values. from_simple_s string end
from_simple_s(string)
click to toggle source
# File lib/nrser/types/hashes.rb, line 89 def from_simple_s string hash = {} pair_strs = string.split NRSER::Types::ArrayType::DEFAULT_SPLIT_WITH pair_strs.each do |pair_str| key_str, match, value_str = pair_str.rpartition /\:\s*/m if match.empty? raise NRSER::Types::FromStringError.new( "Could not split pair string", pair_str, type: self, string: string, pair_str: pair_str, ) do <<~END Trying to parse a {Hash} out of a string using the "simple" approach, which splits 1. First by `,` (followed by any amount of whitespace) 2. Then by the last `:` in each of those splits (also followed) by any amount of whitespace) END end end key = if keys == NRSER::Types.any key_str else keys.from_s key_str end value = if values == NRSER::Types.any value_str else values.from_s value_str end hash[key] = value end hash end