Class: Vayacondios::Server::Document

Inherits:
Object
  • Object
show all
Includes:
Gorillib::Model
Defined in:
lib/vayacondios/server/models/document.rb

Direct Known Subclasses

Event, Stash

Constant Summary collapse

Error =

A class for errors that arise within documents due to internal or IO errors.

Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(params, document, &driver) ⇒ Object



75
76
77
78
79
# File 'lib/vayacondios/server/models/document.rb', line 75

def create(params, document, &driver)
  action = receive(params).prepare_create(document)
  result = driver.call(action)
  action.format_response result
end

.destroy(params, document, &driver) ⇒ Object



88
89
90
91
92
# File 'lib/vayacondios/server/models/document.rb', line 88

def destroy(params, document, &driver)
  action = receive(params).prepare_destroy(document.symbolize_keys)
  result = driver.call(action, action.filter)
  return result
end

.extract_query_options!(params) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vayacondios/server/models/document.rb', line 53

def extract_query_options! params
  params.symbolize_keys!
  opts = {}
  [:limit, :order, :sort, :fields].each{ |opt| opts[opt] = params.delete opt }
  default_query_options.dup.merge(opts.compact).tap do |opts|
    opts[:sort]   = opts[:sort].to_s.split('.')
    if opts[:fields].is_a? Array
      opts[:fields].map!{|field|
        field = '_id' if field == 'id'
        field.split('.')
      }
    end
  end
end

.find(params, &driver) ⇒ Object



81
82
83
84
85
86
# File 'lib/vayacondios/server/models/document.rb', line 81

def find(params, &driver)
  action = receive(params).prepare_find
  result = driver.call(action, {})
  return nil if result.nil?
  action.format_response result
end

.search(params, query, &driver) ⇒ Object



68
69
70
71
72
73
# File 'lib/vayacondios/server/models/document.rb', line 68

def search(params, query, &driver)
  options = extract_query_options! query
  action  = receive(params).prepare_search(query)
  result  = driver.call(action, action.filter, options)
  result.map{ |res| new.format_response res }
end

Instance Method Details

#format_response(result) ⇒ Object



30
31
32
# File 'lib/vayacondios/server/models/document.rb', line 30

def format_response result
  from_document(result.symbolize_keys.compact).external_document
end

#receive_organization(name) ⇒ Object



26
27
28
# File 'lib/vayacondios/server/models/document.rb', line 26

def receive_organization name
  @organization = sanitize_location_name(name).gsub(/^system\./, '_system.')
end

#sanitize_location_name(name) ⇒ String

Sanitize a string to make a suitable component of a database location name.

Replaces all characters that aren’t letters, digits, underscores, periods, or hyphens with underscores. Also replaces periods at the beginning or at the end of a collection name with an underscore.

Parameters:

  • name (String)

Returns:

  • (String)

    the sanitized ‘name`



48
49
50
# File 'lib/vayacondios/server/models/document.rb', line 48

def sanitize_location_name name
  name.to_s.gsub(/^\.|[^-\w\.]+|\.$/, '_')
end