Class: ActiveShotgun::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/active_shotgun/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(type:, klass:) ⇒ Query

Returns a new instance of Query.



5
6
7
8
9
10
11
12
13
# File 'lib/active_shotgun/query.rb', line 5

def initialize(type:, klass:)
  @type = type
  @klass = klass.is_a?(String) ? klass.constantize : klass
  @conditions = nil
  @limit = 100
  @offset = 0
  @orders = nil
  @fields = nil
end

Instance Method Details

#allObject Also known as: to_a



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_shotgun/query.rb', line 24

def all
  page_calc = format_page_from_limit_and_offset(@limit, @offset)
  results = shotgun_client.all(
    fields: @fields,
    sort: @orders,
    filter: @conditions,
    page: page_calc[:page],
    page_size: page_calc[:page_size]
  )
  results.pop(page_calc[:end_trim])
  results.shift(page_calc[:start_trim])

  results.map{ |result| @klass.parse_shotgun_results(result) }
end

#find_by(conditions) ⇒ Object



97
98
99
# File 'lib/active_shotgun/query.rb', line 97

def find_by(conditions)
  where(conditions).first
end

#first(number = 1) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/active_shotgun/query.rb', line 40

def first(number = 1)
  results = limit(number).all
  if @limit == 1
    results.first
  else
    results
  end
end

#limit(number) ⇒ Object



49
50
51
52
# File 'lib/active_shotgun/query.rb', line 49

def limit(number)
  @limit = number
  self
end

#offset(number) ⇒ Object



54
55
56
57
# File 'lib/active_shotgun/query.rb', line 54

def offset(number)
  @offset = number
  self
end

#orders(new_orders) ⇒ Object



101
102
103
104
# File 'lib/active_shotgun/query.rb', line 101

def orders(new_orders)
  @orders = new_orders
  self
end

#pluck(*fields) ⇒ Object



111
112
113
114
115
# File 'lib/active_shotgun/query.rb', line 111

def pluck(*fields)
  fields.flatten!
  result = select(fields).map{ |e| fields.map{ |field| e.public_send(field) } }
  fields.size == 1 ? result.flatten : result
end

#select(*fields) ⇒ Object



106
107
108
109
# File 'lib/active_shotgun/query.rb', line 106

def select(*fields)
  @fields = fields.flatten
  self
end

#where(conditions) ⇒ Object



59
60
61
62
63
64
65
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/active_shotgun/query.rb', line 59

def where(conditions)
  @conditions ||= {}
  @conditions =
    case conditions
    when Hash
      case @conditions
      when Hash
        @conditions.merge(conditions)
      when Array
        @conditions + translate_hash_contitions_to_array(conditions)
      else
        raise "Unknow type. Please use Hash or Array conditions"
      end
    when Array
      case @conditions
      when Hash
        translate_hash_contitions_to_array(@conditions) +
        if conditions.first.is_a? Array
          conditions
        else
          [conditions]
        end
      when Array
        @conditions +
        if conditions.first.is_a? Array
          conditions
        else
          [conditions]
        end
      else
        raise "Unknow type. Please use Hash or Array conditions"
      end
    else
      @conditions
    end
  self
end