Class: ShotgridApiRuby::Entities::Params

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, T::Sig
Defined in:
lib/shotgrid_api_ruby/entities/params.rb

Defined Under Namespace

Classes: TooComplexFiltersError

Constant Summary collapse

SORT_TYPE =
T.type_alias do
  T.nilable(
    T.any(
      T::Hash[T.any(String, Symbol), T.any(String, Symbol)],
      T::Array[T.any(String, Symbol)],
      String,
      Symbol,
    ),
  )
end
PAGE_TYPE =
T.type_alias { T.nilable(T.any(String, Integer)) }
PAGE_SIZE_TYPE =
T.type_alias { T.nilable(T.any(String, Integer)) }
FIELDS_TYPE =
T.type_alias do
  T.nilable(T.any(String, Symbol, T::Array[T.any(String, Symbol)]))
end
LOGICAL_OPERATOR_TYPE =
T.type_alias { T.any(String, Symbol) }
FILTERS_FIELD_TYPE =
T.type_alias do
  T.nilable(
    T.any(
      T::Array[T.untyped],
      T::Hash[
        T.any(String, Symbol),
        T.any(
          String,
          Symbol,
          Integer,
          Float,
          T::Array[T.any(String, Symbol, Integer, Float)],
          T::Hash[
            T.any(String, Symbol),
            T.any(
              String,
              Symbol,
              Integer,
              Float,
              T::Array[T.any(String, Symbol, Integer, Float)],
              T.untyped,
            )
          ],
          T.untyped,
        )
      ],
      T::Hash[
        T.any(String, Symbol),
        T.any(
          String,
          Symbol,
          Integer,
          Float,
          T::Array[T.any(String, Symbol, Integer, Float)],
        )
      ],
    ),
  )
end
GROUPING_FIELD_TYPE =
T.type_alias do
  T.nilable(
    T.any(
      T::Array[T::Array[T.any(String, Symbol)]],
      T::Hash[
        String,
        T.any(
          String,
          Symbol,
          { type: String },
          { 'type' => String },
          { direction: String },
          { 'direction' => String },
          { type: String, direction: String },
          { :type => String, 'direction' => String },
          { 'type' => String, :direction => String },
          { 'type' => String, 'direction' => String },
        )
      ],
    ),
  )
end
SUMMARY_FILEDS_TYPE =
T.type_alias do
  T.nilable(
    T.any(
      T::Array[
        T::Hash[
          T.any(String, Symbol),
          T::Hash[T.any(String, Symbol), T.any(String, Symbol)]
        ]
      ],
      T::Hash[T.any(String, Symbol), T.any(String, Symbol)],
    ),
  )
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParams

Returns a new instance of Params.



14
15
16
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 14

def initialize
  @parsed_params = T.let({}, T::Hash[T.any(String, Symbol), T.untyped])
end

Class Method Details

.filters_are_simple?(filters) ⇒ Boolean

Returns:

  • (Boolean)


302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 302

def self.filters_are_simple?(filters)
  return false unless filters
  return false if filters.is_a? Array

  return false if filters[:conditions] || filters['conditions']

  filters.values.all? do |filter_val|
    (
      filter_val.is_a?(Integer) || filter_val.is_a?(String) ||
        filter_val.is_a?(Symbol)
    ) ||
      (
        filter_val.is_a?(Array) &&
          filter_val.all? do |val|
            val.is_a?(String) || val.is_a?(Symbol) || val.is_a?(Integer)
          end
      )
  end
end

Instance Method Details

#add_fields(fields) ⇒ Object



69
70
71
72
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 69

def add_fields(fields)
  @parsed_params[:fields] =
    fields && !fields.empty? ? [fields].flatten.join(',') : '*'
end

#add_filter(filters, logical_operator = 'and') ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 156

def add_filter(filters, logical_operator = 'and')
  return unless filters

  # cast are here because Sorbet is confused by the madness filters can be
  @parsed_params[:filter] =
    if (self.class.filters_are_simple?(filters))
      translate_simple_filters_to_sg(
        T.cast(
          filters,
          T::Hash[
            T.any(String, Symbol),
            T.any(
              String,
              Symbol,
              Integer,
              Float,
              T::Array[T.any(String, Symbol, Integer, Float)],
            )
          ],
        ),
      )
    elsif filters.is_a? Hash
      filters =
        T.cast(
          T.unsafe(filters),
          T::Hash[
            T.any(String, Symbol),
            T.any(
              String,
              Symbol,
              Integer,
              Float,
              T::Array[T.any(String, Symbol, Integer, Float)],
              T::Hash[
                T.any(String, Symbol),
                T.any(
                  String,
                  Symbol,
                  Integer,
                  Float,
                  T::Array[T.any(String, Symbol, Integer, Float)],
                  T.untyped,
                )
              ],
              T.untyped,
            )
          ],
        )
      {
        conditions:
          filters[:conditions] || filters['conditions'] ||
            translate_complex_filters_to_sg(filters),
        logical_operator:
          filters[:logical_operator] || filters['logical_operator'] ||
            logical_operator.to_s,
      }
    else
      { conditions: filters, logical_operator: logical_operator.to_s }
    end
end

#add_grouping(grouping) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 241

def add_grouping(grouping)
  return unless grouping

  if grouping.is_a? Array
    @parsed_params[:grouping] = grouping
    return
  end

  @parsed_params[:grouping] =
    grouping.each_with_object([]) do |(key, options), result|
      if options.is_a? Hash
        result << {
          field: key.to_s,
          type: options[:type]&.to_s || options['type']&.to_s || 'exact',
          direction:
            options[:direction]&.to_s || options['direction']&.to_s ||
              'asc',
        }
      else
        result << {
          field: key.to_s,
          type: 'exact',
          direction: options.to_s,
        }
      end
    end
end

#add_options(return_only, include_archived_projects) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 85

def add_options(return_only, include_archived_projects)
  return if return_only.nil? && include_archived_projects.nil?

  @parsed_params[:options] = {
    return_only: return_only ? 'retired' : 'active',
    include_archived_projects: !!include_archived_projects,
  }
end

#add_page(page, page_size) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 54

def add_page(page, page_size)
  return unless page || page_size

  page = page.to_i if page
  page_size = page_size.to_i if page_size

  page = 1 if page && page < 1
  @parsed_params[:page] = { size: page_size || 20, number: page || 1 }
end

#add_sort(sort) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 32

def add_sort(sort)
  return unless sort

  @parsed_params[:sort] =
    if sort.is_a?(Hash)
      sort
        .map do |field, direction|
          "#{direction.to_s.start_with?('desc') ? '-' : ''}#{field}"
        end
        .join(',')
    else
      [sort].flatten.join(',')
    end
end

#add_summary_fields(summary_fields) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/shotgrid_api_ruby/entities/params.rb', line 287

def add_summary_fields(summary_fields)
  return unless summary_fields

  if summary_fields.is_a? Array
    @parsed_params[:summary_fields] = summary_fields
    return
  end

  if summary_fields.is_a? Hash
    @parsed_params[:summary_fields] =
      summary_fields.map { |k, v| { field: k.to_s, type: v.to_s } }
  end
end