Class: MotionModel::FinderQuery
- Inherits:
-
Object
- Object
- MotionModel::FinderQuery
- Defined in:
- lib/motion_model/model/finder_query.rb
Instance Attribute Summary collapse
-
#field_name ⇒ Object
Returns the value of attribute field_name.
Instance Method Summary collapse
-
#all ⇒ Object
returns all elements that match as an array.
-
#and(field_name) ⇒ Object
(also: #where)
Conjunction to add conditions to query.
-
#belongs_to(obj, klass = nil) ⇒ Object
nodoc.
-
#contain(query_string, options = {:case_sensitive => false}) ⇒ Object
(also: #contains, #like)
performs a “like” query.
-
#create(options) ⇒ Object
task.assignees.create(:name => ‘bob’) creates a new Assignee object on the Task object task.
-
#do_comparison(query_string, options = {:case_sensitive => false}) ⇒ Object
nodoc.
-
#each(&block) ⇒ Object
each is a shortcut method to turn a query into an iterator.
-
#eq(query_string, options = {:case_sensitive => false}) ⇒ Object
(also: #==, #equal)
performs strict equality comparison.
-
#first ⇒ Object
returns first element that matches.
-
#gt(query_string, options = {:case_sensitive => false}) ⇒ Object
(also: #>, #greater_than)
performs greater-than comparison.
-
#gte(query_string, options = {:case_sensitive => false}) ⇒ Object
(also: #>=, #greater_than_or_equal)
performs greater-than-or-equal comparison.
-
#in(set) ⇒ Object
performs a set-inclusion test.
-
#initialize(*args) ⇒ FinderQuery
constructor
nodoc.
-
#last ⇒ Object
returns last element that matches.
-
#length ⇒ Object
(also: #count)
Returns number of objects (rows) in collection.
-
#lt(query_string, options = {:case_sensitive => false}) ⇒ Object
(also: #<, #less_than)
performs less-than comparison.
-
#lte(query_string, options = {:case_sensitive => false}) ⇒ Object
(also: #<=, #less_than_or_equal)
performs less-than-or-equal comparison.
-
#ne(query_string, options = {:case_sensitive => false}) ⇒ Object
(also: #!=, #not_equal)
performs inequality comparison.
-
#new(options = {}) ⇒ Object
task.assignees.new(:name => ‘BoB’) creates a new unsaved Assignee object on the Task object task.
-
#order(field = nil, &block) ⇒ Object
Specifies how to sort.
-
#push(object) ⇒ Object
(also: #<<)
Pushes an object onto an association.
-
#translate_case(item, case_sensitive) ⇒ Object
nodoc.
Constructor Details
#initialize(*args) ⇒ FinderQuery
nodoc
5 6 7 8 |
# File 'lib/motion_model/model/finder_query.rb', line 5 def initialize(*args)#nodoc @field_name = args[0] if args.length > 1 @collection = args.last end |
Instance Attribute Details
#field_name ⇒ Object
Returns the value of attribute field_name.
3 4 5 |
# File 'lib/motion_model/model/finder_query.rb', line 3 def field_name @field_name end |
Instance Method Details
#all ⇒ Object
returns all elements that match as an array.
163 164 165 |
# File 'lib/motion_model/model/finder_query.rb', line 163 def all @collection end |
#and(field_name) ⇒ Object Also known as: where
Conjunction to add conditions to query.
Task.find(:name => ‘bob’).and(:gender).eq(‘M’) Task.asignees.where(:assignee_name).eq(‘bob’)
20 21 22 23 |
# File 'lib/motion_model/model/finder_query.rb', line 20 def and(field_name) @field_name = field_name self end |
#belongs_to(obj, klass = nil) ⇒ Object
nodoc
10 11 12 13 14 |
# File 'lib/motion_model/model/finder_query.rb', line 10 def belongs_to(obj, klass = nil) #nodoc @related_object = obj @klass = klass self end |
#contain(query_string, options = {:case_sensitive => false}) ⇒ Object Also known as: contains, like
performs a “like” query.
Task.find(:work_group).contain(‘dev’) => [‘UI dev’, ‘Core dev’, …]
59 60 61 62 63 64 65 66 67 |
# File 'lib/motion_model/model/finder_query.rb', line 59 def contain(query_string, = {:case_sensitive => false}) do_comparison(query_string) do |comparator, item| if [:case_sensitive] item =~ Regexp.new(comparator, Regexp::MULTILINE) else item =~ Regexp.new(comparator, Regexp::IGNORECASE | Regexp::MULTILINE) end end end |
#create(options) ⇒ Object
task.assignees.create(:name => ‘bob’) creates a new Assignee object on the Task object task
186 187 188 189 190 191 |
# File 'lib/motion_model/model/finder_query.rb', line 186 def create() raise ArgumentError.new("Creating on a relation requires the parent be saved first.") if @related_object.nil? obj = new() obj.save obj end |
#do_comparison(query_string, options = {:case_sensitive => false}) ⇒ Object
nodoc
46 47 48 49 50 51 52 53 54 |
# File 'lib/motion_model/model/finder_query.rb', line 46 def do_comparison(query_string, = {:case_sensitive => false})#nodoc query_string = translate_case(query_string, [:case_sensitive]) @collection = @collection.collect do |item| comparator = item.send(@field_name.to_sym) comparator = translate_case(comparator, [:case_sensitive]) item if yield query_string, comparator end.compact self end |
#each(&block) ⇒ Object
each is a shortcut method to turn a query into an iterator. It allows you to write code like:
Task.where(:assignee).eq('bob').each{ |assignee| do_something_with(assignee) }
171 172 173 174 |
# File 'lib/motion_model/model/finder_query.rb', line 171 def each(&block) raise ArgumentError.new("each requires a block") unless block_given? @collection.each{|item| yield item} end |
#eq(query_string, options = {:case_sensitive => false}) ⇒ Object Also known as: ==, equal
performs strict equality comparison.
If arguments are strings, they are, by default, compared case-insensitive, if case-sensitivity is required, use:
eq(‘something’, :case_sensitive => true)
87 88 89 90 91 |
# File 'lib/motion_model/model/finder_query.rb', line 87 def eq(query_string, = {:case_sensitive => false}) do_comparison(query_string, ) do |comparator, item| comparator == item end end |
#first ⇒ Object
returns first element that matches.
153 154 155 |
# File 'lib/motion_model/model/finder_query.rb', line 153 def first @collection.first end |
#gt(query_string, options = {:case_sensitive => false}) ⇒ Object Also known as: >, greater_than
performs greater-than comparison.
see ‘eq` for notes on case sensitivity.
98 99 100 101 102 |
# File 'lib/motion_model/model/finder_query.rb', line 98 def gt(query_string, = {:case_sensitive => false}) do_comparison(query_string, ) do |comparator, item| comparator > item end end |
#gte(query_string, options = {:case_sensitive => false}) ⇒ Object Also known as: >=, greater_than_or_equal
performs greater-than-or-equal comparison.
see ‘eq` for notes on case sensitivity.
120 121 122 123 124 |
# File 'lib/motion_model/model/finder_query.rb', line 120 def gte(query_string, = {:case_sensitive => false}) do_comparison(query_string, ) do |comparator, item| comparator >= item end end |
#in(set) ⇒ Object
performs a set-inclusion test.
Task.find(:id).id([3, 5, 9])
74 75 76 77 78 |
# File 'lib/motion_model/model/finder_query.rb', line 74 def in(set) @collection = @collection.collect do |item| item if set.include?(item.send(@field_name.to_sym)) end.compact end |
#last ⇒ Object
returns last element that matches.
158 159 160 |
# File 'lib/motion_model/model/finder_query.rb', line 158 def last @collection.last end |
#length ⇒ Object Also known as: count
Returns number of objects (rows) in collection
177 178 179 |
# File 'lib/motion_model/model/finder_query.rb', line 177 def length @collection.length end |
#lt(query_string, options = {:case_sensitive => false}) ⇒ Object Also known as: <, less_than
performs less-than comparison.
see ‘eq` for notes on case sensitivity.
109 110 111 112 113 |
# File 'lib/motion_model/model/finder_query.rb', line 109 def lt(query_string, = {:case_sensitive => false}) do_comparison(query_string, ) do |comparator, item| comparator < item end end |
#lte(query_string, options = {:case_sensitive => false}) ⇒ Object Also known as: <=, less_than_or_equal
performs less-than-or-equal comparison.
see ‘eq` for notes on case sensitivity.
131 132 133 134 135 |
# File 'lib/motion_model/model/finder_query.rb', line 131 def lte(query_string, = {:case_sensitive => false}) do_comparison(query_string, ) do |comparator, item| comparator <= item end end |
#ne(query_string, options = {:case_sensitive => false}) ⇒ Object Also known as: !=, not_equal
performs inequality comparison.
see ‘eq` for notes on case sensitivity.
142 143 144 145 146 |
# File 'lib/motion_model/model/finder_query.rb', line 142 def ne(query_string, = {:case_sensitive => false}) do_comparison(query_string, ) do |comparator, item| comparator != item end end |
#new(options = {}) ⇒ Object
task.assignees.new(:name => ‘BoB’) creates a new unsaved Assignee object on the Task object task
195 196 197 198 199 200 201 202 |
# File 'lib/motion_model/model/finder_query.rb', line 195 def new( = {}) raise ArgumentError.new("Creating on a relation requires the parent be saved first.") if @related_object.nil? id_field = (@related_object.class.to_s.underscore + '_id').to_sym new_obj = @klass.new(.merge(id_field => @related_object.id)) new_obj end |
#order(field = nil, &block) ⇒ Object
Specifies how to sort. only ascending sort is supported in the short form. For descending, implement the block form.
Task.where(:name).eq('bob').order(:pay_grade).all => array of bobs ascending by pay grade
Task.where(:name).eq('bob').order(:pay_grade){|o1, o2| o2 <=> o1} => array of bobs descending by pay grade
31 32 33 34 35 36 37 38 39 |
# File 'lib/motion_model/model/finder_query.rb', line 31 def order(field = nil, &block) if block_given? @collection = @collection.sort{|o1, o2| yield(o1, o2)} else raise ArgumentError.new('you must supply a field name to sort unless you supply a block.') if field.nil? @collection = @collection.sort{|o1, o2| o1.send(field) <=> o2.send(field)} end self end |
#push(object) ⇒ Object Also known as: <<
Pushes an object onto an association. For e.g.:
Task.find(3).assignees.push(assignee)
This both establishes the relation and saves the related object, so make sure the related object is valid.
216 217 218 219 220 221 222 |
# File 'lib/motion_model/model/finder_query.rb', line 216 def push(object) id_field = (@related_object.class.to_s.underscore + '_id=').to_sym object.send(id_field, @related_object.id) result = object.save result ||= @related_object.save result end |
#translate_case(item, case_sensitive) ⇒ Object
nodoc
41 42 43 44 |
# File 'lib/motion_model/model/finder_query.rb', line 41 def translate_case(item, case_sensitive)#nodoc item = item.underscore if case_sensitive === false && item.respond_to?(:underscore) item end |