Class: RestfulSharePoint::CommonBase

Inherits:
Object
  • Object
show all
Defined in:
lib/restful-sharepoint/common-base.rb

Direct Known Subclasses

Collection, Object

Instance Method Summary collapse

Instance Method Details

#objectify(tree) ⇒ Object

Converts the given enumerable tree to a collection or object.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/restful-sharepoint/common-base.rb', line 4

def objectify(tree)
  if tree['results'] && !tree['results'].empty?
    type = tree.dig('__metadata', 'type')  || tree.dig('results', 0, '__metadata', 'type')
    raise "Object type not specified" unless type
    pattern, klass = COLLECTION_MAP.find { |pattern,| pattern.match(type)  }
    klass ? RestfulSharePoint.const_get(klass).new(parent: self, collection: tree['results']) : tree['results']
  elsif tree['__metadata']
    type = tree['__metadata']['type']
    raise "Collection type not specified" unless type
    pattern, klass = OBJECT_MAP.find { |pattern,| pattern.match(type)  }
    klass ? RestfulSharePoint.const_get(klass).new(parent: self, properties: tree) : tree
  end
end