Module: MopedMapping::SessionContextExt

Defined in:
lib/moped_mapping/session_context_ext.rb

Constant Summary collapse

COLLECTION_COMMAND_NAMES =
(collection_command_names + collection_command_names.map(&:to_s).map(&:freeze)).freeze
COMMAND_ARGS_ARRAY =
(
  command_args_array +
  command_args_array.map{|args| args.map(&:to_s).map(&:freeze).freeze }
).freeze
FULL_NAME_COMMAND_NAMES =
(full_name_command_names + full_name_command_names.map(&:to_s).map(&:freeze)).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/moped_mapping/session_context_ext.rb', line 6

def self.included(klass)
  klass.module_eval do
    %w[query insert update remove command].each do |m|
      alias_method :"#{m}_without_mapping", m.to_sym
      alias_method m.to_sym, :"#{m}_with_mapping"
    end
  end
end

Instance Method Details

#command_with_mapping(database, command, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/moped_mapping/session_context_ext.rb', line 76

def command_with_mapping(database, command, &block)
  done = false
  COLLECTION_COMMAND_NAMES.each do |name|
    next unless command.key?(name)
    command[name] = MopedMapping.mapped_name(database, command[name])
    done = true
    break
  end
  unless done
    COMMAND_ARGS_ARRAY.each do |args|
      next unless command.key?(args.first)
      args.each do |arg|
        command[arg] = MopedMapping.mapped_name(database, command[arg])
      end
      done = true
      break
    end
  end
  unless done
    FULL_NAME_COMMAND_NAMES.each do |name|
      next unless command.key?(name)
      command[name] = MopedMapping.mapped_full_name(database, command[name])
    end
  end
  return command_without_mapping(database, command, &block)
end

#insert_with_mapping(database, collection, documents, options = {}, &block) ⇒ Object



20
21
22
23
# File 'lib/moped_mapping/session_context_ext.rb', line 20

def insert_with_mapping(database, collection, documents, options = {}, &block)
  collection = MopedMapping.mapped_name(database, collection)
  return insert_without_mapping(database, collection, documents, options, &block)
end

#query_with_mapping(database, collection, selector, options = {}, &block) ⇒ Object



15
16
17
18
# File 'lib/moped_mapping/session_context_ext.rb', line 15

def query_with_mapping(database, collection, selector, options = {}, &block)
  collection = MopedMapping.mapped_name(database, collection)
  return query_without_mapping(database, collection, selector, options, &block)
end

#remove_with_mapping(database, collection, selector, options = {}, &block) ⇒ Object



30
31
32
33
# File 'lib/moped_mapping/session_context_ext.rb', line 30

def remove_with_mapping(database, collection, selector, options = {}, &block)
  collection = MopedMapping.mapped_name(database, collection)
  return remove_without_mapping(database, collection, selector, options, &block)
end

#update_with_mapping(database, collection, selector, change, options = {}, &block) ⇒ Object



25
26
27
28
# File 'lib/moped_mapping/session_context_ext.rb', line 25

def update_with_mapping(database, collection, selector, change, options = {}, &block)
  collection = MopedMapping.mapped_name(database, collection)
  return update_without_mapping(database, collection, selector, change, options, &block)
end