class Syncano::ActiveRecord::ScopeBuilder

ScopeBuilder class allows for creating and chaining more complex queries

Attributes

model[RW]
parameters[RW]
scopes[RW]

Public Class Methods

new(model) click to toggle source

Constructor for ScopeBuilder @param [Class] model

# File lib/syncano/active_record/scope_builder.rb, line 7
def initialize(model)
  raise 'Model should be a class extending module Syncano::ActiveRecord::Base' unless model <= Syncano::ActiveRecord::Base

  self.model = model
  self.parameters = {}
end

Private Class Methods

where_mapping() click to toggle source

Returns mapping for operators @return [Hash]

# File lib/syncano/active_record/scope_builder.rb, line 148
def self.where_mapping
  { '=' => 'eq', '!=' => 'neq', '<>' => 'neq', '>=' => 'gte', '>' => 'gt', '<=' => 'lte', '<' => 'lt' }
end

Public Instance Methods

all() click to toggle source

Returns collection of objects @return [Array]

# File lib/syncano/active_record/scope_builder.rb, line 16
def all
  folder.data_objects.all(parameters).collect do |data_object|
    model.new(data_object)
  end
end
before(id) click to toggle source

Adds to the current scope builder condition for filtering by ids older than provided @param [Integer] id @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/scope_builder.rb, line 117
def before(id)
  self.parameters[:max_id] = id
  self
end
by_parent_id(parent_id) click to toggle source

Adds to the current scope builder condition for filtering by parent_id @param [Integer] parent_id @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/scope_builder.rb, line 82
def by_parent_id(parent_id)
  parameters[:parent_ids] = parent_id
  self
end
count() click to toggle source

Returns count of objects @return [Integer]

# File lib/syncano/active_record/scope_builder.rb, line 24
def count
  folder.data_objects.all(parameters).count
end
find(id) click to toggle source

Returns one object found by id @param [Integer] id @return [Object]

# File lib/syncano/active_record/scope_builder.rb, line 31
def find(id)
  parameters[:data_ids] = [id]
  all.first
end
first(amount = nil) click to toggle source

Returns first object or collection of first x objects @param [Integer] amount @return [Object, Array]

# File lib/syncano/active_record/scope_builder.rb, line 39
def first(amount = nil)
  objects = all.first(amount || 1)
  amount.nil? ? objects.first : objects
end
last(amount) click to toggle source

Returns last object or last x objects @param [Integer] amount @return [Object, Array]

# File lib/syncano/active_record/scope_builder.rb, line 47
def last(amount)
  objects = all.last(amount || 1)
  amount.nil? ? objects.first : objects
end
limit(amount) click to toggle source

Adds to the current scope builder limit clause @param [Integer] amount @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/scope_builder.rb, line 125
def limit(amount)
  self.parameters[:limit] = amount
  self
end
order(order) click to toggle source

Adds to the current scope builder order clause @param [String] order @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/scope_builder.rb, line 90
def order(order)
  attribute, order_type = order.gsub(/\s+/, ' ').split(' ')
  raise 'Invalid attribute in order clause' unless (model.attributes.keys + ['id', 'created_at']).include?(attribute)

  attribute = model.map_to_syncano_attribute(attribute)
  order_type = order_type.to_s.downcase == 'desc' ? 'DESC' : 'ASC'

  self.parameters.merge!({ order_by: attribute, order: order_type })

  self
end
since(id) click to toggle source

Adds to the current scope builder condition for filtering by ids newer than provided @param [Integer, String] id - id or datetime @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/scope_builder.rb, line 105
def since(id)
  if !(id =~ /\A[-+]?[0-9]+\z/)
    self.parameters[:since] = id
  else
    self.parameters[:since_time] = id.to_time
  end
  self
end
where(condition, *params) click to toggle source

Adds to the current scope builder condition to the scope builder @param [String] condition @param [Array] params @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/scope_builder.rb, line 56
def where(condition, *params)
  raise 'Invalid params count in where clause!' unless condition.count('?') == params.count

  params.each do |param|
    condition.sub!('?', param.to_s)
  end

  conditions = condition.gsub(/\s+/, ' ').split(/and/i)

  conditions.each do |condition|
    attribute, operator, value = condition.split(' ')

    raise 'Invalid attribute in where clause!' unless model.attributes.keys.include?(attribute)
    raise 'Invalid operator in where clause!' unless self.class.where_mapping.keys.include?(operator)
    raise 'Parameter in where clause is not an integer!' if !(value =~ /\A[-+]?[0-9]+\z/)

    method_name = "#{model.filterable_attributes[attribute]}__#{self.class.where_mapping[operator]}"
    parameters[method_name] = value
  end

  self
end

Private Instance Methods

execute_scope(name, *args) click to toggle source

Applies scope to the current scope builder @param [Symbol] name @param [Array] args @return [Syncano::ActiveRecord::ScopeBuilder]

# File lib/syncano/active_record/scope_builder.rb, line 156
def execute_scope(name, *args)
  procedure = scopes[name]
  instance_exec(*args, &procedure)
  self
end
folder() click to toggle source

Returns folder for current model @return [Syncano::Resources::Folder]

# File lib/syncano/active_record/scope_builder.rb, line 136
def folder
  model.folder
end
method_missing(name, *args) click to toggle source

Overwritten method_missing for handling calling defined scopes @param [String] name @param [Array] args

Calls superclass method
# File lib/syncano/active_record/scope_builder.rb, line 165
def method_missing(name, *args)
  if scopes[name].nil?
    super
  else
    execute_scope(name, *args)
  end
end