Module: SmoothOperator::Persistence

Included in:
Base
Defined in:
lib/smooth_operator/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/smooth_operator/persistence.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#destroy(relative_path = nil, data = {}, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/smooth_operator/persistence.rb', line 66

def destroy(relative_path = nil, data = {}, options = {})
  return false unless persisted?

  make_the_call(*persistent_method_args(:destroy, relative_path, data, options)) do |remote_call|
    @destroyed = true if remote_call.status

    block_given? ? yield(remote_call) : remote_call.status
  end
end

#destroyed?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/smooth_operator/persistence.rb', line 40

def destroyed?
  return @destroyed if defined?(@destroyed)

  @destroyed = false
end

#new_record?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/smooth_operator/persistence.rb', line 34

def new_record?
  return @new_record if defined?(@new_record)

  @new_record = Helpers.blank?(get_internal_data("id"))
end

#persisted?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/smooth_operator/persistence.rb', line 46

def persisted?
  !(new_record? || destroyed?)
end

#reload(relative_path = nil, data = {}, options = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/smooth_operator/persistence.rb', line 26

def reload(relative_path = nil, data = {}, options = {})
  raise 'UnknownPath' if Helpers.blank?(relative_path) && (!respond_to?(:id) || Helpers.blank?(id))

  make_the_call(*persistent_method_args(:reload, relative_path, data, options)) do |remote_call|
    block_given? ? yield(remote_call) : remote_call.status
  end
end

#save(relative_path = nil, data = {}, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/smooth_operator/persistence.rb', line 50

def save(relative_path = nil, data = {}, options = {})
  data = data_with_object_attributes(data, options)

  if new_record?
    create(relative_path, data, options) { |remote_call| block_given? ? yield(remote_call) : remote_call.status }
  else
    update(relative_path, data, options) { |remote_call| block_given? ? yield(remote_call) : remote_call.status }
  end
end

#save!(relative_path = nil, data = {}, options = {}) ⇒ Object



60
61
62
63
64
# File 'lib/smooth_operator/persistence.rb', line 60

def save!(relative_path = nil, data = {}, options = {})
  save(relative_path, data, options) do |remote_call|
    block_given? ? yield(remote_call) : remote_call.status
  end || raise('RecordNotSaved')
end