Module: Redcord::Actions::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#countObject



29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



80
81
82
83
84
85
86
87
# 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) ⇒ Object



90
91
92
# File 'lib/redcord/actions.rb', line 90

def where(args)
  Redcord::Relation.new(T.let(self, T.untyped)).where(args)
end