Class: GProv::Provision::EntryBase

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/gprov/provision/entrybase.rb,
lib/gprov/provision/entrybase/xmlattr.rb,
lib/gprov/provision/entrybase/classmethods.rb

Direct Known Subclasses

CustomerID, Group, Member, OrgMember, OrgUnit, Owner, User

Defined Under Namespace

Modules: ClassMethods Classes: XMLAttr

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

attribute_names, attribute_titles, attributes, xmlattr

Constructor Details

#initialize(opts = {}) ⇒ EntryBase

Instantiates a new entry object.

Possible data sources:

* Hash of attribute names and values
* A nokogiri node containing the root of the object
* nothing, as in we have a fresh object.


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gprov/provision/entrybase.rb', line 35

def initialize(opts={})

  @status = (opts[:status] || :new)

  if opts[:connection]
    @connection = opts[:connection]
  else
    raise ArgumentError, "#{self.class}.new requires a connection parameter"
  end

  case source = opts[:source]
  when Hash
    attributes_from_hash source
  when Nokogiri::XML::Node
    hash = xml_to_hash(source)
    attributes_from_hash hash
  when NilClass
    # New object!
  else
    raise ArgumentError, "unrecognized object source #{opts[:source]}"
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



27
28
29
# File 'lib/gprov/provision/entrybase.rb', line 27

def connection
  @connection
end

#statusObject (readonly)

Status with respect to google. TODO protected? values: :new, :clean, :dirty, :deleted



26
27
28
# File 'lib/gprov/provision/entrybase.rb', line 26

def status
  @status
end

Instance Method Details

#attributes_from_hash(hash) ⇒ Object

Maps hash key/value pairs to object attributes



72
73
74
75
76
77
78
79
80
# File 'lib/gprov/provision/entrybase.rb', line 72

def attributes_from_hash(hash)
  hash.each_pair do |k, v|
    if respond_to? "#{k}=".intern
      send("#{k}=".intern, v)
    else
      raise ArgumentError, %Q{Received invalid attribute "#{k}"}
    end
  end
end

#xml_to_hash(xml) ⇒ Object

Takes all xmlattrs defined against this object, and a given XML document, and converts each xmlattr into the according value.



60
61
62
63
64
65
66
67
68
69
# File 'lib/gprov/provision/entrybase.rb', line 60

def xml_to_hash(xml)
  h = {}
  if attrs = self.class.attributes
    attrs.inject(h) do |hash, attr|
      hash[attr.name] = attr.parse(xml)
      hash
    end
  end
  h
end