Module: Orientdb4r::Utils

Included in:
Client, Node, Rid
Defined in:
lib/orientdb4r/utils.rb

Defined Under Namespace

Classes: Proxy

Instance Method Summary collapse

Instance Method Details

#blank?(str) ⇒ Boolean

Checks if a given string is either ‘nil’ or empty string.

Returns:

  • (Boolean)


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

Yields:

  • (rslt)

Raises:

  • (ArgumentError)


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 verify_and_sanitize_options(options, pattern)
  verify_options(options, pattern)

  # set default values if missing in options
  pattern.each do |k,v|
    options[k] = v if !v.nil? and :optional != v and !options.keys.include? k
  end
  options
end

#verify_options(options, pattern) ⇒ Object

Raises:

  • (ArgumentError)


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 verify_options(options, pattern)
  raise ArgumentError, 'options cannot be nil' if options.nil?

  # unknown key?
  options.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 !options.keys.include? k
  end
  # option in a set of allowed values
  pattern.each do |k,v|
    if v.instance_of? Array
      raise ArgumentError, "value '#{options[k]}' not in #{v.inspect}, key=#{k}" unless v.include?(options[k])
    end
  end

  options
end