Class: Syncano::ActiveRecord::ScopeBuilder
- Inherits:
-
Object
- Object
- Syncano::ActiveRecord::ScopeBuilder
- Defined in:
- lib/syncano/active_record/scope_builder.rb
Overview
ScopeBuilder class allows for creating and chaining more complex queries
Instance Method Summary collapse
-
#all ⇒ Array
Returns collection of objects.
-
#before(id) ⇒ Syncano::ActiveRecord::ScopeBuilder
Adds to the current scope builder condition for filtering by ids older than provided.
-
#by_parent_id(parent_id) ⇒ Syncano::ActiveRecord::ScopeBuilder
Adds to the current scope builder condition for filtering by parent_id.
-
#count ⇒ Integer
Returns count of objects.
-
#find(id) ⇒ Object
Returns one object found by id.
-
#first(amount = nil) ⇒ Object, Array
Returns first object or collection of first x objects.
-
#initialize(model) ⇒ ScopeBuilder
constructor
Constructor for ScopeBuilder.
-
#last(amount) ⇒ Object, Array
Returns last object or last x objects.
-
#limit(amount) ⇒ Syncano::ActiveRecord::ScopeBuilder
Adds to the current scope builder limit clause.
-
#order(order) ⇒ Syncano::ActiveRecord::ScopeBuilder
Adds to the current scope builder order clause.
-
#since(id) ⇒ Syncano::ActiveRecord::ScopeBuilder
Adds to the current scope builder condition for filtering by ids newer than provided.
-
#where(condition, *params) ⇒ Syncano::ActiveRecord::ScopeBuilder
Adds to the current scope builder condition to the scope builder.
Constructor Details
#initialize(model) ⇒ ScopeBuilder
Constructor for ScopeBuilder
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
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
#all ⇒ Array
Returns collection of objects
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
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
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 |
#count ⇒ Integer
Returns count of objects
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
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
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
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
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
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
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
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 |