Sha256: d74b3f0cd0d131c61008bc18365192d256ae79a74c6034d59ad4e9c6f4adfb48
Contents?: true
Size: 1.83 KB
Versions: 1
Compression:
Stored size: 1.83 KB
Contents
module EnsureIt patch Object do def ensure_symbol(default: nil, **opts) default end def ensure_symbol!(default: nil, **opts) EnsureIt.raise_error( :ensure_symbol!, **EnsureIt.ensure_symbol_error_options(**opts) ) end end patch String do def ensure_symbol(default: nil, values: nil, downcase: nil, **opts) value = downcase == true ? self.downcase.to_sym : to_sym if values.nil? || values.is_a?(Array) && values.include?(value) value else default end end def ensure_symbol!(default: nil, values: nil, downcase: nil, **opts) value = downcase == true ? self.downcase.to_sym : to_sym if values.nil? || values.is_a?(Array) && values.include?(value) return value end EnsureIt.raise_error( :ensure_symbol!, **EnsureIt.ensure_symbol_error_options(**opts) ) end end patch Symbol do def ensure_symbol(default: nil, values: nil, downcase: nil, **opts) value = downcase == true ? self.to_s.downcase.to_sym : self if values.nil? || values.is_a?(Array) && values.include?(value) value else default end end def ensure_symbol!(default: nil, values: nil, downcase: nil, **opts) value = downcase == true ? self.to_s.downcase.to_sym : self if values.nil? || values.is_a?(Array) && values.include?(value) return value end EnsureIt.raise_error( :ensure_symbol!, **EnsureIt.ensure_symbol_error_options(**opts) ) end end def self.ensure_symbol_error_options(**opts) unless opts.key?(opts[:message]) opts[:message] = '#{subject} should be' + if opts[:values].is_a?(Array) " one of #{opts[:values]}" else ' a Symbol or a String' end end opts end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ensure_it-0.1.3 | lib/ensure_it/ensure_symbol.rb |