Sha256: 082c67be06e5e292d0705e7b916f560a09c0c6afd3acee92d50ed5c7539760fc

Contents?: true

Size: 635 Bytes

Versions: 2

Compression:

Stored size: 635 Bytes

Contents

# Handles serialisation and deserialisation of object as a hash
# 
module Esendex
  module HashSerialisation
    def initialize(attrs={})
      attrs.each_pair do |k, v|
        raise ArgumentError.new("#{k} not an attribute of #{self.class.name}") unless respond_to?("#{k}=")
        send("#{k}=", v)
      end
    end

    def to_hash
      attrs = {}
      public_methods
        .select { |m| m =~ /\w\=$/ }
        .select { |m| respond_to?(m.to_s.chop) }
        .collect { |m| m.to_s.chop.to_sym }
        .each do |method| 
          attrs[method] = send(method)
        end
      attrs
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
esendex-0.6.0 lib/esendex/hash_serialisation.rb
esendex-0.5.0 lib/esendex/hash_serialisation.rb