Module: Tupplur::ModelExtensions::ClassMethods

Defined in:
lib/tupplur/model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#external_create!(fields) ⇒ Object



102
103
104
105
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


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



84
85
86
87
88
# 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) ⇒ Object



78
79
80
81
82
# 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) ⇒ Object



64
65
66
67
68
69
70
# 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_allObject



96
97
98
99
100
# 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) ⇒ Object



90
91
92
93
94
# 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) ⇒ Object



72
73
74
75
76
# 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