Module: RhGet

Included in:
Array, Hash
Defined in:
lib/rh.rb

Overview

Module to implement common function for Hash and Array class

Instance Method Summary collapse

Instance Method Details

#_key_options(opts) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/rh.rb', line 219

def _key_options(opts)
  empty = false
  empty = opts.include?('e') if opts
  one = false
  one = opts.include?('0') if opts
  [empty, one]
end

#_key_to_s(k) ⇒ Object



236
237
238
239
# File 'lib/rh.rb', line 236

def _key_to_s(k)
  return ':' + k.to_s if k.is_a?(Symbol)
  k
end

#_loop_on_regs(regs, key) ⇒ Object



227
228
229
230
231
232
233
234
# File 'lib/rh.rb', line 227

def _loop_on_regs(regs, key)
  regs.each do |r|
    init = r[1]
    reg = r[0].match(key)
    return [Regexp.new(reg[1]), init, reg[2]] if reg && reg[1]
  end
  [nil, nil, nil]
end

#_regexp(key) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rh.rb', line 207

def _regexp(key)
  return [key, [], nil] if key.is_a?(Regexp)
  return [nil, nil, nil] unless key.is_a?(String)

  regs = []
  regs << [%r{^/(.*)/([e0])*$},     []]
  regs << [%r{^\[/(.*)/([e0])*\]$}, []]
  regs << [%r{^\{/(.*)/([e0])*\}$}, {}]

  _loop_on_regs(regs, key)
end

#_update_res(res, k, v, one) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'lib/rh.rb', line 241

def _update_res(res, k, v, one)
  res << v   if res.is_a?(Array)
  return unless res.is_a?(Hash)
  if one && v.is_a?(Array) && v.length == 1
    res[k] = v[0]
  else
    res[k] = v
  end
end