Blame view

lib/syspro/syspro_object.rb 2.32 KB
dc8aa5b6   Joe Weakley   Rubocop corrections
1
2
  # frozen_string_literal: true
  
db76748d   Isaac Lewis   cop a bunch of St...
3
  module Syspro
dc8aa5b6   Joe Weakley   Rubocop corrections
4
    # This class represents a syspro response
db76748d   Isaac Lewis   cop a bunch of St...
5
    class SysproObject
3d0157a5   Isaac Lewis   add logoff
6
      include Enumerable
db76748d   Isaac Lewis   cop a bunch of St...
7
  
3d0157a5   Isaac Lewis   add logoff
8
      def initialize(id = nil, opts = {})
db76748d   Isaac Lewis   cop a bunch of St...
9
        @opts = Util.normalize_opts(opts)
0c0af54a   Isaac Lewis   error handling; c...
10
11
12
13
14
15
16
17
        @original_values = {}
        @values = {}
  
        # This really belongs in APIResource, but not putting it there allows us
        # to have a unified inspect method
        @unsaved_values = Set.new
        @transient_values = Set.new
        @values[:id] = id if id
3d0157a5   Isaac Lewis   add logoff
18
      end
db76748d   Isaac Lewis   cop a bunch of St...
19
  
3d0157a5   Isaac Lewis   add logoff
20
21
22
23
      # 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)
dc8aa5b6   Joe Weakley   Rubocop corrections
24
25
        other.is_a?(SysproObject) &&
          @values == other.instance_variable_get(:@values)
3d0157a5   Isaac Lewis   add logoff
26
      end
db76748d   Isaac Lewis   cop a bunch of St...
27
  
3d0157a5   Isaac Lewis   add logoff
28
29
30
      def to_s(*_args)
        JSON.pretty_generate(to_hash)
      end
db76748d   Isaac Lewis   cop a bunch of St...
31
  
3d0157a5   Isaac Lewis   add logoff
32
      def inspect
dc8aa5b6   Joe Weakley   Rubocop corrections
33
34
        id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : ''
        "#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values) # rubocop:disable Metrics/LineLength
3d0157a5   Isaac Lewis   add logoff
35
      end
db76748d   Isaac Lewis   cop a bunch of St...
36
  
3d0157a5   Isaac Lewis   add logoff
37
38
      def keys
        @values.keys
db76748d   Isaac Lewis   cop a bunch of St...
39
40
      end
  
3d0157a5   Isaac Lewis   add logoff
41
42
43
      def values
        @values.values
      end
db76748d   Isaac Lewis   cop a bunch of St...
44
  
dc8aa5b6   Joe Weakley   Rubocop corrections
45
      def to_hash # rubocop:disable Metrics/MethodLength
3d0157a5   Isaac Lewis   add logoff
46
        maybe_to_hash = lambda do |value|
ec2356d2   Nathan-O   added parsing for...
47
          value.respond_to?(:to_hash) ? value.to_hash : value
3d0157a5   Isaac Lewis   add logoff
48
        end
db76748d   Isaac Lewis   cop a bunch of St...
49
  
3d0157a5   Isaac Lewis   add logoff
50
51
52
53
54
55
56
        @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...
57
58
59
        end
      end
  
3d0157a5   Isaac Lewis   add logoff
60
61
62
      def each(&blk)
        @values.each(&blk)
      end
db76748d   Isaac Lewis   cop a bunch of St...
63
  
3d0157a5   Isaac Lewis   add logoff
64
      # Produces a deep copy of the given object including support for arrays,
0c0af54a   Isaac Lewis   error handling; c...
65
      # hashes, and SysproObjects.
dc8aa5b6   Joe Weakley   Rubocop corrections
66
      def self.deep_copy(obj) # rubocop:disable Metrics/MethodLength
3d0157a5   Isaac Lewis   add logoff
67
68
69
70
71
72
73
        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...
74
          end
3d0157a5   Isaac Lewis   add logoff
75
76
77
78
79
80
81
82
83
        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...
84
85
        end
      end
3d0157a5   Isaac Lewis   add logoff
86
      private_class_method :deep_copy
db76748d   Isaac Lewis   cop a bunch of St...
87
88
    end
  end