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?)
38 39 40 |
# File 'lib/reactor/cm/object_base.rb', line 38 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)
13 14 15 |
# File 'lib/reactor/cm/object_base.rb', line 13 def self.base_name name.split("::").last.downcase end |
.delete!(pk_val) ⇒ Object
Removes object with given pk from CM. Returns true on success, raises XmlRequestError on error
118 119 120 121 122 123 124 125 |
# File 'lib/reactor/cm/object_base.rb', line 118 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 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 |
# 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! response.ok? rescue XmlRequestError 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)
110 111 112 113 114 |
# File 'lib/reactor/cm/object_base.rb', line 110 def self.get(pk_val) obj = new(pk_val) obj.reload obj end |
.inherited(subclass) ⇒ Object
6 7 8 9 10 |
# File 'lib/reactor/cm/object_base.rb', line 6 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
25 26 27 28 29 30 31 32 33 |
# File 'lib/reactor/cm/object_base.rb', line 25 def self.set_base_name(base_name_value) # we us evaluation of a string in this case, because # define_method cannot handle default values class_eval " def self.base_name\n '\#{base_name_value}'\n end\n EOH\nend\n" |
Instance Method Details
#base_name ⇒ Object
17 18 19 |
# File 'lib/reactor/cm/object_base.rb', line 17 def base_name self.class.base_name end |
#delete ⇒ Object
Alias for #delete!
128 129 130 |
# 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!(primary_key_value) end |
#reload ⇒ Object
Reloads the data from CM. Fetches all defined attributes.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/reactor/cm/object_base.rb', line 43 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| send(:"#{attr_name}=", self.class.response_handler.get(response, attr_def)) end self end |
#save ⇒ Object
Alias for #save!
75 76 77 |
# File 'lib/reactor/cm/object_base.rb', line 75 def save save! end |
#save! ⇒ Object
Saves all settable instance attributes to the Content Manager.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/reactor/cm/object_base.rb', line 59 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 = 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 |