Class: Reactor::Cm::ObjectBase
- Inherits:
-
Object
- Object
- Reactor::Cm::ObjectBase
- Defined in:
- lib/reactor/cm/object_base.rb
Direct Known Subclasses
Class Method Summary collapse
-
.base_name ⇒ Object
Default base_name is lowercased class name (without namespaces).
-
.delete!(pk_val) ⇒ Object
Removes object with given pk from CM.
-
.exists?(pk_val) ⇒ Boolean
Returns true when object with given primary key exists in CM Returns false otherwise.
-
.get(pk_val) ⇒ Object
Returns an instance of the class for object with given primary key XmlRequestError will be raised when error occurs (for example when there is no object with given primary key).
- .inherited(subclass) ⇒ Object
- .serialize_attribute_to_xml(xml, xml_attribute, value) ⇒ Object
-
.set_base_name(base_name_value) ⇒ Object
Sets the base name for the object.
Instance Method Summary collapse
- #base_name ⇒ Object
-
#delete ⇒ Object
Alias for #delete!.
-
#delete! ⇒ Object
Proxy method.
-
#initialize(pk_val) ⇒ ObjectBase
constructor
Constructor of this class should never be called directly.
-
#reload ⇒ Object
Reloads the data from CM.
-
#save ⇒ Object
Alias for #save!.
-
#save! ⇒ Object
Saves all settable instance attributes to the Content Manager.
- #serialize_attribute_to_xml(xml, xml_attribute, value) ⇒ Object
Constructor Details
#initialize(pk_val) ⇒ ObjectBase
Constructor of this class should never be called directly. Use class methods .get and .create instead (as well as helper method .exists?)
39 40 41 |
# File 'lib/reactor/cm/object_base.rb', line 39 def initialize(pk_val) primary_key_value_set(pk_val) end |
Class Method Details
.base_name ⇒ Object
Default base_name is lowercased class name (without namespaces)
14 15 16 |
# File 'lib/reactor/cm/object_base.rb', line 14 def self.base_name self.name.split('::').last.downcase end |
.delete!(pk_val) ⇒ Object
Removes object with given pk from CM. Returns true on success, raises XmlRequestError on error
119 120 121 122 123 124 125 126 |
# File 'lib/reactor/cm/object_base.rb', line 119 def self.delete!(pk_val) request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, primary_key, pk_val) xml.delete_tag!(base_name) end return request.execute!.ok? end |
.exists?(pk_val) ⇒ Boolean
Returns true when object with given primary key exists in CM Returns false otherwise
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/reactor/cm/object_base.rb', line 94 def self.exists?(pk_val) request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, primary_key, pk_val) xml.get_key_tag!(base_name, primary_key) end response = request.execute! return response.ok? rescue XmlRequestError => e return false end |
.get(pk_val) ⇒ Object
Returns an instance of the class for object with given primary key XmlRequestError will be raised when error occurs (for example when there is no object with given primary key)
111 112 113 114 115 |
# File 'lib/reactor/cm/object_base.rb', line 111 def self.get(pk_val) obj = new(pk_val) obj.reload obj end |
.inherited(subclass) ⇒ Object
7 8 9 10 11 |
# File 'lib/reactor/cm/object_base.rb', line 7 def self.inherited(subclass) # dynamic binding is required, otherwise class attributes # aren't stored in the correct class subclass.send(:include, Reactor::XmlAttributes) end |
.serialize_attribute_to_xml(xml, xml_attribute, value) ⇒ Object
88 89 90 |
# File 'lib/reactor/cm/object_base.rb', line 88 def self.serialize_attribute_to_xml(xml, xml_attribute, value) xml.value_tag!(xml_attribute.name, value) end |
.set_base_name(base_name_value) ⇒ Object
Sets the base name for the object. Use it when inheriting the class, for example:
class Obj < ObjectBase
set_base_name 'obj'
end
26 27 28 29 30 31 32 33 34 |
# File 'lib/reactor/cm/object_base.rb', line 26 def self.set_base_name(base_name_value) # we us evaluation of a string in this case, because # define_method cannot handle default values self.class_eval <<-EOH def self.base_name '#{base_name_value}' end EOH end |
Instance Method Details
#base_name ⇒ Object
18 19 20 |
# File 'lib/reactor/cm/object_base.rb', line 18 def base_name self.class.base_name end |
#delete ⇒ Object
Alias for #delete!
128 |
# File 'lib/reactor/cm/object_base.rb', line 128 def delete ; delete! ; end |
#delete! ⇒ Object
Proxy method. @see .delete!(login)
80 81 82 |
# File 'lib/reactor/cm/object_base.rb', line 80 def delete! self.class.delete!(self.primary_key_value) end |
#reload ⇒ Object
Reloads the data from CM. Fetches all defined attributes.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/reactor/cm/object_base.rb', line 44 def reload request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, primary_key, primary_key_value) xml.get_key_tag!(base_name, self.class.xml_attribute_names) end response = request.execute! self.class.attributes.each do |attr_name, attr_def| self.send(:"#{attr_name}=", self.class.response_handler.get(response, attr_def)) end self end |
#save ⇒ Object
Alias for #save!
77 |
# File 'lib/reactor/cm/object_base.rb', line 77 def save ; save! ; end |
#save! ⇒ Object
Saves all settable instance attributes to the Content Manager.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/reactor/cm/object_base.rb', line 60 def save! request = XmlRequest.prepare do |xml| xml.where_key_tag!(base_name, primary_key, primary_key_value) xml.set_tag!(base_name) do self.class.attributes(:set).each do |name, xml_attribute| value = self.send(name) serialize_attribute_to_xml(xml, xml_attribute, value) end end end response = request.execute! response.ok? end |
#serialize_attribute_to_xml(xml, xml_attribute, value) ⇒ Object
84 85 86 |
# File 'lib/reactor/cm/object_base.rb', line 84 def serialize_attribute_to_xml(xml, xml_attribute, value) self.class.serialize_attribute_to_xml(xml, xml_attribute, value) end |