Class: Coral::Util::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/coral_core/util/data.rb

Class Method Summary collapse

Class Method Details

.array(data, default = [], split_string = false) ⇒ Object




180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/coral_core/util/data.rb', line 180

def self.array(data, default = [], split_string = false)
  result = default    
  if data
    case data
    when Array
      result = data
    when String
      result = [ ( split_string ? data.split(/\s*,\s*/) : data ) ]
    else
      result = [ data ]
    end
  end
  return result
end

.empty?(value) ⇒ Boolean


Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/coral_core/util/data.rb', line 40

def self.empty?(value)
  if undef?(value) || false?(value) || (value.respond_to?('empty?') && value.empty?)
    return true
  end
  return false
end

.ensure(test, success_value = nil, failure_value = nil) ⇒ Object




370
371
372
373
374
375
376
377
378
379
380
# File 'lib/coral_core/util/data.rb', line 370

def self.ensure(test, success_value = nil, failure_value = nil)
  success_value = (success_value ? success_value : test)
  failure_value = (failure_value ? failure_value : nil)
    
  if empty?(test)
    value = failure_value
  else
    value = success_value
  end
  return value
end

.ensure_value(value, failure_value = nil) ⇒ Object




384
385
386
# File 'lib/coral_core/util/data.rb', line 384

def self.ensure_value(value, failure_value = nil)
  return self.ensure(value, nil, failure_value)
end

.exists?(data, keys, check_empty = false) ⇒ Boolean


Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'lib/coral_core/util/data.rb', line 49

def self.exists?(data, keys, check_empty = false)
  keys = [ keys ] unless keys.is_a?(Array)
  keys.each do |key|
    return false unless data.is_a?(Hash) && data.has_key?(key)
    return false if check_empty && empty?(data[key])
    data = data[key]
  end
  return true
end

.false?(value) ⇒ Boolean


Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/coral_core/util/data.rb', line 30

def self.false?(value)
  if value == false || 
    (value.is_a?(String) && value.match(/^\s*(false|FALSE|False)\s*$/))
    return true
  end
  return false  
end

.filter(data, method = false) ⇒ Object




170
171
172
173
174
175
176
# File 'lib/coral_core/util/data.rb', line 170

def self.filter(data, method = false)
  if method && method.is_a?(Symbol) && 
    [ :array, :hash, :string, :symbol, :test ].include?(method.to_sym)
    return send(method, data)
  end
  return data
end

.hash(data, default = {}) ⇒ Object




197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/coral_core/util/data.rb', line 197

def self.hash(data, default = {})
  result = default    
  if data
    case data
    when Hash
      result = data
    else
      result = {}
    end
  end
  return result
end

.interpolate(value, scope, options = {}) ⇒ Object




296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/coral_core/util/data.rb', line 296

def self.interpolate(value, scope, options = {})    
  
  pattern = ( options.has_key?(:pattern) ? options[:pattern] : '\$(\{)?([a-zA-Z0-9\_\-]+)(\})?' )
  group   = ( options.has_key?(:var_group) ? options[:var_group] : 2 )
  flags   = ( options.has_key?(:flags) ? options[:flags] : '' )
  
  if scope.is_a?(Hash)
    regexp = Regexp.new(pattern, flags.split(''))
  
    replace = lambda do |item|
      matches = item.match(regexp)
      result  = nil
      
      #dbg(item, 'item')
      #dbg(matches, 'matches')
      
      unless matches.nil?
        replacement = scope.search(matches[group], options)
        result      = item.gsub(matches[0], replacement) unless replacement.nil?
      end
      return result
    end
    
    case value
    when String
      #dbg(value, 'interpolate (string) -> init')
      while (temp = replace.call(value))
        #dbg(temp, 'interpolate (string) -> replacement')
        value = temp
      end
      
    when Hash
      #dbg(value, 'interpolate (hash) -> init')
      value.each do |key, data|
        #dbg(data, "interpolate (#{key}) -> data")
        value[key] = interpolate(data, scope, options)
      end
    end
  end
  #dbg(value, 'interpolate -> result')
  return value  
end

.merge(data, force = true) ⇒ Object


Operations



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/coral_core/util/data.rb', line 252

def self.merge(data, force = true)
  value = data
  
  # Special case because this method is called from within Config.new so we 
  # can not use Config.ensure, as that would cause an infinite loop.
  force = force.is_a?(Coral::Config) ? force.get(:force, true) : force
  
  if data.is_a?(Array)
    value = undef?(data[0]) ? nil : data.shift.clone
    
    data.each do |item|
      item = undef?(item) ? nil : item.clone
      
      case value
      when Hash
        begin
          require 'deep_merge'
          value = force ? value.deep_merge!(item) : value.deep_merge(item)
          
        rescue LoadError
          if item.is_a?(Hash) # Non recursive top level by default.
            value = value.merge(item)              
          elsif force
            value = item
          end
        end  
      when Array
        if item.is_a?(Array)
          value = value.concat(item).uniq
        elsif force
          value = item
        end
              
      else
        value = item if force || item.is_a?(String) || item.is_a?(Symbol)
      end
    end  
  end
  
  return value
end

.parse_json(json_text) ⇒ Object




96
97
98
99
100
101
102
103
104
# File 'lib/coral_core/util/data.rb', line 96

def self.parse_json(json_text)
  output = ''
  begin
    output = MultiJson.load(json_text)
    
  rescue Exception
  end
  return output
end

.parse_yaml(yaml_text) ⇒ Object




120
121
122
123
124
125
126
127
128
# File 'lib/coral_core/util/data.rb', line 120

def self.parse_yaml(yaml_text)
  output = ''
  begin
    output = YAML.load(yaml_text)
    
  rescue Exception
  end
  return output  
end

.prefix(prefix, data) ⇒ Object


Utilities



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/coral_core/util/data.rb', line 342

def self.prefix(prefix, data)
  result = nil
  
  unless prefix.is_a?(String) && ! empty?(prefix)
    prefix = ''
  end
  
  case data
  when String, Symbol
    result = ( prefix.empty? ? data.to_s : prefix + '_' + data.to_s )
    
  when Array
    result = []
    data.each do |value|
      result << prefix(prefix, value)
    end
    
  when Hash
    result = {}
    data.each do |key, value|
      result[prefix(prefix, key)] = value  
    end      
  end
  return result
end

.string(data, default = '') ⇒ Object




212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/coral_core/util/data.rb', line 212

def self.string(data, default = '')
  result = default    
  if data
    case data
    when String
      result = data
    else
      result = data.to_s
    end
  end
  return result
end

.string_map(data) ⇒ Object




79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/coral_core/util/data.rb', line 79

def self.string_map(data)
  results = {}
  return data unless data
  
  case data
  when Hash
    data.each do |key, value|
      results[key.to_s] = string_map(value)
    end
  else
    results = data
  end    
  return results
end

.symbol(data, default = :undefined) ⇒ Object




227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/coral_core/util/data.rb', line 227

def self.symbol(data, default = :undefined)
  result = default    
  if data
    case data
    when Symbol
      result = data
    when String
      result = data.to_sym
    else
      result = data.class.to_sym
    end
  end
  return result
end

.symbol_map(data) ⇒ Object


Translation



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/coral_core/util/data.rb', line 62

def self.symbol_map(data)
  results = {}
  return data unless data
  
  case data
  when Hash
    data.each do |key, value|
      results[key.to_sym] = symbol_map(value)
    end
  else
    results = data
  end    
  return results
end

.test(data) ⇒ Object




244
245
246
247
# File 'lib/coral_core/util/data.rb', line 244

def self.test(data)
  return false if Util::Data.empty?(data)
  return true
end

.to_json(data, pretty = true) ⇒ Object




108
109
110
111
112
113
114
115
116
# File 'lib/coral_core/util/data.rb', line 108

def self.to_json(data, pretty = true)
  output = ''
  begin
    output = MultiJson.dump(data, :pretty => pretty)
    
  rescue Exception
  end
  return output
end

.to_yaml(data) ⇒ Object




132
133
134
135
136
137
138
139
140
# File 'lib/coral_core/util/data.rb', line 132

def self.to_yaml(data)
  output = ''
  begin
    output = YAML.dump(data)
    
  rescue Exception
  end
  return output
end

.true?(value) ⇒ Boolean


Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/coral_core/util/data.rb', line 20

def self.true?(value)
  if value == true || 
    (value.is_a?(String) && value.match(/^\s*(true|TRUE|True)\s*$/))
    return true
  end
  return false  
end

.undef?(value) ⇒ Boolean


Type checking

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
# File 'lib/coral_core/util/data.rb', line 9

def self.undef?(value)
  if value.nil? || 
    (value.is_a?(Symbol) && value == :undef || value == :undefined) || 
    (value.is_a?(String) && value.match(/^\s*(undef|UNDEF|Undef|nil|NIL|Nil)\s*$/))
    return true
  end
  return false  
end

.value(value) ⇒ Object




144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/coral_core/util/data.rb', line 144

def self.value(value)
  case value
  when String
    if undef?(value)
      value = nil
    elsif true?(value)
      value = true
    elsif false?(value)
      value = false
    end
  
  when Array
    value.each_with_index do |item, index|
      value[index] = value(item)
    end
  
  when Hash
    value.each do |key, data|
      value[key] = value(data)
    end
  end
  return value  
end