# File lib/reactor/cm/object_base.rb, line 151 def primary_key self.class.primary_key end
class Reactor::Cm::ObjectBase
Public Class Methods
Default base_name
is lowercased class name (without namespaces)
# File lib/reactor/cm/object_base.rb, line 13 def self.base_name self.name.split('::').last.downcase end
Removes object with given pk from CM. Returns true on success, raises XmlRequestError
on error
# 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 return request.execute!.ok? end
Returns true when object with given primary key exists in CM Returns false otherwise
# File lib/reactor/cm/object_base.rb, line 93 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
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)
# File lib/reactor/cm/object_base.rb, line 110 def self.get(pk_val) obj = new(pk_val) obj.reload obj end
# 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
Constructor of this class should never be called directly. Use class methods .get and .create instead (as well as helper method .exists?)
# File lib/reactor/cm/object_base.rb, line 38 def initialize(pk_val) primary_key_value_set(pk_val) end
# File lib/reactor/cm/object_base.rb, line 87 def self.serialize_attribute_to_xml(xml, xml_attribute, value) xml.value_tag!(xml_attribute.name, value) end
Sets the base name for the object. Use it when inheriting the class, for example:
class Obj < ObjectBase set_base_name 'obj' end
# 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 self.class_eval <<-EOH def self.base_name '#{base_name_value}' end EOH end
Protected Class Methods
This method should never be called directly. It should always be overriden! pk_value is the value of primary key, it should have its double in attributes hash attributes is a hash of attributes set on creation {:name => 'value'}
# File lib/reactor/cm/object_base.rb, line 134 def create(pk_value, attributes) request = XmlRequest.prepare do |xml| xml.create_tag!(base_name) do attributes.each do |attr_name, attr_value| #serialize_attribute_to_xml(xml, xml_attribute, value) xml.value_tag!(attr_name, attr_value) end end end response = request.execute! return get(pk_value) end
Public Instance Methods
# File lib/reactor/cm/object_base.rb, line 17 def base_name self.class.base_name end
Alias for delete!
# File lib/reactor/cm/object_base.rb, line 127 def delete ; delete! ; end
Proxy method. @see .delete!(login)
# File lib/reactor/cm/object_base.rb, line 79 def delete! self.class.delete!(self.primary_key_value) end
Reloads the data from CM. Fetches all defined attributes.
# 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| self.send(:"#{attr_name}=", self.class.response_handler.get(response, attr_def)) end self end
Alias for save!
# File lib/reactor/cm/object_base.rb, line 76 def save ; save! ; end
Saves all settable instance attributes to the Content Manager.
# 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 = self.send(name) serialize_attribute_to_xml(xml, xml_attribute, value) end end end response = request.execute! response.ok? end
# File lib/reactor/cm/object_base.rb, line 83 def serialize_attribute_to_xml(xml, xml_attribute, value) self.class.serialize_attribute_to_xml(xml, xml_attribute, value) end
Protected Instance Methods
# File lib/reactor/cm/object_base.rb, line 155 def primary_key_value instance_variable_get("@#{self.primary_key}") end
# File lib/reactor/cm/object_base.rb, line 159 def primary_key_value_set(value) instance_variable_set("@#{self.primary_key}", value) end