Module: FakeDynamo::Filter

Includes:
Validation
Included in:
Table
Defined in:
lib/fake_dynamo/filter.rb

Constant Summary collapse

INF =
1.0/0.0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#add_errors, #api_config, #api_config_path, #api_input_spec, #available_operations, #param, #validate!, #validate_input, #validate_key_data, #validate_key_schema, #validate_operation, #validate_payload, #validate_request_size, #validate_spec, #validate_type

Class Method Details

.def_filter(name, size, supported_types, &comparator) ⇒ Object



46
47
48
49
50
# File 'lib/fake_dynamo/filter.rb', line 46

def self.def_filter(name, size, supported_types, &comparator)
  define_method "#{name}_filter" do |value_list, target_attribute, fail_on_type_mismatch|
    comparison_filter(value_list, size, target_attribute, fail_on_type_mismatch, supported_types, comparator)
  end
end

Instance Method Details

#comparison_filter(value_list, size, target_attribute, fail_on_type_mismatch, supported_types, comparator) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fake_dynamo/filter.rb', line 5

def comparison_filter(value_list, size, target_attribute, fail_on_type_mismatch, supported_types, comparator)
  return false if target_attribute.nil?

  validate_size(value_list, size)

  if fail_on_type_mismatch
    value_list.each do |value|
      validate_type(value, target_attribute)
    end
  end

  value_attribute_list = value_list.map do |value|
    value_attribute = Attribute.from_hash(target_attribute.name, value)
    validate_supported_types(value_attribute, supported_types)
    value_attribute
  end

  value_attribute_list.each do |value_attribute|
    return false if target_attribute.type != value_attribute.type
  end

  if target_attribute.type == 'N'
    comparator.call(target_attribute.value.to_f, *value_attribute_list.map(&:value).map(&:to_f))
  else
    comparator.call(target_attribute.value, *value_attribute_list.map(&:value))
  end
end

#contains_filter(value_list, target_attribute, fail_on_type_mismatch) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fake_dynamo/filter.rb', line 69

def contains_filter(value_list, target_attribute, fail_on_type_mismatch)
  return false if target_attribute.nil?

  validate_size(value_list, 1)
  value_attribute = Attribute.from_hash(target_attribute.name, value_list.first)
  validate_supported_types(value_attribute, ['N', 'S'])

  if ((value_attribute.type == 'S' and
       (target_attribute.type == 'S' or target_attribute.type == 'SS')) or
      (value_attribute.type == 'N' and target_attribute.type == 'NS'))
    target_attribute.value.include?(value_attribute.value)
  end
end

#in_filter(value_list, target_attribute, fail_on_type_mismatch) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fake_dynamo/filter.rb', line 99

def in_filter(value_list, target_attribute, fail_on_type_mismatch)
  return false if target_attribute.nil?

  validate_size(value_list, (1..INF))

  value_attribute_list = value_list.map do |value|
    value_attribute = Attribute.from_hash(target_attribute.name, value)
    validate_supported_types(value_attribute, ['N', 'S'])
    value_attribute
  end

  value_attribute_list.each do |value_attribute|
    return true if value_attribute == target_attribute
  end

  false
end

#not_contains_filter(value_list, target_attribute, fail_on_type_mismatch) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fake_dynamo/filter.rb', line 83

def not_contains_filter(value_list, target_attribute, fail_on_type_mismatch)
  return false if target_attribute.nil?

  validate_size(value_list, 1)
  value_attribute = Attribute.from_hash(target_attribute.name, value_list.first)
  validate_supported_types(value_attribute, ['N', 'S'])

  if ((value_attribute.type == 'S' and
       (target_attribute.type == 'S' or target_attribute.type == 'SS')) or
      (value_attribute.type == 'N' and target_attribute.type == 'NS'))
    !target_attribute.value.include?(value_attribute.value)
  end
end

#not_null_filter(value_list, target_attribute, fail_on_type_mismatch) ⇒ Object



61
62
63
# File 'lib/fake_dynamo/filter.rb', line 61

def not_null_filter(value_list, target_attribute,  fail_on_type_mismatch)
  not target_attribute.nil?
end

#null_filter(value_list, target_attribute, fail_on_type_mismatch) ⇒ Object



65
66
67
# File 'lib/fake_dynamo/filter.rb', line 65

def null_filter(value_list, target_attribute, fail_on_type_mismatch)
  target_attribute.nil?
end

#validate_size(value_list, size) ⇒ Object



39
40
41
42
43
44
# File 'lib/fake_dynamo/filter.rb', line 39

def validate_size(value_list, size)
  if (size.kind_of? Range and (not (size.include? value_list.size))) or
      (size.kind_of? Integer and value_list.size != size)
    raise ValidationException, "The attempted filter operation is not supported for the provided filter argument count"
  end
end

#validate_supported_types(value_attribute, supported_types) ⇒ Object



33
34
35
36
37
# File 'lib/fake_dynamo/filter.rb', line 33

def validate_supported_types(value_attribute, supported_types)
  unless supported_types.include? value_attribute.type
    raise ValidationException, "The attempted filter operation is not supported for the provided type"
  end
end