module Redcord::Actions::ClassMethods

Public Instance Methods

count() click to toggle source
# File lib/redcord/actions.rb, line 29
def count
  Redcord::Base.trace(
   'redcord_actions_class_methods_count',
    model_name: name,
  ) do
    res = 0
    redis.scan_each_shard("#{model_key}:id:*") { res += 1 }
    res
  end
end
create!(args) click to toggle source
# File lib/redcord/actions.rb, line 41
def create!(args)
  Redcord::Base.trace(
   'redcord_actions_class_methods_create!',
    model_name: name,
  ) do
    self.props.keys.each { |attr_key| args[attr_key] = nil unless args.key?(attr_key) }
    args[:created_at] = args[:updated_at] = Time.zone.now
    instance = TypeCoerce[self].new.from(args)
    id = redis.create_hash_returning_id(
      model_key,
      to_redis_hash(args),
      ttl: _script_arg_ttl,
      index_attrs: _script_arg_index_attrs,
      range_index_attrs: _script_arg_range_index_attrs,
      custom_index_attrs: _script_arg_custom_index_attrs,
      hash_tag: instance.hash_tag,
    )
    instance.send(:id=, id)
    instance
  end
end
destroy(id) click to toggle source
# File lib/redcord/actions.rb, line 95
def destroy(id)
  Redcord::Base.trace(
   'redcord_actions_class_methods_destroy',
    model_name: name,
  ) do
    redis.delete_hash(
      model_key,
      id,
      index_attrs: _script_arg_index_attrs,
      range_index_attrs: _script_arg_range_index_attrs,
      custom_index_attrs: _script_arg_custom_index_attrs,
    ) == 1
  end
end
find(id) click to toggle source
# File lib/redcord/actions.rb, line 64
def find(id)
  Redcord::Base.trace(
   'redcord_actions_class_methods_find',
    model_name: name,
  ) do
    instance_key = "#{model_key}:id:#{id}"
    args = redis.hgetall(instance_key)
    if args.empty?
      raise Redcord::RecordNotFound, "Couldn't find #{name} with 'id'=#{id}"
    end

    coerce_and_set_id(args, id)
  end
end
find_by(args) click to toggle source
# File lib/redcord/actions.rb, line 80
def find_by(args)
  Redcord::Base.trace(
   'redcord_actions_class_methods_find_by_args',
    model_name: name,
  ) do
    where(args).to_a.first
  end
end
where(args) click to toggle source
# File lib/redcord/actions.rb, line 90
def where(args)
  Redcord::Relation.new(T.let(self, T.untyped)).where(args)
end