Class: RestfulSharePoint::Object

Inherits:
CommonBase show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/restful-sharepoint/object.rb

Direct Known Subclasses

File, List, ListItem, Web

Constant Summary collapse

DEFAULT_OPTIONS =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, connection: nil, properties: nil, id: nil, options: {}) ⇒ Object

Returns a new instance of Object.

Raises:



10
11
12
13
14
15
16
17
# File 'lib/restful-sharepoint/object.rb', line 10

def initialize(parent: nil, connection: nil, properties: nil, id: nil, options: {})
  raise Error, "Either a parent or connection must be provided." unless parent || connection
  @parent = parent
  @connection = @parent ? @parent.connection : connection
  self.properties = properties
  @id = id
  self.options = options
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



19
20
21
# File 'lib/restful-sharepoint/object.rb', line 19

def connection
  @connection
end

#endpointObject



26
27
28
# File 'lib/restful-sharepoint/object.rb', line 26

def endpoint
  @endpoint || self['__metadata']['uri'] || (raise NotImplementedError, "Endpoint could not be determined")
end

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/restful-sharepoint/object.rb', line 20

def options
  @options
end

#propertiesObject



31
32
33
# File 'lib/restful-sharepoint/object.rb', line 31

def properties
  @properties || self.properties = connection.get(endpoint, options: @options)
end

Instance Method Details

#==(other) ⇒ Object



46
47
48
# File 'lib/restful-sharepoint/object.rb', line 46

def ==(other)
  other.== properties
end

#[](key, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/restful-sharepoint/object.rb', line 35

def [](key, options = {})
  if connection.objectified?(properties[key])
    warn "`options` have been ignored as deferred object has already been fetched" unless options.empty?
    properties[key]
  elsif properties[key].respond_to?('[]') && properties[key]['__deferred']
    properties[key] = fetch_deferred(key, options)
  else
    properties[key] = connection.objectify(properties[key])
  end
end

#each(&block) ⇒ Object



77
78
79
80
81
# File 'lib/restful-sharepoint/object.rb', line 77

def each(&block)
  properties.each do |k,v|
    yield k, self[k]
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/restful-sharepoint/object.rb', line 50

def eql?(other)
  other.eql? properties
end

#fetch_deferred(property, options = {}) ⇒ Object



69
70
71
# File 'lib/restful-sharepoint/object.rb', line 69

def fetch_deferred(property, options = {})
  connection.get_as_object(@properties[property]['__deferred']['uri'], options: options)
end

#to_hObject Also known as: to_hash



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/restful-sharepoint/object.rb', line 54

def to_h
  hash = {}
  properties.each do |k,v|
    hash[k] = case v
    when Object
      v.to_h
    when Collection
      v.to_a
    else
      v
    end
  end
end

#to_json(*args, &block) ⇒ Object



73
74
75
# File 'lib/restful-sharepoint/object.rb', line 73

def to_json(*args, &block)
  properties.to_json(*args, &block)
end