module Tupplur::ModelExtensions::ClassMethods

Public Instance Methods

external_create!(fields) click to toggle source
# File lib/tupplur/model_extensions.rb, line 102
def external_create!(fields)
  allowed_fields = filter_accessible_fields(fields)
  create!(allowed_fields)
end
rest?(operation = nil) click to toggle source

Does the model allow a certain REST operation? If no operation given: Does the model allow any REST operation at all?

# File lib/tupplur/model_extensions.rb, line 56
def rest?(operation = nil)
  if operation
    const_get(:REST_INTERFACE).include?(operation)
  else
    ! const_get(:REST_INTERFACE).empty?
  end
end
rest_create(client_fields) click to toggle source
# File lib/tupplur/model_extensions.rb, line 84
def rest_create(client_fields)
  raise Tupplur::Error::RESTOperationNotAllowed.new('create') unless rest?(:create)

  external_create!(client_fields)
end
rest_delete(document_id) click to toggle source
# File lib/tupplur/model_extensions.rb, line 78
def rest_delete(document_id)
  raise Tupplur::Error::RESTOperationNotAllowed.new('delete') unless rest?(:delete)

  find(document_id).delete
end
rest_read(document_id = nil) click to toggle source
# File lib/tupplur/model_extensions.rb, line 64
def rest_read(document_id = nil)
  if document_id
    rest_read_document(document_id)
  else
    rest_read_all
  end
end
rest_read_all() click to toggle source
# File lib/tupplur/model_extensions.rb, line 96
def rest_read_all
  raise Tupplur::Error::RESTOperationNotAllowed.new('read') unless rest?(:read)

  all.map { |m| m.as_external_document }
end
rest_read_document(document_id) click to toggle source
# File lib/tupplur/model_extensions.rb, line 90
def rest_read_document(document_id)
  raise Tupplur::Error::RESTOperationNotAllowed.new('read') unless rest?(:read)

  find(document_id).as_external_document
end
rest_update(document_id, client_model) click to toggle source
# File lib/tupplur/model_extensions.rb, line 72
def rest_update(document_id, client_model)
  raise Tupplur::Error::RESTOperationNotAllowed.new('update') unless rest?(:update)

  find(document_id).external_update!(client_model)
end

Private Instance Methods

externally_accessible(*fields) click to toggle source

Externally accessible fields and embedded documents.

# File lib/tupplur/model_extensions.rb, line 110
def externally_accessible(*fields)
  const_get(:EXTERNALLY_ACCESSIBLE_FIELDS).push(*fields)
end
externally_readable(*fields) click to toggle source

Externally readable fields and embedded documents.

# File lib/tupplur/model_extensions.rb, line 115
def externally_readable(*fields)
  const_get(:EXTERNALLY_READABLE_FIELDS).push(*fields)
end
filter_accessible_fields(hsh) click to toggle source
# File lib/tupplur/model_extensions.rb, line 132
def filter_accessible_fields(hsh)
  hsh.slice(*const_get(:EXTERNALLY_ACCESSIBLE_FIELDS).map(&:to_s))
end
readable_fields() click to toggle source
# File lib/tupplur/model_extensions.rb, line 136
def readable_fields
  (const_get(:EXTERNALLY_ACCESSIBLE_FIELDS) + const_get(:EXTERNALLY_READABLE_FIELDS)).map(&:to_s)
end
rest_interface(*operations) click to toggle source

Used to define allowed REST operations (e.g. :read, :create, :update, :delete). Example usage: class MyModel

include Mongoid::Document
include Tupplur::ModelExtensions

rest_interface :read, :update, :delete
# File lib/tupplur/model_extensions.rb, line 127
def rest_interface(*operations)
  const_get(:REST_INTERFACE).push(*operations)
end