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.



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

def last_remote_call
  @last_remote_call
end

Class Method Details

.included(base) ⇒ Object



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

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

Instance Method Details

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



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

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

  resource_data = resource_data_for_server(data, options)

  make_a_persistence_call(:destroy, relative_path, resource_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)


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

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

  @destroyed = false
end

#known_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


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

def known_attribute?(attribute)
  super ||
  [self.class.primary_key, self.class.destroy_key].include?(attribute.to_s)
end

#marked_for_destruction?(ignore_cache = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def marked_for_destruction?(ignore_cache = false)
  if !ignore_cache && defined?(@marked_for_destruction)
    return @marked_for_destruction
  end

  _destroy = internal_data_get(self.class.destroy_key)

  @marked_for_destruction = TypeCasting::TRUE_VALUES.include?(_destroy)
end

#new_record?(ignore_cache = false) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  @new_record = Helpers.has_primary_key?(self)
end

#persisted?Boolean

Returns:

  • (Boolean)


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

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

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



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

def reload(relative_path = nil, data = {}, options = {})
  if Helpers.blank?(relative_path) && Helpers.has_primary_key?(self)
    raise 'UnknownPath'
  end

  make_a_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



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

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

  method = new_record? ? :create : :update

  make_a_persistence_call(method, relative_path, resource_data, options) do |remote_call|
    @new_record = false if method == :create && remote_call.status

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

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



75
76
77
78
79
# File 'lib/smooth_operator/persistence.rb', line 75

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