class Locomotive::Steam::MongoDBAdapter

Attributes

session[R]

Public Class Methods

build_session(uri_or_hosts, client_options) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 79
def build_session(uri_or_hosts, client_options)
  @session ||= Mongo::Client.new(uri_or_hosts, client_options)
end
disconnect_session() click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 83
def disconnect_session
  @session.try(:close)
  @session = nil
end

Public Instance Methods

all(mapper, query) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 15
def all(mapper, query)
  dataset(mapper, query)
end
base_url(mapper, scope, entity = nil) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 62
def base_url(mapper, scope, entity = nil)
  return nil if scope.site.nil?

  # Note: mimic the Carrierwave behavior
  base = "/sites/#{scope.site._id.to_s}"

  case mapper.name
  when :theme_assets      then "#{base}/theme"
  when :pages             then "#{base}/pages/#{entity._id}/files"
  when :content_entries   then "#{base}/content_entry#{scope.context[:content_type]._id}/#{entity._id}/files"
  end
end
count(mapper, scope, &block) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 24
def count(mapper, scope, &block)
  query = query_klass.new(scope, mapper.localized_attributes, &block)
  query.against(collection(mapper)).count
end
create(mapper, scope, entity) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 34
def create(mapper, scope, entity)
  command(mapper).insert(entity)
end
delete(mapper, scope, entity) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 46
def delete(mapper, scope, entity)
  command(mapper).delete(entity)
end
find(mapper, scope, id) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 29
def find(mapper, scope, id)
  _id = make_id(id)
  query(mapper, scope) { where(_id: _id) }.first
end
inc(mapper, entity, attribute, amount = 1) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 42
def inc(mapper, entity, attribute, amount = 1)
  command(mapper).inc(entity, attribute, amount)
end
key(name, operator) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 50
def key(name, operator)
  name.to_sym.__send__(operator.to_sym)
end
make_id(id) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 54
def make_id(id)
  begin
    BSON::ObjectId.from_string(id)
  rescue BSON::ObjectId::Invalid
    false
  end
end
query(mapper, scope, &block) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 19
def query(mapper, scope, &block)
  query = query_klass.new(scope, mapper.localized_attributes, &block)
  all(mapper, query)
end
update(mapper, scope, entity) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 38
def update(mapper, scope, entity)
  command(mapper).update(entity)
end

Private Instance Methods

client_options() click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 124
def client_options
  options.slice(*Mongo::Client::VALID_OPTIONS)
end
collection(mapper) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 112
def collection(mapper)
  session["locomotive_#{mapper.name}"]
end
command(mapper) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 108
def command(mapper)
  command_klass.new(collection(mapper), mapper)
end
command_klass() click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 96
def command_klass
  Locomotive::Steam::Adapters::MongoDB::Command
end
dataset(mapper, query) click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 100
def dataset(mapper, query)
  Locomotive::Steam::Adapters::MongoDB::Dataset.new do
    query.against(collection(mapper)).map do |attributes|
      entity = mapper.to_entity(attributes)
    end
  end
end
query_klass() click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 92
def query_klass
  Locomotive::Steam::Adapters::MongoDB::Query
end
session() click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 116
def session
  self.class.build_session(uri_or_hosts, client_options)
end
uri_or_hosts() click to toggle source
# File lib/locomotive/steam/adapters/mongodb.rb, line 120
def uri_or_hosts
  options[:uri] || [*options[:hosts]]
end