Module: RedisModelExtension::ClassValidations

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

Instance Method Summary collapse

Instance Method Details

#valid_alias_key?(alias_name, args = {}) ⇒ Boolean

Validates if key by alias name and arguments is valid (all needed fields are not nil!)

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/redis-model-extension/validation.rb', line 21

def valid_alias_key? alias_name, args = {}
  raise ArgumentError, "Unknown dynamic alias, 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)

  config = redis_alias_config[alias_name.to_sym]


  # use all specified keys
  config[:main_fields].each do |key|
    return false unless valid_item_for_redis_key? args, key
  end

  # is dynamic alias?
    if config[:order_field] && config[:args_field]
    #check if input arguments has order field
    if args.has_key?(config[:order_field]) && args[config[:order_field]] && args.has_key?(config[:args_field]) && args[config[:args_field]]
      #use filed order from defined field in args
      args[config[:order_field]].each do |key|
        return false unless valid_item_for_redis_key? args[config[:args_field]], key
      end
    else 
      return false
    end
  end

  return true
end

#valid_item_for_redis_key?(args, key) ⇒ Boolean

validate one item of redis key

Returns:

  • (Boolean)


52
53
54
# File 'lib/redis-model-extension/validation.rb', line 52

def valid_item_for_redis_key? args, key
  (args.has_key?(key) && !args[key].nil?) || redis_fields_config[key] == :autoincrement
end

#valid_key?(args = {}) ⇒ Boolean

Validates if key by arguments is valid (all needed fields are not nil!)

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
# File 'lib/redis-model-extension/validation.rb', line 8

def valid_key? args = {}
  #normalize input hash of arguments
  args = HashWithIndifferentAccess.new(args)

  redis_key_config.each do |key|
    return false unless valid_item_for_redis_key? args, key
  end
  return true
end