Module: Knj::ArrayExt

Defined in:
lib/knj/arrayext.rb

Class Method Summary collapse

Class Method Details

.array_hash(arr) ⇒ Object

Returns a hash based on the string-values of an array.



166
167
168
169
170
171
172
173
# File 'lib/knj/arrayext.rb', line 166

def self.array_hash(arr)
  hashes = []
  arr.each do |ele|
    hashes << Digest::MD5.hexdigest(ele.to_s)
  end
  
  return Digest::MD5.hexdigest(hashes.join("_"))
end

.array_to_hash(arr) ⇒ Object

Converts an array to a hash with the index a string-numbers.



48
49
50
51
52
53
54
55
# File 'lib/knj/arrayext.rb', line 48

def self.array_to_hash(arr)
  ret = {}
  arr.each do |item|
    ret[ret.length.to_s] = item
  end
  
  return ret
end

.clone_encode(hash, encoding, args = {}) ⇒ Object

Forces all strings in an array or a hash to be encoded to a specific encoding recursively.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/knj/arrayext.rb', line 197

def self.clone_encode(hash, encoding, args = {})
  return hash if !hash
  
  hash = hash.clone
  Knj::Php.foreach(hash) do |key, val|
    if val.is_a?(String)
      begin
        hash[key] = Knj::Php.utf8_encode(encoding)
      rescue Encoding::UndefinedConversionError => e
        if args["ignore_encoding_errors"]
          next
        else
          raise e
        end
      end
    elsif val.is_a?(Array) or val.is_a?(Hash)
      hash[key] = Knj::ArrayExt.clone_encode(val, encoding, args)
    end
  end
  
  return hash
end

.dict(arr) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/knj/arrayext.rb', line 89

def self.dict(arr)
  ret = Dictionary.new
  arr.each do |item|
    ret[ret.length.to_s] = item
  end
  
  return ret
end

.hash_diff?(h1, h2, args = {}) ⇒ Boolean

Compares the keys and values of two hashes and returns true if they are different.

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/knj/arrayext.rb', line 113

def self.hash_diff?(h1, h2, args = {})
  if !args.key?("h1_to_h2") or args["h1_to_h2"]
    h1.each do |key, val|
      return true if !h2.key?(key)
      
      hash_val = h2[key].to_s
      hash_val = hash_val.force_encoding("UTF-8") if hash_val.respond_to?(:force_encoding)
      
      val = val.to_s
      val = val.force_encoding("UTF-8") if val.respond_to?(:force_encoding)
      
      return true if hash_val != val
    end
  end
  
  if !args.key?("h2_to_h1") or args["h2_to_h1"]
    h2.each do |key, val|
      return true if !h1.key?(key)
      
      hash_val = h1[key].to_s
      hash_val = hash_val.force_encoding("UTF-8") if hash_val.respond_to?(:force_encoding)
      
      val = val.to_s
      val = val.force_encoding("UTF-8") if val.respond_to?(:force_encoding)
      
      return true if hash_val != val
    end
  end
  
  return false
end

.hash_keys_hash(hash) ⇒ Object

Returns a hash based on the string-keys in a hash.



146
147
148
149
150
151
152
153
# File 'lib/knj/arrayext.rb', line 146

def self.hash_keys_hash(hash)
  hashes = []
  hash.keys.sort.each do |key|
    hashes << Digest::MD5.hexdigest(key.to_s)
  end
  
  return Digest::MD5.hexdigest(hashes.join("_"))
end

.hash_md5(hash) ⇒ Object

Returns a hash based on the keys and values of a hash.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/knj/arrayext.rb', line 99

def self.hash_md5(hash)
  combined_val = ""
  hash.each do |key, val|
    if combined_val.length > 0
      combined_val << ";"
    end
    
    combined_val << "#{key}:#{val}"
  end
  
  return Digest::MD5.hexdigest(combined_val)
end

.hash_numeric_keys?(hash) ⇒ Boolean

Returns true if all keys in a hash are numeric objects.

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/knj/arrayext.rb', line 58

def self.hash_numeric_keys?(hash)
  all_num = true
  hash.each do |key, val|
    if !Knj::Php.is_numeric(key)
      all_num = false
      break
    end
  end
  
  return all_num
end

.hash_sort(hash, &block) ⇒ Object

Sorts the hash without being a retard…



185
186
187
188
189
190
191
192
193
194
# File 'lib/knj/arrayext.rb', line 185

def self.hash_sort(hash, &block)
  sorted = hash.sort(&block)
  
  ret = {}
  sorted.each do |ele|
    ret[ele[0]] = ele[1]
  end
  
  return ret
end

.hash_sym(hash_given) ⇒ Object

Converts all keys in the given hash to symbols.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/knj/arrayext.rb', line 71

def self.hash_sym(hash_given)
  raise "Invalid argument-class: '#{hash_given.class.name}'." if !hash_given or !hash_given.respond_to?(:each)
  
  adds = {}
  hash_given.each do |key, value|
    if !key.is_a?(Symbol)
      adds[key.to_sym] = value
      hash_given.delete(key)
    end
  end
  
  adds.each do |key, value|
    hash_given[key] = value
  end
  
  return hash_given
end

.hash_values_hash(hash) ⇒ Object

Returns hash based on the string-values in a hash.



156
157
158
159
160
161
162
163
# File 'lib/knj/arrayext.rb', line 156

def self.hash_values_hash(hash)
  hashes = []
  hash.keys.sort.each do |key|
    hashes << Digest::MD5.hexdigest(hash[key].to_s)
  end
  
  return Digest::MD5.hexdigest(hashes.join("_"))
end

.join(args = {}, key = nil, sep = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/knj/arrayext.rb', line 2

def self.join(args = {}, key = nil, sep = nil)
  if args.is_a?(Array) and sep
    args = {
      :arr => args,
      :sep => sep,
      :key => key
    }
  end
  
  raise "No seperator given." if !args[:sep]
  
  str = ""
  first = true
  
  Knj::Php.foreach(args[:arr]) do |key, value|
    if first
      first = false
    else
      str << args[:sep]
    end
    
    if args[:key]
      value = key
    else
      value = value
    end
    
    if args[:callback]
      if args[:callback].is_a?(Proc) or args[:callback].is_a?(Method)
        value = args[:callback].call(value)
      else
        value = Knj::Php.call_user_func(args[:callback], value) if args[:callback]
      end
    end
    
    raise "Callback returned nothing." if args[:callback] and !value
    
    str << args[:surr] if args[:surr]
    str << value.to_s
    str << args[:surr] if args[:surr]
  end
  
  return str
end

.validate_hash(h, args) ⇒ Object

Validates a hash of data.



176
177
178
179
180
181
182
# File 'lib/knj/arrayext.rb', line 176

def self.validate_hash(h, args)
  h.each do |key, val|
    if args.key?(:not_empty) and args[:not_empty].index(key) != nil and val.to_s.strip.length <= 0
      raise Knj::Errors::InvalidData, sprintf(args[:not_empty_error], key)
    end
  end
end