Class: Reedb::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/reedb/utils/utilities.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.capitalise_first(string) ⇒ Object

Normalises name to have the first letter be capital



68
69
70
# File 'lib/reedb/utils/utilities.rb', line 68

def self.capitalise_first(string)
  string[0].upcase + string.slice(1..-1)
end

.check_port(port) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/reedb/utils/utilities.rb', line 112

def self.check_port(port)
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new('127.0.0.1', port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue Timeout::Error
    return false
  end
end

.get_time(only_date = false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/reedb/utils/utilities.rb', line 128

def self.get_time(only_date = false)
  time = Time.now
  val_h = "#{time.year}-#{'%02d' % time.month}-#{'%02d' % time.day}"
  val_t = "#{time.hour}:#{'%02d' % time.min}:#{'%02d' % time.sec}"

  # => TODO: Make this more Ruby-Like
  if only_date
    return "#{val_h}"
  else
    return "[#{val_h} #{val_t}]"
  end
end

.increment_version(version) ⇒ Object

Takes version string and increments it by 1. Does patch-minor and minor-major steps TODO: Replace this with a RegEx to split the version string at the dots, and analise it’s sub-elements.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/reedb/utils/utilities.rb', line 44

def self.increment_version(version)
  # <b>DEPRECATED:</b> Please use <tt>Version class</tt> instead.
  warn "[DEPRECIATED] `increment_version` is depreciated. Please use Version class instead."

  if version?(version) # Checks if it actually is version
    i = version[2].to_i

    tmp = version

    if i < 9
      i += 1
      tmp = version[0] + "." + i.to_s

    elsif i >= 9
      i = version[0].to_i
      i += 1
      tmp = i.to_s + "." + "0".to_s
    end

    return tmp
  end
end

.parse_hostObject



98
99
100
# File 'lib/reedb/utils/utilities.rb', line 98

def self.parse_host
  return "#{Socket.gethostname}"
end

.parse_osObject

Fix the actual inputs (aka test on virtual machines)



87
88
89
90
91
92
93
94
95
96
# File 'lib/reedb/utils/utilities.rb', line 87

def self.parse_os
  platform = RUBY_PLATFORM
  if platform.end_with?('linux')
    return :linux
  elsif platform.end_with?('Windows')
    return :win
  elsif platform.end_with?('Mac OS X')
    return :osx
  end
end

.parse_userObject

Returns user currently logged in. Not sure for what that will be used but hey!



104
105
106
107
108
109
110
# File 'lib/reedb/utils/utilities.rb', line 104

def self.parse_user
  if parse_os == :win
    return ENV['USERNAME']
  else
    return ENV['USER']
  end
end

.version?(version) ⇒ Boolean

Returns if the provided string is actually a version string.

> I love you Ashley, stop looking in my source :‘)

TODO: Use RegEx to scan through ENTIRE string, not just mayor and minor version

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/reedb/utils/utilities.rb', line 78

def self.version?(version)
  def is_i?(i)
    i.to_i.to_s == i
  end

  is_i?(version[0]) and is_i?(version[2]) ? true : false
end

Instance Method Details

#is_i?(i) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/reedb/utils/utilities.rb', line 79

def is_i?(i)
  i.to_i.to_s == i
end