Class: ShotgunApiRuby::Entities::Params

Inherits:
Hash
  • Object
show all
Defined in:
lib/shotgun_api_ruby/entities/params.rb

Defined Under Namespace

Classes: TooComplexFiltersError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filters_are_simple?(filters) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/shotgun_api_ruby/entities/params.rb', line 111

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

  if filters.is_a?(Hash) &&
       (filters[:conditions] || filters['conditions'])
    return false
  end

  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



32
33
34
35
# File 'lib/shotgun_api_ruby/entities/params.rb', line 32

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

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/shotgun_api_ruby/entities/params.rb', line 46

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

  self[:filter] =
    if (self.class.filters_are_simple?(filters))
      translate_simple_filters_to_sg(filters)
    elsif filters.is_a? Hash
      {
        conditions:
          filters[:conditions] || filters['conditions'] ||
            translate_complex_filters_to_sg(filters),
        logical_operator:
          filters[:logical_operator] || filters['logical_operator'] ||
            logical_operator,
      }
    else
      { conditions: filters, logical_operator: logical_operator }
    end
end

#add_grouping(grouping) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shotgun_api_ruby/entities/params.rb', line 66

def add_grouping(grouping)
  return unless grouping

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

  self[: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



37
38
39
40
41
42
43
44
# File 'lib/shotgun_api_ruby/entities/params.rb', line 37

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

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

#add_page(page, page_size) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/shotgun_api_ruby/entities/params.rb', line 22

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
  self[:page] = { size: page_size || 20, number: page || 1 }
end

#add_sort(sort) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/shotgun_api_ruby/entities/params.rb', line 9

def add_sort(sort)
  return unless sort

  self[: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



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/shotgun_api_ruby/entities/params.rb', line 97

def add_summary_fields(summary_fields)
  return unless summary_fields

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

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