Class: Saklient::Util
- Inherits:
-
Object
- Object
- Saklient::Util
- Defined in:
- lib/saklient/util.rb
Class Method Summary collapse
- .are_same(a, b) ⇒ Object
- .auto_rename(name, candidates) ⇒ Object
- .create_class_instance(classPath, args) ⇒ any
- .date2str(d) ⇒ String
- .exists_path(obj, path) ⇒ any
- .get_by_path(obj, path) ⇒ any
- .get_by_path_any(objects, pathes) ⇒ Object
- .ip2long(s) ⇒ Integer
- .long2ip(v) ⇒ String
- .set_by_path(obj, path, value) ⇒ void
- .sleep(sec) ⇒ void
- .str2date(s) ⇒ NativeDate
- .url_encode(s) ⇒ String
- .validate_type(value, typeName, force = false) ⇒ void
Class Method Details
.are_same(a, b) ⇒ Object
158 159 160 |
# File 'lib/saklient/util.rb', line 158 def self.are_same(a, b) return a.equal?(b) end |
.auto_rename(name, candidates) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/saklient/util.rb', line 162 def self.auto_rename(name, candidates) m = /^(.*?)(\d+)(\W*)$/.match(name) if m then max = 0 len = 0 for c in candidates n = /^(.*?)(\d+)(\W*)$/.match(c) next unless n next unless m[1]==n[1] and m[3]==n[3] max = [max, n[2].to_i].max len = [len, n[2].length].max end if 0 < len return sprintf("%s%0" + len.to_s + "d%s", m[1], max+1, m[3]) end end return name + "-2" end |
.create_class_instance(classPath, args) ⇒ any
72 73 74 75 76 77 78 79 80 |
# File 'lib/saklient/util.rb', line 72 def self.create_class_instance(classPath, args) ret = nil if !(classPath).nil? apath = classPath.split('.') ret = apath.inject(Object) { |obj, name| obj.const_get(name[0].upcase + name[1..-1]) }.new(*args) end raise Exception.new('Could not create class instance of ' + classPath) if (ret).nil? return ret end |
.date2str(d) ⇒ String
91 92 93 94 |
# File 'lib/saklient/util.rb', line 91 def self.date2str(d) return nil if (d).nil? return d.to_s end |
.exists_path(obj, path) ⇒ any
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/saklient/util.rb', line 14 def self.exists_path(obj, path) aPath = path.split('.') for seg in aPath return false if (obj).nil? return false if !obj.is_a?(Hash) next if seg == '' return false if !(!obj.nil? && obj.key?(seg.to_sym)) obj = obj[seg.to_sym] end return true end |
.get_by_path(obj, path) ⇒ any
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/saklient/util.rb', line 29 def self.get_by_path(obj, path) aPath = path.split('.') for seg in aPath return nil if (obj).nil? return nil if !obj.is_a?(Hash) next if seg == '' return nil if !(!obj.nil? && obj.key?(seg.to_sym)) obj = obj[seg.to_sym] end return obj end |
.get_by_path_any(objects, pathes) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/saklient/util.rb', line 41 def self.get_by_path_any(objects, pathes) for obj in objects for path in pathes ret = get_by_path(obj, path) return ret if !ret.nil? end end return nil end |
.ip2long(s) ⇒ Integer
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/saklient/util.rb', line 98 def self.ip2long(s) return nil unless s.is_a? String return nil unless /^\d+\.\d+\.\d+\.\d+$/.match(s) ret = 0 s.split(/\./).each{|o| v = o.to_i ret = nil unless 0<=v && v<=255 ret = ret<<8 | v if !ret.nil? } return ret end |
.long2ip(v) ⇒ String
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/saklient/util.rb', line 112 def self.long2ip(v) v = v.to_i if v.is_a? String return nil unless v.is_a? Integer ret = [] v += (1<<32) if v<0 4.times{|| ret.unshift(v & 255) v >>= 8 } return nil if v != 0 return ret.join(".") end |
.set_by_path(obj, path, value) ⇒ void
TODO:
array support
TODO:
overwriting
TODO:
writing into children of non-object
This method returns an undefined value.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/saklient/util.rb', line 58 def self.set_by_path(obj, path, value) aPath = path.split('.') key = aPath.pop for seg in aPath next if seg == '' obj[seg.to_sym] = {} if !(!obj.nil? && obj.key?(seg.to_sym)) obj = obj[seg.to_sym] end obj[key.to_sym] = value end |
.sleep(sec) ⇒ void
This method returns an undefined value.
133 134 135 |
# File 'lib/saklient/util.rb', line 133 def self.sleep(sec) super self end |
.str2date(s) ⇒ NativeDate
84 85 86 87 |
# File 'lib/saklient/util.rb', line 84 def self.str2date(s) return nil if (s).nil? return DateTime.parse(s) end |
.url_encode(s) ⇒ String
127 128 129 |
# File 'lib/saklient/util.rb', line 127 def self.url_encode(s) return URI.encode(s.to_s) end |
.validate_type(value, typeName, force = false) ⇒ void
This method returns an undefined value.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/saklient/util.rb', line 140 def self.validate_type(value, typeName, force=false) isOk = false if !force then return if typeName=="any" || typeName=="void" || value.nil? clazz = value.class.to_s if typeName=="bool" isOk = clazz=="TrueClass" || clazz=="FalseClass" elsif typeName=="Float" isOk = clazz=="Fixnum" || clazz=="Float" elsif typeName=="String" isOk = clazz=="Fixnum" || clazz=="Float" || clazz=="String" else isOk = value.is_a?(Object.const_get(typeName)) end end raise Saklient::Errors::SaklientException.new('argument_type_mismatch', 'Argument type mismatch (expected '+typeName+' but got '+clazz+')') unless isOk end |