Mark Mapper

Types

Because of MarkLogic’s ability to store rich documents, MarkMapper supports most of Ruby’s data types out of the box, such as Array, Float, Hash, Integer, NilClass, Object, String, and Time. In addition to those, MarkMapper adds support for Binary, Boolean, Date, ObjectId, and Set by serializing to and from MarkLogic safe types.

Custom Types

The great thing about MarkMapper is that you can create your own types to make your models more pimp. All that is required to make your own type is to define the class and add the to_marklogic and from_marklogic class methods. For example, here is a type that always downcases strings when saving to the database:

class DowncasedString
  def self.to_marklogic(value)
    value.nil? ? nil : value.to_s.downcase
  end

  def self.from_marklogic(value)
    to_marklogic(value)
  end
end

Or, if you are curious about a slightly more complex example, here is a type that is used in MarkMapper’s tests:

class WindowSize
  attr_reader :width, :height

  def self.to_marklogic(value)
    value.to_a
  end

  def self.from_marklogic(value)
    value.is_a?(self) ? value : WindowSize.new(value)
  end

  def initialize(*args)
    @width, @height = args.flatten
  end

  def to_a
    [width, height]
  end

  def eql?(other)
    self.class.eql?(other.class) &&
       width == other.width &&
       height == other.height
  end
  alias :== :eql?
end

This example actually serializes to an array for storage in MarkLogic, but returns an actual WindowSize object after retrieving from the database.

Fork me on GitHub //