class ActiveShotgun::Query

Public Class Methods

new(type:, klass:) click to toggle source
# 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

Public Instance Methods

all() click to toggle source
# 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
Also aliased as: to_a
find_by(conditions) click to toggle source
# File lib/active_shotgun/query.rb, line 97
def find_by(conditions)
  where(conditions).first
end
first(number = 1) click to toggle source
# 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) click to toggle source
# File lib/active_shotgun/query.rb, line 49
def limit(number)
  @limit = number
  self
end
offset(number) click to toggle source
# File lib/active_shotgun/query.rb, line 54
def offset(number)
  @offset = number
  self
end
orders(new_orders) click to toggle source
# File lib/active_shotgun/query.rb, line 101
def orders(new_orders)
  @orders = new_orders
  self
end
pluck(*fields) click to toggle source
# 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) click to toggle source
# File lib/active_shotgun/query.rb, line 106
def select(*fields)
  @fields = fields.flatten
  self
end
to_a()
Alias for: all
where(conditions) click to toggle source
# 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

Private Instance Methods

format_page_from_limit_and_offset(limit, offset) click to toggle source
# File lib/active_shotgun/query.rb, line 125
def format_page_from_limit_and_offset(limit, offset)
  min = (offset + 1).to_f
  max = (offset + limit).to_f
  limit.upto(limit + offset) do |size|
    next unless (min / size).ceil == (max / size).ceil

    page = (min / size).ceil
    start_trim = min - (page - 1) * size - 1
    end_trim = page * size - max
    return {
      page_size: [size, 1000].min,
      page: page,
      start_trim: start_trim.to_i,
      end_trim: end_trim.to_i,
    }
  end
end
shotgun_client() click to toggle source
# File lib/active_shotgun/query.rb, line 143
def shotgun_client
  Client.shotgun.entities(@type)
end
translate_hash_contitions_to_array(hash_conditions) click to toggle source
# File lib/active_shotgun/query.rb, line 119
def translate_hash_contitions_to_array(hash_conditions)
  hash_conditions.map do |k, v|
    [k, "is", v]
  end
end