Class: Restforce::Mash

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/restforce/mash.rb

Direct Known Subclasses

SObject

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_hash = nil, client = nil, default = nil) ⇒ Mash

Returns a new instance of Mash.



49
50
51
52
53
# File 'lib/restforce/mash.rb', line 49

def initialize(source_hash = nil, client = nil, default = nil, &)
  @client = client
  deep_update(source_hash) if source_hash
  default ? super(default) : super(&)
end

Class Method Details

.build(val, client) ⇒ Object

Pass in an Array or Hash and it will be recursively converted into the appropriate Restforce::Collection, Restforce::SObject and Restforce::Mash objects.



13
14
15
16
17
18
19
20
21
22
# File 'lib/restforce/mash.rb', line 13

def build(val, client)
  case val
  when Array
    val.collect { |a_val| self.build(a_val, client) }
  when Hash
    self.klass(val).new(val, client)
  else
    val
  end
end

.klass(val) ⇒ Object

When passed a hash, it will determine what class is appropriate to represent the data.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/restforce/mash.rb', line 26

def klass(val)
  if val.key? 'records'
    # When the hash has a records key, it should be considered a collection
    # of sobject records.
    Restforce::Collection
  elsif val.key? 'attributes'
    case val.dig('attributes', 'type')
    when "Attachment"
      Restforce::Attachment
    when "Document"
      Restforce::Document
    else
      # When the hash contains an attributes key, it should be considered an
      # sobject record
      Restforce::SObject
    end
  else
    # Fallback to a standard Restforce::Mash for everything else
    Restforce::Mash
  end
end

Instance Method Details

#convert_value(val, duping = false) ⇒ Object

The #convert_value method and its signature are part of Hashie::Mash’s API, so we can’t unilaterally decide to change ‘duping` to be a keyword argument rubocop:disable Style/OptionalBooleanParameter



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/restforce/mash.rb', line 62

def convert_value(val, duping = false)
  case val
  when self.class
    val.dup
  when ::Hash
    val = val.dup if duping
    self.class.klass(val).new(val, @client)
  when Array
    val.collect { |e| convert_value(e) }
  else
    val
  end
end

#dupObject



55
56
57
# File 'lib/restforce/mash.rb', line 55

def dup
  self.class.new(self, @client, self.default)
end