class Reactor::Cm::ObjectBase

Public Class Methods

base_name() click to toggle source

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
delete!(pk_val) click to toggle source

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
exists?(pk_val) click to toggle source

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
get(pk_val) click to toggle source

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
inherited(subclass) click to toggle source
# 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
new(pk_val) click to toggle source

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
serialize_attribute_to_xml(xml, xml_attribute, value) click to toggle source
# 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
set_base_name(base_name_value) click to toggle source

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

create(pk_value, attributes) click to toggle source

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

base_name() click to toggle source
# File lib/reactor/cm/object_base.rb, line 17
def base_name
  self.class.base_name
end
delete() click to toggle source

Alias for delete!

# File lib/reactor/cm/object_base.rb, line 127
def delete ; delete! ; end
delete!() click to toggle source

Proxy method. @see .delete!(login)

# File lib/reactor/cm/object_base.rb, line 79
def delete!
  self.class.delete!(self.primary_key_value)
end
reload() click to toggle source

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
save() click to toggle source

Alias for save!

# File lib/reactor/cm/object_base.rb, line 76
def save ; save! ; end
save!() click to toggle source

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
serialize_attribute_to_xml(xml, xml_attribute, value) click to toggle source
# 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

primary_key() click to toggle source
# File lib/reactor/cm/object_base.rb, line 151
def primary_key
  self.class.primary_key
end
primary_key_value() click to toggle source
# File lib/reactor/cm/object_base.rb, line 155
def primary_key_value
  instance_variable_get("@#{self.primary_key}")
end
primary_key_value_set(value) click to toggle source
# File lib/reactor/cm/object_base.rb, line 159
def primary_key_value_set(value)
  instance_variable_set("@#{self.primary_key}", value)
end