Class: Hash

Inherits:
Object show all
Defined in:
lib/plotrb/base.rb

Overview

monkey patch Hash class to support reverse_merge and collect_attributes

Instance Method Summary collapse

Instance Method Details

#attribute_post_processingObject



155
156
157
# File 'lib/plotrb/base.rb', line 155

def attribute_post_processing
  # nothing to do for Hash
end

#classify(name, format = nil) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/plotrb/base.rb', line 180

def classify(name, format=nil)
  klass = name.to_s.split('_').collect(&:capitalize).join
  if format == :json
    klass[0].downcase + klass[1..-1]
  else
    klass
  end
end

#collect_attributesObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/plotrb/base.rb', line 163

def collect_attributes
  collected = {}
  self.each do |k, v|
    json_attr = classify(k, :json)
    if v.respond_to?(:collect_attributes)
      collected[json_attr] = v.collect_attributes
    elsif v.is_a?(Array)
      collected[json_attr] = [].concat(v.collect{ |va|
        va.respond_to?(:collect_attributes) ? va.collect_attributes : va
      })
    else
      collected[json_attr] = v
    end
  end
  collected
end

#reverse_merge(other_hash) ⇒ Object



159
160
161
# File 'lib/plotrb/base.rb', line 159

def reverse_merge(other_hash)
  other_hash.merge(self)
end