Module: SmoothOperator::Persistence

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_remote_callObject (readonly)

Returns the value of attribute last_remote_call.



9
10
11
# File 'lib/smooth_operator/persistence.rb', line 9

def last_remote_call
  @last_remote_call
end

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



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

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

  persistence_call(: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)


30
31
32
33
34
# File 'lib/smooth_operator/persistence.rb', line 30

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

  @destroyed = false
end

#get_primary_keyObject



12
13
14
# File 'lib/smooth_operator/persistence.rb', line 12

def get_primary_key
  get_internal_data(self.class.primary_key)
end

#new_record?(bypass_cache = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/smooth_operator/persistence.rb', line 24

def new_record?(bypass_cache = false)
  return @new_record if !bypass_cache && defined?(@new_record)

  @new_record = Helpers.blank?(get_primary_key)
end

#persisted?Boolean

Returns:

  • (Boolean)


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

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

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



16
17
18
19
20
21
22
# File 'lib/smooth_operator/persistence.rb', line 16

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

  persistence_call(: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



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

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



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

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