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
16
17
18
19
20
21
22
23
# File 'lib/scout/indiferent_hash/options.rb', line 2

def self.add_defaults(options, defaults = {})
  options ||= {}
  IndiferentHash.setup(options)
  case
  when Hash === options
    new_options = options.dup
  when String === options
    new_options = string2hash options
  else
    raise "Format of '#{options.inspect}' not understood. It should be a hash"
  end

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

    new_options[key] = value 
  end

  new_options

  options.replace new_options
end

.array2hash(array, default = nil) ⇒ Object



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

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



93
94
95
96
97
98
99
# File 'lib/scout/indiferent_hash/options.rb', line 93

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/scout/indiferent_hash/options.rb', line 66

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/scout/indiferent_hash/options.rb', line 25

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

  defaults = keys.pop if Hash === keys.last
  hahs = 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



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

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

.pull_keys(hash, prefix) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/scout/indiferent_hash/options.rb', line 38

def self.pull_keys(hash, prefix)
  new = {}
  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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/scout/indiferent_hash/options.rb', line 101

def self.string2hash(string)
  options = {}

  string.split('#').each do |str|
    key, sep, 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 and next 

    options[key] = begin
                     saved_safe = $SAFE
                     $SAFE = 0
                     eval(value)
                   rescue Exception
                     value
                   ensure
                     $SAFE = saved_safe
                   end
  end

  IndiferentHash.setup(options)
end

.zip2hash(list1, list2) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/scout/indiferent_hash/options.rb', line 58

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



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

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
# File 'lib/scout/indiferent_hash.rb', line 58

def delete(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

#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

#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