module MarkMapper::Plugins::Indexable::ClassMethods

Public Instance Methods

index(name, options = {}) click to toggle source

Defines all the MarkLogic indexes for this document

@example Define an index. Model.index :first_name, :type => String, :facet => true

@param [ Symbol ] name The localname of the element to index. @param [ Hash ] options The options to pass to the index.

@option options [ String ] :type The atomic type of the element to index. @option options [ Boolean ] :facet Whether to facet on this element index or not

@since 0.0.1

# File lib/mark_mapper/plugins/indexable.rb, line 31
def index(name, options = {})
  options[:type] ||= String
  options[:type] = options[:type].xs_type if options[:type].respond_to?(:xs_type)

  raise InvalidIndexType, "Invalid index type: #{options[:type]}" unless [Boolean.xs_type, Integer.xs_type, String.xs_type, Float.xs_type].include? options[:type]

  new_index =
    begin
      case options[:kind]
        when "field" then MarkLogic::DatabaseSettings::RangeFieldIndex.new(name, options)
        when "path" then MarkLogic::DatabaseSettings::RangePathIndex.new(name, options)
        else MarkLogic::DatabaseSettings::RangeElementIndex.new(name, options)
      end
    end

  if !index_defs.has_key?(name.to_s)
    index_defs[name.to_s] = new_index
    MarkMapper.application.add_index(new_index)
  end
end
index_defs() click to toggle source

Get all the defined indexes for this document

@example Get the indexes hash

person.index_defs

@return[ Hash ] A hash containing all the index definitions

@since 0.0.1

# File lib/mark_mapper/plugins/indexable.rb, line 15
def index_defs
  @index_defs ||= {}
end
remove_index(name) click to toggle source

Removes the definition for an index Used for testing

@example Remove an index Model.remove_index :name

@param [ String, Symbol ] name The name of the index to remove

@since 0.0.1

# File lib/mark_mapper/plugins/indexable.rb, line 61
def remove_index(name)
  index_defs.delete name.to_s
end