Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/docs/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#only_presentedObject



36
37
38
# File 'lib/docs/core_ext.rb', line 36

def only_presented
  select {|_, v| v.present? }
end

#smart_append_to(key, value, delimeter = ' ') ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/docs/core_ext.rb', line 2

def smart_append_to(key, value, delimeter=' ')
  if self[key].nil?
    self[key] ||= value
  else
    self[key] << delimeter if self[key].is_a?(String)
    self[key] << value
  end
end

#smart_merge(other) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/docs/core_ext.rb', line 11

def smart_merge(other)
  return self unless other.is_a? Hash and other.any?
  return other unless self.any?
  
  self.merge other do |key, left, right| 
    case left
      when Hash
        right.is_a?(Hash) ? left.smart_merge(right) : right
      when Array
        (left | [right]).flatten
      when String
        right.is_a?(Array) ? ([left]|right).flatten : right
      else right
    end
  end
end

#smart_merge!(other_hash) ⇒ Object



28
29
30
# File 'lib/docs/core_ext.rb', line 28

def smart_merge!(other_hash)
  self.replace(empty? ? other_hash : smart_merge(other_hash))
end

#smart_update(other_hash) ⇒ Object



32
33
34
# File 'lib/docs/core_ext.rb', line 32

def smart_update(other_hash)
  self.replace(other_hash.smart_merge(self))
end