Class: Syncano::ActiveRecord::ScopeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/syncano/active_record/scope_builder.rb

Overview

ScopeBuilder class allows for creating and chaining more complex queries

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ScopeBuilder

Constructor for ScopeBuilder

Parameters:

  • model (Class)


7
8
9
10
11
12
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

Overwritten method_missing for handling calling defined scopes

Parameters:

  • name (String)
  • args (Array)


165
166
167
168
169
170
171
# 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

Instance Method Details

#allArray

Returns collection of objects

Returns:

  • (Array)


16
17
18
19
20
# 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) ⇒ Syncano::ActiveRecord::ScopeBuilder

Adds to the current scope builder condition for filtering by ids older than provided

Parameters:

  • id (Integer)

Returns:



117
118
119
120
# 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) ⇒ Syncano::ActiveRecord::ScopeBuilder

Adds to the current scope builder condition for filtering by parent_id

Parameters:

  • parent_id (Integer)

Returns:



82
83
84
85
# File 'lib/syncano/active_record/scope_builder.rb', line 82

def by_parent_id(parent_id)
  parameters[:parent_ids] = parent_id
  self
end

#countInteger

Returns count of objects

Returns:

  • (Integer)


24
25
26
# File 'lib/syncano/active_record/scope_builder.rb', line 24

def count
  folder.data_objects.all(parameters).count
end

#find(id) ⇒ Object

Returns one object found by id

Parameters:

  • id (Integer)

Returns:

  • (Object)


31
32
33
34
# File 'lib/syncano/active_record/scope_builder.rb', line 31

def find(id)
  parameters[:data_ids] = [id]
  all.first
end

#first(amount = nil) ⇒ Object, Array

Returns first object or collection of first x objects

Parameters:

  • amount (Integer) (defaults to: nil)

Returns:

  • (Object, Array)


39
40
41
42
# 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) ⇒ Object, Array

Returns last object or last x objects

Parameters:

  • amount (Integer)

Returns:

  • (Object, Array)


47
48
49
50
# 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) ⇒ Syncano::ActiveRecord::ScopeBuilder

Adds to the current scope builder limit clause

Parameters:

  • amount (Integer)

Returns:



125
126
127
128
# File 'lib/syncano/active_record/scope_builder.rb', line 125

def limit(amount)
  self.parameters[:limit] = amount
  self
end

#order(order) ⇒ Syncano::ActiveRecord::ScopeBuilder

Adds to the current scope builder order clause

Parameters:

  • order (String)

Returns:



90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Syncano::ActiveRecord::ScopeBuilder

Adds to the current scope builder condition for filtering by ids newer than provided

Parameters:

  • id (Integer, String)
    • id or datetime

Returns:



105
106
107
108
109
110
111
112
# 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) ⇒ Syncano::ActiveRecord::ScopeBuilder

Adds to the current scope builder condition to the scope builder

Parameters:

  • condition (String)
  • params (Array)

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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