Module: RedisModelExtension::ClassGetFind

Defined in:
lib/redis-model-extension/get_find.rb

Overview

Get & Find

  • Model.all => Array of all instances

  • Model.find(1) => Array of one instance with id 1

  • Model.get(1) => Array of one instance with id 1

  • Model.find( id: 1 ) => Array of one instance with id 1

  • Model.find( field: “test” ) => Array of all instances with field == test [field must be in redis key]

Instance Method Summary collapse

Instance Method Details

#find(args = {}) ⇒ Object Also known as: all

Find method for searching in redis

  • args (Integer) - search by id

  • args (Hash) - search by arguments in redis_key



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/redis-model-extension/get_find.rb', line 19

def find(args = {})
  # when argument is integer - search by id 
  args = { id: args } if args.is_a?(Integer)
  #normalize input hash of arguments
  args = HashWithIndifferentAccess.new(args)

  out = []
  klass = self.name.constantize
  search_key = klass.generate_key(args)
  #is key specified directly? -> no needs of looking for other keys! -> faster
  unless search_key =~ /\*/
    out << klass.new_by_key(search_key) if klass.exists?(args)
  else
    RedisModelExtension::Database.redis.keys(search_key).each do |key|
      out << klass.new_by_key(key) 
    end
  end
  out
end

#find_by_alias(alias_name, args = {}) ⇒ Object

Find method for searching in redis

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/redis-model-extension/get_find.rb', line 41

def find_by_alias(alias_name, args = {})
  #check if asked dynamic alias exists
  raise ArgumentError, "Unknown dynamic alias: '#{alias_name}', use: #{redis_alias_config.keys.join(", ")} " unless redis_alias_config.has_key?(alias_name.to_sym)

  #normalize input hash of arguments
  args = HashWithIndifferentAccess.new(args)

  out = []
  klass = self.name.constantize
  search_key = klass.generate_alias_key(alias_name, args)
  #is key specified directly? -> no needs of looking for other keys! -> faster
  unless search_key =~ /\*/
    out = klass.get_by_alias(alias_name, args) if klass.alias_exists?(alias_name, args)
  else
    RedisModelExtension::Database.redis.keys(search_key).each do |key|
      out << klass.get_by_alias_key(key)
    end
  end
  out.flatten
end

#get(args = {}) ⇒ Object

fastest method to get object from redis by getting it by arguments

  • args (Integer) - search by id

  • args (Hash) - search by arguments in redis_key



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/redis-model-extension/get_find.rb', line 69

def get(args = {})
  # when argument is integer - search by id 
  args = { id: args } if args.is_a?(Integer)

  #normalize input hash of arguments
  args = HashWithIndifferentAccess.new(args)

  klass = self.name.constantize
  if klass.valid_key?(args) && klass.exists?(args)
    klass.new_by_key(klass.generate_key(args)) 
  else
    nil
  end
end

#get_by_alias(alias_name, args = {}) ⇒ Object

fastest method to get object from redis by getting it by dynamic alias and arguments

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/redis-model-extension/get_find.rb', line 89

def get_by_alias(alias_name, args = {})
  #check if asked dynamic alias exists
  raise ArgumentError, "Unknown dynamic alias: '#{alias_name}', use: #{redis_alias_config.keys.join(", ")} " unless redis_alias_config.has_key?(alias_name.to_sym)
  
  #normalize input hash of arguments
  args = HashWithIndifferentAccess.new(args)

  klass = self.name.constantize
  if klass.valid_alias_key?(alias_name, args) && klass.alias_exists?(alias_name, args)
    out = []
    RedisModelExtension::Database.redis.smembers(klass.generate_alias_key(alias_name, args)).each do |key|
      out << klass.new_by_key(key) if RedisModelExtension::Database.redis.exists(key)
    end
    return out
  end
  nil
end

#get_by_alias_key(alias_key) ⇒ Object

fastest method to get object from redis by getting it by alias and arguments



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/redis-model-extension/get_find.rb', line 123

def get_by_alias_key(alias_key)
  klass = self.name.constantize
  if RedisModelExtension::Database.redis.exists(alias_key)
    out = []
    RedisModelExtension::Database.redis.smembers(alias_key).each do |key|
      out << klass.new_by_key(key) if RedisModelExtension::Database.redis.exists(key)
    end
    return out
  end
  nil
end

#get_by_redis_key(redis_key) ⇒ Object

if you know redis key and would like to get object



113
114
115
116
117
118
119
120
# File 'lib/redis-model-extension/get_find.rb', line 113

def get_by_redis_key(redis_key)
  if redis_key.is_a?(String) && RedisModelExtension::Database.redis.exists(redis_key)
    klass = self.name.constantize
    klass.new_by_key(redis_key)
  else
    nil
  end
end

#new_by_key(key) ⇒ Object

read all data from redis and create new instance (used for Find & Get method)



140
141
142
143
144
145
146
147
# File 'lib/redis-model-extension/get_find.rb', line 140

def new_by_key(key)
  args = RedisModelExtension::Database.redis.hgetall(key).symbolize_keys

  new_instance = new(args)
  new_instance.store_keys

  return new_instance
end