Class: Hash

Inherits:
Object show all
Defined in:
lib/ultrasphinx/core_extensions.rb

Overview

class HashWithIndifferentAccess

# Returns a regular Hash with all string keys. Much faster
# than HWIA#merge.
def _fast_merge(right)
  left = Hash[self]
  left.merge!(self.class.new(right))
end

end

Instance Method Summary collapse

Instance Method Details

#_coerce_basic_typesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ultrasphinx/core_extensions.rb', line 40

def _coerce_basic_types
  # XXX To remove
  Hash[*self.map do |key, value|
    [key.to_s,
      if value.respond_to?(:to_i) && value.to_i.to_s == value
        value.to_i
      elsif value == ""
        nil
      else
        value
      end]
    end._flatten_once]
end

#_delete(*args) ⇒ Object

Delete by multiple keys



64
65
66
67
68
# File 'lib/ultrasphinx/core_extensions.rb', line 64

def _delete(*args)
  args.map do |key|
    self.delete key
  end    
end

#_stringify_all!(*skip) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ultrasphinx/core_extensions.rb', line 54

def _stringify_all!(*skip)
  # Stringifies all keys, and stringifies all values except those slotted for keys in 'skip'
  stringify_keys!
  self.except(*skip).each do |key, value|
    self[key] = value.to_s
  end
  self
end

#_to_conf_string(section = nil) ⇒ Object

Convert a hash to a Sphinx-style conf string



71
72
73
74
75
76
# File 'lib/ultrasphinx/core_extensions.rb', line 71

def _to_conf_string(section = nil)
  inner = self.map do |key, value|
    "  #{key} = #{value}"
  end.join("\n")
  section ? "#{section} {\n#{inner}\n}\n" : inner
end

#except(*keys) ⇒ Object

Rails 1.2.6 compatibility



80
81
82
83
# File 'lib/ultrasphinx/core_extensions.rb', line 80

def except(*keys)
  rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
  reject { |key,| rejected.include?(key) }
end

#except!(*keys) ⇒ Object



84
85
86
# File 'lib/ultrasphinx/core_extensions.rb', line 84

def except!(*keys)
  replace(except(*keys))
end