Class: Locomotive::Steam::MongoDBAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/adapters/mongodb.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.sessionObject (readonly)

Returns the value of attribute session.



77
78
79
# File 'lib/locomotive/steam/adapters/mongodb.rb', line 77

def session
  @session
end

Class Method Details

.build_session(uri_or_hosts, client_options) ⇒ Object



79
80
81
# 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_sessionObject



83
84
85
86
# File 'lib/locomotive/steam/adapters/mongodb.rb', line 83

def disconnect_session
  @session.try(:close)
  @session = nil
end

Instance Method Details

#all(mapper, query) ⇒ Object



15
16
17
# File 'lib/locomotive/steam/adapters/mongodb.rb', line 15

def all(mapper, query)
  dataset(mapper, query)
end

#base_url(mapper, scope, entity = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



24
25
26
27
# 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) ⇒ Object



34
35
36
# File 'lib/locomotive/steam/adapters/mongodb.rb', line 34

def create(mapper, scope, entity)
  command(mapper).insert(entity)
end

#delete(mapper, scope, entity) ⇒ Object



46
47
48
# File 'lib/locomotive/steam/adapters/mongodb.rb', line 46

def delete(mapper, scope, entity)
  command(mapper).delete(entity)
end

#find(mapper, scope, id) ⇒ Object



29
30
31
32
# 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) ⇒ Object



42
43
44
# 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) ⇒ Object



50
51
52
# File 'lib/locomotive/steam/adapters/mongodb.rb', line 50

def key(name, operator)
  name.to_sym.__send__(operator.to_sym)
end

#make_id(id) ⇒ Object



54
55
56
57
58
59
60
# 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) ⇒ Object



19
20
21
22
# 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) ⇒ Object



38
39
40
# File 'lib/locomotive/steam/adapters/mongodb.rb', line 38

def update(mapper, scope, entity)
  command(mapper).update(entity)
end