Blame view

lib/syspro/syspro_object.rb 1.89 KB
db76748d   Isaac Lewis   cop a bunch of St...
1
2
  module Syspro
    class SysproObject
3d0157a5   Isaac Lewis   add logoff
3
      include Enumerable
db76748d   Isaac Lewis   cop a bunch of St...
4
  
3d0157a5   Isaac Lewis   add logoff
5
      def initialize(id = nil, opts = {})
db76748d   Isaac Lewis   cop a bunch of St...
6
        @opts = Util.normalize_opts(opts)
3d0157a5   Isaac Lewis   add logoff
7
      end
db76748d   Isaac Lewis   cop a bunch of St...
8
  
3d0157a5   Isaac Lewis   add logoff
9
10
11
12
13
14
      # Determines the equality of two Syspro objects. Syspro objects are
      # considered to be equal if they have the same set of values and each one
      # of those values is the same.
      def ==(other)
        other.is_a?(SysproObject) && @values == other.instance_variable_get(:@values)
      end
db76748d   Isaac Lewis   cop a bunch of St...
15
  
3d0157a5   Isaac Lewis   add logoff
16
17
18
      def to_s(*_args)
        JSON.pretty_generate(to_hash)
      end
db76748d   Isaac Lewis   cop a bunch of St...
19
  
3d0157a5   Isaac Lewis   add logoff
20
21
22
23
      def inspect
        id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : ""
        "#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
      end
db76748d   Isaac Lewis   cop a bunch of St...
24
  
3d0157a5   Isaac Lewis   add logoff
25
26
      def keys
        @values.keys
db76748d   Isaac Lewis   cop a bunch of St...
27
28
      end
  
3d0157a5   Isaac Lewis   add logoff
29
30
31
      def values
        @values.values
      end
db76748d   Isaac Lewis   cop a bunch of St...
32
  
3d0157a5   Isaac Lewis   add logoff
33
34
35
36
      def to_hash
        maybe_to_hash = lambda do |value|
          value && value.respond_to?(:to_hash) ? value.to_hash : value
        end
db76748d   Isaac Lewis   cop a bunch of St...
37
  
3d0157a5   Isaac Lewis   add logoff
38
39
40
41
42
43
44
        @values.each_with_object({}) do |(key, value), acc|
          acc[key] = case value
                     when Array
                       value.map(&maybe_to_hash)
                     else
                       maybe_to_hash.call(value)
                     end
db76748d   Isaac Lewis   cop a bunch of St...
45
46
47
        end
      end
  
3d0157a5   Isaac Lewis   add logoff
48
49
50
      def each(&blk)
        @values.each(&blk)
      end
db76748d   Isaac Lewis   cop a bunch of St...
51
  
3d0157a5   Isaac Lewis   add logoff
52
53
54
55
56
57
58
59
60
61
62
63
      private
  
      # Produces a deep copy of the given object including support for arrays,
      # hashes, and SysproObject.
      def self.deep_copy(obj)
        case obj
        when Array
          obj.map { |e| deep_copy(e) }
        when Hash
          obj.each_with_object({}) do |(k, v), copy|
            copy[k] = deep_copy(v)
            copy
db76748d   Isaac Lewis   cop a bunch of St...
64
          end
3d0157a5   Isaac Lewis   add logoff
65
66
67
68
69
70
71
72
73
        when SysproObject
          obj.class.construct_from(
            deep_copy(obj.instance_variable_get(:@values)),
            obj.instance_variable_get(:@opts).select do |k, _v|
              Util::OPTS_COPYABLE.include?(k)
            end
          )
        else
          obj
db76748d   Isaac Lewis   cop a bunch of St...
74
75
        end
      end
3d0157a5   Isaac Lewis   add logoff
76
77
      private_class_method :deep_copy
  
db76748d   Isaac Lewis   cop a bunch of St...
78
79
    end
  end