Sha256: 9d10f72c150d3fdb8497f16e7445d232711b2d506d5ccde4dbae9f20b445f91d

Contents?: true

Size: 678 Bytes

Versions: 1

Compression:

Stored size: 678 Bytes

Contents

require "json"

module Thirtythirty

  def marshal(*attributes)
    extend ClassMethods
    send :include, InstanceMethods
    @marshalled_attributes = attributes
  end

  module ClassMethods

    attr_reader :marshalled_attributes

  protected

    def _load(dumped)
      data = JSON.parse(dumped)
      obj = new
      marshalled_attributes.each do |attr|
        obj.send(:"#{attr}=", Marshal.load(data[attr.to_s]))
      end
      obj
    end

  end

  module InstanceMethods

    def _dump(*args)
      self.class.marshalled_attributes.inject({}) do |hash, attr|
        hash[attr] = Marshal.dump(self.send(attr.to_sym))
        hash
      end.to_json
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thirtythirty-0.0.1 lib/thirtythirty.rb