Class: RestfulSharePoint::Object

Inherits:
CommonBase show all
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

Methods inherited from CommonBase

#objectify

Constructor Details

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

Returns a new instance of Object.

Raises:



5
6
7
8
9
10
11
12
# File 'lib/restful-sharepoint/object.rb', line 5

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 = connection || @parent.connection
  self.properties = properties
  @id = id
  self.options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/restful-sharepoint/object.rb', line 61

def method_missing(method, *args, &block)
  if properties.respond_to?(method)
    properties.send(method, *args, &block)
  elsif self.methods(false).include?(method) # Works around lazily loaded `properties`
    self.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



14
15
16
# File 'lib/restful-sharepoint/object.rb', line 14

def connection
  @connection
end

#endpointObject



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

def endpoint

end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/restful-sharepoint/object.rb', line 15

def options
  @options
end

Instance Method Details

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



52
53
54
55
# File 'lib/restful-sharepoint/object.rb', line 52

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

#propertiesObject



44
45
46
# File 'lib/restful-sharepoint/object.rb', line 44

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

#properties=(properties) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/restful-sharepoint/object.rb', line 25

def properties=(properties)
  @properties = properties
  @properties&.each do |k,v|
    if v.respond_to?(:keys) && v['__deferred']
      define_singleton_method(k) do |options = {}|
        if Hash === properties[k] && properties[k]['__deferred']
          fetch_deferred(k, options)
        else
          warn("`options` have been ignored as `#{k}` has already been loaded") unless options.empty?
          properties[k]
        end
      end
    elsif v.respond_to?(:keys) && (v['__metadata'] || v['results'])
      @properties[k] = objectify(v)
    end
  end
  @properties
end

#respond_to_missing?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/restful-sharepoint/object.rb', line 71

def respond_to_missing?(method, include_all = false)
  properties.respond_to?(method, include_all)
end

#to_json(*args, &block) ⇒ Object



57
58
59
# File 'lib/restful-sharepoint/object.rb', line 57

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

#valuesObject



48
49
50
# File 'lib/restful-sharepoint/object.rb', line 48

def values
  properties.dup.each { |k,v| properties[k] = v.values if v.is_a?(Object) || v.is_a?(Collection) }
end