Module: Redcord::Actions::ClassMethods
- Extended by:
- T::Sig
- Defined in:
- lib/redcord/actions.rb
Instance Method Summary collapse
- #count ⇒ Object
- #create!(args) ⇒ Object
- #destroy(id) ⇒ Object
- #find(id) ⇒ Object
- #find_by(args) ⇒ Object
- #where(args) ⇒ Object
Instance Method Details
#count ⇒ Object
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 |