Skip to content

Instantly share code, notes, and snippets.

@a6b8
Last active October 15, 2021 16:59
Show Gist options
  • Select an option

  • Save a6b8/d2edaadcdd93fbc22fffe977079abe64 to your computer and use it in GitHub Desktop.

Select an option

Save a6b8/d2edaadcdd93fbc22fffe977079abe64 to your computer and use it in GitHub Desktop.
Options Validation
def options_update( options, obj, validation )
def str_difference( a, b )
a = a.to_s.downcase.split( '_' ).join( '' )
b = b.to_s.downcase.split( '_' ).join( '' )
longer = [ a.size, b.size ].max
same = a
.each_char
.zip( b.each_char )
.select { | a, b | a == b }
.size
( longer - same ) / a.size.to_f
end
allow_list =[
:format__title__symbol__video,
:format__title__symbol__custom,
:format__title__symbol__web,
:format__title__separator,
:format__title__length,
:format__title__str, :channels
]
wildcards = [
:options__s3
]
messages = []
insert = Marshal.load( Marshal.dump( obj ) )
options.keys.each do | key |
if allow_list.include?( key )
keys = key.to_s.split( '__' ).map { | a | a.to_sym }
case( keys.length )
when 1
insert[ keys[ 0 ] ] = options[ key ]
when 2
insert[ keys[ 0 ] ][ keys[ 1 ] ] = options[ key ]
when 3
insert[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ] = options[ key ]
when 4
insert[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ][ keys[ 3 ] ] = options[ key ]
end
else
standard = true
keys = key.to_s.split( '__' ).map { | a | a.to_sym }
case keys.length
when 1
inside = wildcards
.map { | a | a.to_s.split( '__' ).first.to_sym }
.include?( keys[ 0 ] )
if inside
message = "\"#{key}\" is a potential Wildcard key but has an invalid length. Use two additional keys (plus '__') to set your option."
messages.push( message )
standard = false
else
end
when 2..3
wildcard = [ keys[ 0 ].to_s, keys[ 1 ].to_s ].join( '__' ).to_sym
if wildcards.include?( wildcard )
if keys.length == 2
message = "\"#{key}\" is a Wildcard key but has an invalid length. Use an additional key (plus '__') to set your option."
messages.push( message )
standard = false
else
!insert.keys.include?( keys[ 0 ] ) ? insert[ keys[ 0 ] ] = {} : ''
!insert[ keys[ 0 ] ].keys.include?( keys[ 1 ] ) ? insert[ keys[ 0 ] ][ keys[ 1 ] ] = {} : ''
insert[ keys[ 0 ] ][ keys[ 1 ] ][ keys[ 2 ] ] = options[ key ]
standard = false
end
else
end
else
end
if standard
nearest = allow_list
.map { | word | { score: self.str_difference( key, word ), word: word } }
.min_by { | item | item[:score] }
message = "\"#{key}\" is not a valid key, did you mean \"<--similar-->\"?"
message = message.gsub( '<--similar-->', nearest[:word].to_s )
messages.push( message )
end
end
end
if messages.length != 0
messages.length == 1 ? puts( 'Error found:' ) : puts( 'Errors found:' )
messages.each { | m | puts( '- ' + m ) }
end
return validation ? messages.length == 0 : insert
end
obj = {
format: {
title: {
symbol: {
video: '👾',
custom: '⚙️',
web: '🤖'
},
separator: '|',
length: 50,
str: '{{sym}} {{title_meta__upcase}} {{separator}} {{title_item}}'
}
},
channels: [],
options: {}
}
options = {
options__s3__bucket_name: 'aaa',
options__s3__bucket_sub_folder: 'bbb',
options__s3__region: 'ccc',
options__s3__folder: 'ddd',
options: 'test',
options__s2: 'test',
options__s3: 'test'
}
if options_update( options, obj, true )
## DO STUFF
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment