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, #key_schema_mismatch, #param, #validate!, #validate_hash_condition, #validate_hash_key, #validate_index_names, #validate_input, #validate_key_data, #validate_key_schema, #validate_operation, #validate_payload, #validate_projection, #validate_range_condition, #validate_range_key, #validate_request_size, #validate_spec, #validate_type

Class Method Details

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



42
43
44
45
46
# File 'lib/fake_dynamo/filter.rb', line 42

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

#comparable_types?(value_type, target_type) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/fake_dynamo/filter.rb', line 65

def comparable_types?(value_type, target_type)
  ((value_type == 'S' and
    (target_type == 'S' or target_type == 'SS')) or
   (value_type == 'N' and target_type == 'NS')  or
   (value_type == 'B' and
    (target_type == 'B' or target_type == 'BS')))
end

#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
# 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

  comparator.call(target_attribute.value, *value_attribute_list.map(&:value))
end

#contains_filter(value_list, target_attribute, fail_on_type_mismatch) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fake_dynamo/filter.rb', line 73

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', 'B'])

  if comparable_types?(value_attribute.type, target_attribute.type)
    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', 'B'])
    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



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

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', 'B'])

  if comparable_types?(value_attribute.type, target_attribute.type)
    !target_attribute.value.include?(value_attribute.value)
  end
end

#not_null_filter(value_list, target_attribute, fail_on_type_mismatch) ⇒ Object



57
58
59
# File 'lib/fake_dynamo/filter.rb', line 57

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



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

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

#validate_size(value_list, size) ⇒ Object



35
36
37
38
39
40
# File 'lib/fake_dynamo/filter.rb', line 35

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



29
30
31
32
33
# File 'lib/fake_dynamo/filter.rb', line 29

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