Module: Yapper::Document::Persistence

Extended by:
MotionSupport::Concern
Included in:
Yapper::Document
Defined in:
lib/yapper/document/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attrs, options = {}) ⇒ Object Also known as: attributes=



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/yapper/document/persistence.rb', line 71

def assign_attributes(attrs, options={})
  self.attributes ||= {}

  if options[:pristine]
    self.attributes = {}
  end

  attrs.each do |k,v|
    if respond_to?("#{k}=")
      __send__("#{k}=", v) unless v.nil?
    else
      Log.warn "#{k} not defined on #{self.class}"
    end
  end
end

#destroy(options = {}) ⇒ Object



144
145
146
147
# File 'lib/yapper/document/persistence.rb', line 144

def destroy(options={})
  db.execute { |txn| txn.removeObjectForKey(self.id, inCollection: _type) }
  @destroyed = true
end

#destroyed?Boolean

Returns:



112
113
114
# File 'lib/yapper/document/persistence.rb', line 112

def destroyed?
  !!@destroyed
end

#initialize(attrs = {}, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/yapper/document/persistence.rb', line 47

def initialize(attrs={}, options={})
  super

  @new_record = options[:new].nil? ? true : options[:new]
  @changes = {}
  @queued_saves = []

  assign_attributes({:id => generate_id}, options) if @new_record
  assign_attributes(attrs, options)

  set_defaults

  if options[:pristine]
    self.changes = {}
  end

  self
end

#new_record?Boolean

Returns:



104
105
106
# File 'lib/yapper/document/persistence.rb', line 104

def new_record?
  @new_record
end

#persisted?Boolean

Returns:



116
117
118
# File 'lib/yapper/document/persistence.rb', line 116

def persisted?
  !new_record? && !destroyed?
end

#reloadObject



98
99
100
101
102
# File 'lib/yapper/document/persistence.rb', line 98

def reload
  reloaded = self.class.find(self.id)
  self.assign_attributes(reloaded.attributes, :pristine => true)
  self
end

#save(options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/yapper/document/persistence.rb', line 120

def save(options={})
  db.execute("#{self.model_name}:save" => self) do |txn|
    @queued_saves.each { |queued, queued_options| queued.save(queued_options) }

    run_callbacks 'save' do
      txn.setObject(stringify_keys(attributes), forKey: self.id, inCollection: _type)

      @was_new = @new_record
      @new_record = false
      @queued_saves = []

      self.previous_changes = self.changes
      self.changes = {}
    end

    unless options[:embedded]
      # XXX Use middleware pattern instead of this ugliness
      sync_changes if defined? sync_changes
    end
  end

  true
end

#set_attribute(name, value) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/yapper/document/persistence.rb', line 88

def set_attribute(name, value)
  if self.class.fields[name][:type] == Time
    value = Time.parse(value) unless value.is_a?(Time)
  end
  # XXX This should not be set if the object was created from a
  # selection
  @changes[name.to_s] = value
  self.attributes[name] = value
end

#update_attributes(attrs, options = {}) ⇒ Object



66
67
68
69
# File 'lib/yapper/document/persistence.rb', line 66

def update_attributes(attrs, options={})
  self.assign_attributes(attrs, options)
  self.save(options)
end

#was_new?Boolean

Returns:



108
109
110
# File 'lib/yapper/document/persistence.rb', line 108

def was_new?
  @was_new
end