Class: DTK::Common::PrettyPrintHash

Inherits:
SimpleOrderedHash show all
Defined in:
lib/hash_object.rb

Instance Method Summary collapse

Methods inherited from SimpleOrderedHash

#initialize, #set_unless_nil

Constructor Details

This class inherits a constructor from DTK::Common::SimpleOrderedHash

Instance Method Details

#add(model_object, *keys, &block) ⇒ Object

field with ‘?’ suffix means optioanlly add depending on whether name present and non-null in source if block is given then apply to source rather than returning just source



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hash_object.rb', line 47

def add(model_object,*keys,&block)
  keys.each do |key|
    #if marked as optional skip if not present
    if key.to_s =~ /(^.+)\?$/
      key = $1.to_sym
      next unless model_object[key]
    end
    #special treatment of :id
    val = (key == :id ? model_object.id : model_object[key]) 
    self[key] = (block ? block.call(val) : val)
  end
  self
end

#slice(*keys) ⇒ Object



61
62
63
# File 'lib/hash_object.rb', line 61

def slice(*keys)
  keys.inject(self.class.new){|h,k|h.merge(k => self[k])}
end