File: marshable.rb

package info (click to toggle)
ruby-icalendar 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 492 kB
  • sloc: ruby: 2,868; makefile: 5
file content (34 lines) | stat: -rw-r--r-- 790 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Icalendar
  module Marshable
    def self.included(base)
      base.extend ClassMethods
    end

    def marshal_dump
      instance_variables
        .reject { |ivar| self.class.transient_variables.include?(ivar) }
        .each_with_object({}) do |ivar, serialized|

        serialized[ivar] = instance_variable_get(ivar)
      end
    end

    def marshal_load(serialized)
      serialized.each do |ivar, value|
        unless self.class.transient_variables.include?(ivar)
          instance_variable_set(ivar, value)
        end
      end
    end

    module ClassMethods
      def transient_variables
        @transient_variables ||= [:@transient_variables]
      end

      def transient_variable(name)
        transient_variables.push(name.to_sym)
      end
    end
  end
end