Module: IndiferentHash

Defined in:
lib/scout/indiferent_hash.rb,
lib/scout/indiferent_hash/options.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_defaults(options, defaults = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/scout/indiferent_hash/options.rb', line 2

def self.add_defaults(options, defaults = {})
  options = string2hash options if String === options
  IndiferentHash.setup(options)

  defaults = string2hash defaults if String === defaults

  defaults.each do |key, value|
    next if options.include?(key)

    options[key] = value 
  end

  options
end

.array2hash(array, default = nil) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/scout/indiferent_hash/options.rb', line 73

def self.array2hash(array, default = nil)
  hash = {}
  array.each do |key, value|
    value = default.dup if value.nil? and not default.nil?
    hash[key] = value
  end
  IndiferentHash.setup(hash)
end

.hash2string(hash) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/scout/indiferent_hash/options.rb', line 87

def self.hash2string(hash)
  hash.sort_by{|k,v| k.to_s}.collect{|k,v| 
    next unless %w(Symbol String Float Fixnum Integer Numeric TrueClass FalseClass Module Class Object).include? v.class.to_s
    [ Symbol === k ? ":" << k.to_s : k.to_s.chomp,
      Symbol === v ? ":" << v.to_s : v.to_s.chomp] * "="
  }.compact * "#"
end

.positional2hash(keys, *values) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/scout/indiferent_hash/options.rb', line 60

def self.positional2hash(keys, *values)
  if Hash === values.last
    extra = values.pop
    inputs = IndiferentHash.zip2hash(keys, values)
    inputs.delete_if{|k,v| v.nil? or (String === v and v.empty?)}
    inputs = IndiferentHash.add_defaults inputs, extra
    inputs.delete_if{|k,v| not keys.include?(k) and not (Symbol === k ? keys.include?(k.to_s) : keys.include?(k.to_sym))}
    inputs
  else
    IndiferentHash.zip2hash(keys, values)
  end
end

.process_options(hash, *keys) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/scout/indiferent_hash/options.rb', line 17

def self.process_options(hash, *keys)
  IndiferentHash.setup(hash)

  defaults = keys.pop if Hash === keys.last
  hash = IndiferentHash.add_defaults hash, defaults if defaults

  if keys.length == 1
    hash.include?(keys.first.to_sym) ? hash.delete(keys.first.to_sym) : hash.delete(keys.first.to_s) 
  else
    keys.collect do |key| hash.include?(key.to_sym) ? hash.delete(key.to_sym) : hash.delete(key.to_s) end
  end
end

.process_to_hash(list) ⇒ Object



82
83
84
85
# File 'lib/scout/indiferent_hash/options.rb', line 82

def self.process_to_hash(list)
  result = yield list
  zip2hash(list, result)
end

.pull_keys(hash, prefix) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/scout/indiferent_hash/options.rb', line 30

def self.pull_keys(hash, prefix)
  IndiferentHash.setup(hash)
  new = hash.include?("#{prefix}_options") ? hash.delete("#{prefix}_options") : {}
  prefix = prefix.to_s
  hash.keys.each do |key|
    if key.to_s =~ /#{ prefix }_(.*)/
      case
      when String === key
        new[$1] = hash.delete key
      when Symbol === key
        new[$1.to_sym] = hash.delete key
      end
    else
      if key.to_s == prefix.to_s
        new[key] = hash.delete key
      end
    end
  end

  IndiferentHash.setup(new)
end

.setup(hash) ⇒ Object



6
7
8
9
# File 'lib/scout/indiferent_hash.rb', line 6

def self.setup(hash)
  hash.extend IndiferentHash 
  hash
end

.string2hash(string) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/scout/indiferent_hash/options.rb', line 95

def self.string2hash(string)
  options = {}

  string.split('#').each do |str|
    key, _, value = str.partition "="

    key = key[1..-1].to_sym if key[0] == ":"

    options[key] = true and next if value.empty?
    options[key] = value[1..-1].to_sym and next if value[0] == ":"
    options[key] = Regexp.new(/#{value[1..-2]}/) and next if value[0] == "/" and value[-1] == "/"
    options[key] = value[1..-2] and next if value =~ /^['"].*['"]$/
    options[key] = value.to_i and next if value =~ /^\d+$/
    options[key] = value.to_f and next if value =~ /^\d*\.\d+$/
    options[key] = true and next if value == "true"
    options[key] = false and next if value == "false"
    options[key] = value
  end

  IndiferentHash.setup(options)
end

.zip2hash(list1, list2) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/scout/indiferent_hash/options.rb', line 52

def self.zip2hash(list1, list2)
  hash = {}
  list1.each_with_index do |e,i|
    hash[e] = list2[i]
  end
  IndiferentHash.setup(hash)
end

Instance Method Details

#[](key) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scout/indiferent_hash.rb', line 29

def [](key)
  res = super(key) 
  return res unless res.nil? or (_default? and not keys.include? key)

  case key
  when Symbol, Module
    super(key.to_s)
  when String
    super(key.to_sym)
  else
    res
  end
end

#[]=(key, value) ⇒ Object



20
21
22
23
# File 'lib/scout/indiferent_hash.rb', line 20

def []=(key,value)
  delete(key)
  super(key,value)
end

#_default?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/scout/indiferent_hash.rb', line 25

def _default?
  @_default ||= self.default or self.default_proc
end

#clean_versionObject



71
72
73
74
75
76
77
# File 'lib/scout/indiferent_hash.rb', line 71

def clean_version
  clean = {}
  each do |k,v|
    clean[k.to_s] = v unless clean.include? k.to_s
  end
  clean
end

#delete(key) ⇒ Object



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

def delete(key)
  case key
  when Symbol, Module
    v = super(key) 
    v.nil? ? super(key.to_s) : v
  when String
    v = super(key)
    v.nil? ? super(key.to_sym) : v
  else
    super(key)
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
# File 'lib/scout/indiferent_hash.rb', line 47

def include?(key)
  case key
  when Symbol, Module
    super(key) || super(key.to_s)
  when String
    super(key) || super(key.to_sym)
  else
    super(key)
  end
end

#merge(other) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/scout/indiferent_hash.rb', line 11

def merge(other)
  new = self.dup
  IndiferentHash.setup(new)
  other.each do |k,value|
    new[k] = value
  end
  new
end

#slice(*list) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/scout/indiferent_hash.rb', line 79

def slice(*list)
  ext_list = []
  list.each do |e|
    case e
    when Symbol
      ext_list << e
      ext_list << e.to_s
    when String
      ext_list << e
      ext_list << e.to_sym
    else
      ext_list << e
    end
  end
  IndiferentHash.setup(super(*ext_list))
end

#values_at(*key_list) ⇒ Object



43
44
45
# File 'lib/scout/indiferent_hash.rb', line 43

def values_at(*key_list)
  key_list.inject([]){|acc,key| acc << self[key]}
end