Module: FoundationApi::Table::Persistence::ClassMethods

Defined in:
lib/foundation_api/table/persistence.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes = {}) ⇒ Object



51
52
53
# File 'lib/foundation_api/table/persistence.rb', line 51

def create(attributes = {})
  new(attributes).save
end

#create!(attributes = {}) ⇒ Object



54
55
56
# File 'lib/foundation_api/table/persistence.rb', line 54

def create!(attributes = {})
  new(attributes).save!
end

#destroy(*ids) ⇒ Object



58
59
60
# File 'lib/foundation_api/table/persistence.rb', line 58

def destroy(*ids)
  remote_destroy *ids
end

#persistence(options = {}) ⇒ Object

specify persistence support. You may specify all on one line if you like Example:

persistence create: :CreatePossession, unique: :name
persistence update: :UpdatePossession
persistence destroy: :DestroyPossession, destroy_key: possessionId


23
24
25
26
# File 'lib/foundation_api/table/persistence.rb', line 23

def persistence(options = {})
  options.assert_valid_keys(:create, :update, :destroy, :unique, :hash_to_array, :destroy_key, :destroy_type)
  self.persistence_options = self.persistence_options.merge(options)
end

#remote_create(object) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/foundation_api/table/persistence.rb', line 28

def remote_create(object)
  unique_key = object.persistence_options[:unique]
  if unique_key
    existing_object = where(unique_key => object[unique_key], :deleted => 0).first
    if existing_object
      raise RecordNotUnique, "Record with key #{unique_key}: #{object[unique_key]} already exists"
    end
  end
  id = request persistence_options[:create], object.map_attributes
  ::Rails.logger.debug "FoundationApi::Table::Persistence.remote_create: request response: #{id.inspect}"
  id.is_a?(Array) ? id.first : id
end

#remote_destroy(*ids) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/foundation_api/table/persistence.rb', line 41

def remote_destroy(*ids)
  if persistence_options[:destroy_type] == :array
    request persistence_options[:destroy], ids
  else
    ids.flatten.each do |id|
      request persistence_options[:destroy], {destroy_key => id }
    end
  end
end