module CKick::Hashable

mixin enabling hash serialization

Public Instance Methods

to_hash() click to toggle source

transforms object to Hash such that { :instance_variable => value }

# File lib/ckick/hashable.rb, line 14
def to_hash
  a = {}
  instance_variables_as_key_values.each do |name, obj|
    a[name] = object_value(obj)
  end
  a
end
to_no_empty_value_hash() click to toggle source

transforms object to Hash such that { :instance_variable => value }, excluding any pair where value responds true to :empty? method

# File lib/ckick/hashable.rb, line 23
def to_no_empty_value_hash
  a = {}
  instance_variables_as_key_values.each do |name, obj|
    if !obj.respond_to?(:empty?) || (obj.respond_to?(:empty?) && !obj.empty?)
      a[name] = object_value(obj)
    end
  end
  a
end

Private Instance Methods

instance_variables_as_key_values() click to toggle source
# File lib/ckick/hashable.rb, line 35
def instance_variables_as_key_values
  instance_variables.collect do |att|
    [att[1..-1].to_sym, instance_variable_get(att.to_sym)]
  end
end
object_value(obj) click to toggle source
# File lib/ckick/hashable.rb, line 41
def object_value(obj)
  if obj.respond_to?(:to_hash)
    return obj.to_hash
  else
    return obj.to_hash_element
  end
end