Module: Orientdb4r::Utils
Defined Under Namespace
Classes: Proxy
Instance Method Summary collapse
-
#blank?(str) ⇒ Boolean
Checks if a given string is either ‘nil’ or empty string.
-
#compare_versions(first, second) {|rslt| ... } ⇒ Object
Compares two given versions.
-
#random_string(len = 8) ⇒ Object
Generates a random string with given length.
- #verify_and_sanitize_options(options, pattern) ⇒ Object
- #verify_options(options, pattern) ⇒ Object
Instance Method Details
#blank?(str) ⇒ Boolean
Checks if a given string is either ‘nil’ or empty string.
39 40 41 |
# File 'lib/orientdb4r/utils.rb', line 39 def blank?(str) str.nil? or (str.is_a? String and str.strip.empty?) end |
#compare_versions(first, second) {|rslt| ... } ⇒ Object
Compares two given versions.
Returns
-
1 if first > second
-
0 if first == second
-
-1 if first < second
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/orientdb4r/utils.rb', line 55 def compare_versions(first, second) raise ArgumentError, "bad version format, version=#{first}" unless first =~ Orientdb4r::Client::SERVER_VERSION_PATTERN raise ArgumentError, "bad version format, version=#{second}" unless second =~ Orientdb4r::Client::SERVER_VERSION_PATTERN firstv = /^(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)/.match(first)[0] secondv = /^(?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)/.match(second)[0] rslt = 0 rslt = 1 if firstv > secondv rslt = -1 if firstv < secondv yield rslt if block_given? rslt end |
#random_string(len = 8) ⇒ Object
Generates a random string with given length.
45 46 47 |
# File 'lib/orientdb4r/utils.rb', line 45 def random_string(len=8) (0...len).map{65.+(rand(25)).chr}.join end |
#verify_and_sanitize_options(options, pattern) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/orientdb4r/utils.rb', line 26 def (, pattern) (, pattern) # set default values if missing in options pattern.each do |k,v| [k] = v if !v.nil? and :optional != v and !.keys.include? k end end |
#verify_options(options, pattern) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/orientdb4r/utils.rb', line 5 def (, pattern) raise ArgumentError, 'options cannot be nil' if .nil? # unknown key? .keys.each do |k| raise ArgumentError, "unknow option: #{k}" unless pattern.keys.include? k end # missing mandatory option? pattern.each do |k,v| raise ArgumentError, "missing mandatory option: #{k}" if v == :mandatory and !.keys.include? k end # option in a set of allowed values pattern.each do |k,v| if v.instance_of? Array raise ArgumentError, "value '#{[k]}' not in #{v.inspect}, key=#{k}" unless v.include?([k]) end end end |