Class: MtkFramework::ActiveInteractionParams::ParametrizeFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/mtk_framework/active_interaction_params/parametrize_filter.rb

Constant Summary collapse

TYPES_MAPPING =
{
  'Integer' => Integer,
  'Float' => Float,
  'Boolean' => Grape::API::Boolean,
  'String' => String,
  'Symbol' => Symbol,
  'Date' => Date,
  'DateTime' => DateTime,
  'Time' => Time,
  'File' => File,
  'Hash' => Hash,
  'Array' => Array,
  'TzSecretKey' => String,
  'TzAccount' => String,
  'TzContractAddress' => String,
  'TzPublicKey' => String,
  'TzSignature' => String
}.freeze

Class Method Summary collapse

Class Method Details

.array_grape_filter_type(filter) ⇒ Object

returns Array[<File | Integer …>] if not array of hashes else return Array, because in this case we use nested params with a block



66
67
68
69
70
71
72
73
74
# File 'lib/mtk_framework/active_interaction_params/parametrize_filter.rb', line 66

def array_grape_filter_type(filter)
  child_filter = filter.filters.values.first

  unless child_filter.is_a? ActiveInteraction::HashFilter
    return Array[grape_filter_type(child_filter)]
  end

  Array
end

.array_hash_filters?(filter, child_filter) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mtk_framework/active_interaction_params/parametrize_filter.rb', line 89

def array_hash_filters?(filter, child_filter)
  filter.is_a?(ActiveInteraction::ArrayFilter) && child_filter.is_a?(ActiveInteraction::HashFilter)
end

.call(filter, params_scope) ⇒ Object

TODO: handle the case of Object, Record, Interface(?)



27
28
29
30
31
32
33
34
# File 'lib/mtk_framework/active_interaction_params/parametrize_filter.rb', line 27

def call(filter, params_scope)
  unless nestable_filter?(filter)
    return params_scope.send(grape_method(filter), filter.name,
                             **grape_options(filter))
  end

  parametrize_nestable(filter, params_scope)
end

.grape_filter_type(filter) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/mtk_framework/active_interaction_params/parametrize_filter.rb', line 54

def grape_filter_type(filter)
  if filter.is_a? ActiveInteraction::ArrayFilter
    return array_grape_filter_type(filter)
  end

  filter_name = filter.class.name.split('::').second.split('Filter').first

  TYPES_MAPPING.fetch(filter_name)
end

.grape_method(filter) ⇒ Object



85
86
87
# File 'lib/mtk_framework/active_interaction_params/parametrize_filter.rb', line 85

def grape_method(filter)
  filter.options.key?(:default) ? :optional : :requires
end

.grape_options(filter) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/mtk_framework/active_interaction_params/parametrize_filter.rb', line 76

def grape_options(filter)
  grape_options = { desc: filter.options.fetch(:desc, ''), type: grape_filter_type(filter) }
  if filter.options[:default]
    grape_options[:default] = filter.options[:default]
  end

  grape_options
end

.nestable_filter?(filter) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mtk_framework/active_interaction_params/parametrize_filter.rb', line 93

def nestable_filter?(filter)
  unless filter.is_a? ActiveInteraction::ArrayFilter
    return filter.filters.any?
  end

  # this case is not supposed to happen since ActiveInteraction only allows one type in the case of an Array
  raise NotImplementedError unless filter.filters.count == 1

  # if it is an array of hashes => we use nested params with a block
  # else we use types like Array[String] => not nestable
  filter.filters.values.first.is_a? ActiveInteraction::HashFilter
end

.parametrize_nestable(filter, params_scope) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mtk_framework/active_interaction_params/parametrize_filter.rb', line 36

def parametrize_nestable(filter, params_scope)
  current_filter = filter

  params_scope.send(grape_method(current_filter), current_filter.name,
                    **grape_options(current_filter)) do
    current_filter.filters.each do |_, child_filter|
      unless ParametrizeFilter.array_hash_filters?(current_filter, child_filter)
        ParametrizeFilter.call(child_filter, self)
      end

      # skip the hash require in the case of an array of hashes (AI and grape handle this case differently)
      child_filter.filters.each do |_, small_child_filter|
        ParametrizeFilter.call(small_child_filter, self)
      end
    end
  end
end