Sha256: d087b024a43bd15330b91b9280c99a38a080251e0868313b02cc4afe837f1b58

Contents?: true

Size: 612 Bytes

Versions: 4

Compression:

Stored size: 612 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

4 entries across 4 versions & 1 rubygems

Version Path
esendex-0.4.0 lib/esendex/hash_serialisation.rb
esendex-0.3.2 lib/esendex/hash_serialisation.rb
esendex-0.3.1 lib/esendex/hash_serialisation.rb
esendex-0.3.0 lib/esendex/hash_serialisation.rb