Class: ActiveSP::Base

Inherits:
Object
  • Object
show all
Extended by:
Associations, Caching
Defined in:
lib/activesp/base.rb

Overview

The base class for all objects stored in SharePoint

Direct Known Subclasses

ContentType, Field, Group, Item, List, Role, Site, User

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object

Provides convenient getters and setters for attributes. Note that no name mangling is done, so an attribute such as Title is accessed as obj.Title. The main rationale behind this is that name mangling is usually not lossless (e.g., both Title and title could map to the more Rubyish title) and imperfect.



97
98
99
100
101
102
103
104
105
106
# File 'lib/activesp/base.rb', line 97

def method_missing(m, *a, &b)
  ms = m.to_s
  if a.length == 0 && has_attribute?(ms)
    attribute(ms)
  elsif a.length == 1 && ms[-1] == ?= && has_writable_attribute?(ms[0..-2])
    set_attribute(ms[0..-2], *a)
  else
    super
  end
end

Instance Method Details

#attribute(name) ⇒ Integer, ...

Returns the value of the attribute of the given name, or nil if this object does not have an attribute by the given name

Parameters:

  • name (String)

    The name of the attribute

Returns:

  • (Integer, Float, String, Time, Boolean, Base)


71
72
73
# File 'lib/activesp/base.rb', line 71

def attribute(name)
  current_attributes[name]
end

#attribute_type(name) ⇒ Field

Returns the type of the attribute by the given name, or nil if this object does not have an attribute by the given name

Parameters:

  • name (String)

    The name of the attribute

Returns:



78
79
80
# File 'lib/activesp/base.rb', line 78

def attribute_type(name)
  internal_attribute_types[name]
end

#attribute_typesHash{String => Field}

Returns the types of the attributes of this object as a Hash

Returns:

  • (Hash{String => Field})


49
50
51
# File 'lib/activesp/base.rb', line 49

def attribute_types
  internal_attribute_types
end

#attributesHash{String => Integer, Float, String, Time, Boolean, Base}

Returns the attributes of this object as a Hash

Returns:

  • (Hash{String => Integer, Float, String, Time, Boolean, Base})


42
43
44
# File 'lib/activesp/base.rb', line 42

def attributes
  current_attributes
end

#has_attribute?(name) ⇒ Boolean

Returns whether or not this object has an attribute with the given name

Parameters:

  • name (String)

    The name of the attribute

Returns:

  • (Boolean)


57
58
59
# File 'lib/activesp/base.rb', line 57

def has_attribute?(name)
  current_attributes.has_key?(name)
end

#has_writable_attribute?(name) ⇒ Boolean

Returns whether or not this object has an attribute with the given name that can be assigned to

Parameters:

  • name (String)

    The name of the attribute

Returns:

  • (Boolean)


64
65
66
# File 'lib/activesp/base.rb', line 64

def has_writable_attribute?(name)
  has_attribute?(name) and attr = internal_attribute_types[name] and !attr.ReadOnly
end

#keyString

Returns a key that can be used to retrieve this object later on using Connection#find_by_key

Returns:

  • (String)


36
37
38
# File 'lib/activesp/base.rb', line 36

def key
  raise "This is here for documentation purposes only"
end

#reloadvoid

This method returns an undefined value.

Reloads the object from the server



110
111
# File 'lib/activesp/base.rb', line 110

def reload
end

#savevoid

This method returns an undefined value.

Saves the object to the server. Raises an exception when the save fails



115
116
# File 'lib/activesp/base.rb', line 115

def save
end

#set_attribute(name, value) ⇒ Integer, ...

Sets the attribute with the given name to the given value

Parameters:

  • name (String)

    The name of the attribute

  • value (Integer, Float, String, Time, Boolean, Base)

    The value to assign

Returns:

  • (Integer, Float, String, Time, Boolean, Base)

    The assigned value

Raises:

  • (ArgumentError)

    Raised when this object does not have an attribute by the given name or if the attribute by the given name is read-only



87
88
89
90
91
# File 'lib/activesp/base.rb', line 87

def set_attribute(name, value)
  has_attribute?(name) and field = attribute_type(name) and internal_attribute_types[name] or raise ArgumentError, "#{self} has no field by the name #{name}"
  !field.ReadOnly or raise ArgumentError, "field #{name} of #{self} is read-only"
  current_attributes[name] = type_check_attribute(field, value)
end