Module: Redcord::Actions::InstanceMethods

Extended by:
T::Helpers, T::Sig
Defined in:
lib/redcord/actions.rb

Instance Method Summary collapse

Instance Method Details

#_set_args!(args) ⇒ Object



246
247
248
249
250
# File 'lib/redcord/actions.rb', line 246

def _set_args!(args)
  args.each do |key, value|
    send(:"#{key}=", value)
  end
end

#created_atObject



118
# File 'lib/redcord/actions.rb', line 118

def created_at; end

#created_at=(time) ⇒ Object



125
# File 'lib/redcord/actions.rb', line 125

def created_at=(time); end

#destroyObject



229
230
231
232
233
234
235
236
237
238
# File 'lib/redcord/actions.rb', line 229

def destroy
  Redcord::Base.trace(
   'redcord_actions_instance_methods_destroy',
    model_name: self.class.name,
  ) do
    return false if id.nil?

    self.class.destroy(T.must(id))
  end
end

#idObject



253
254
255
# File 'lib/redcord/actions.rb', line 253

def id
  instance_variable_get(:@_id)
end

#instance_keyObject



241
242
243
# File 'lib/redcord/actions.rb', line 241

def instance_key
  "#{self.class.model_key}:id:#{T.must(id)}"
end

#saveObject



177
178
179
180
181
182
183
184
# File 'lib/redcord/actions.rb', line 177

def save
  save!

  true
rescue Redis::CommandError
  # TODO: break down Redis::CommandError by parsing the error message
  false
end

#save!Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/redcord/actions.rb', line 138

def save!
  Redcord::Base.trace(
   'redcord_actions_instance_methods_save!',
    model_name: self.class.name,
  ) do
    self.updated_at = Time.zone.now
    _id = id
    if _id.nil?
      serialized_instance = serialize
      self.class.props.keys.each do |attr_key|
        serialized_instance[attr_key.to_s] = nil unless serialized_instance.key?(attr_key.to_s) 
      end
      self.created_at = T.must(self.updated_at)
      _id = redis.create_hash_returning_id(
        self.class.model_key,
        self.class.to_redis_hash(serialized_instance),
        ttl: self.class._script_arg_ttl,
        index_attrs: self.class._script_arg_index_attrs,
        range_index_attrs: self.class._script_arg_range_index_attrs,
        custom_index_attrs: self.class._script_arg_custom_index_attrs,
        hash_tag: hash_tag,
      )
      send(:id=, _id)
    else
      redis.update_hash(
        self.class.model_key,
        _id,
        self.class.to_redis_hash(serialize),
        ttl: self.class._script_arg_ttl,
        index_attrs: self.class._script_arg_index_attrs,
        range_index_attrs: self.class._script_arg_range_index_attrs,
        custom_index_attrs: self.class._script_arg_custom_index_attrs,
        hash_tag: hash_tag,
      )
    end
  end
end

#update(args) ⇒ Object



219
220
221
222
223
224
225
226
# File 'lib/redcord/actions.rb', line 219

def update(args)
  update!(args)

  true
rescue Redis::CommandError
  # TODO: break down Redis::CommandError by parsing the error message
  false
end

#update!(args) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/redcord/actions.rb', line 187

def update!(args)
  Redcord::Base.trace(
   'redcord_actions_instance_methods_update!',
    model_name: self.class.name,
  ) do
    shard_by_attr = self.class.shard_by_attribute
    if args.keys.include?(shard_by_attr)
      raise Redcord::InvalidAction, "Cannot update shard_by attribute #{shard_by_attr}"
    end

    _id = id
    if _id.nil?
      _set_args!(args)
      save!
    else
      args[:updated_at] = Time.zone.now
      _set_args!(args)
      redis.update_hash(
        self.class.model_key,
        _id,
        self.class.to_redis_hash(args),
        ttl: self.class._script_arg_ttl,
        index_attrs: self.class._script_arg_index_attrs,
        range_index_attrs: self.class._script_arg_range_index_attrs,
        custom_index_attrs: self.class._script_arg_custom_index_attrs,
        hash_tag: hash_tag,
      )
    end
  end
end

#updated_atObject



128
# File 'lib/redcord/actions.rb', line 128

def updated_at; end

#updated_at=(time) ⇒ Object



135
# File 'lib/redcord/actions.rb', line 135

def updated_at=(time); end