Module: Ankoder::CoreExtension::ArrayExtension
- Included in:
- Array
- Defined in:
- lib/ankoder/ext.rb
Instance Method Summary collapse
-
#find_with_conditions(conditions = {}) ⇒ Object
Select Ankoder#Base object containing the conditions.
- #find_with_options(options = {}) ⇒ Object
- #include_ankoder_object(inc = nil) ⇒ Object
- #limit(lim = nil) ⇒ Object
- #order(order_str = nil) ⇒ Object
Instance Method Details
#find_with_conditions(conditions = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ankoder/ext.rb', line 67 def find_with_conditions(conditions={}) return self if conditions.nil? or conditions.empty? res = [] self.each do |object| object.attributes.each_pair do |k,v| if conditions[k.to_sym] condition = [] if not conditions[k.to_sym].is_a?(Regexp) and conditions[k.to_sym].to_s =~ /([<|>|<=|>=|==]+)[\s]*([0-9]+)$/ conditions[k.to_sym].to_s.split(" and ").each do |c| c.match(/([<|>|<=|>=|==]+)[\s]*([0-9]+)$/) condition << "#{v} #{$1} #{$2}" end res << object if eval(condition.join(" and ")) elsif conditions[k.to_sym].is_a?(Regexp) res << object if v =~ conditions[k.to_sym] else res << object if v == conditions[k.to_sym] end end end end return res end |
#find_with_options(options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/ankoder/ext.rb', line 53 def (={}) collection = self if [:conditions] [:conditions].each_pair do |k,v| collection = collection.find_with_conditions(k => v) end end collection.limit([:limit]).order([:order]).include_ankoder_object([:include]) end |
#include_ankoder_object(inc = nil) ⇒ Object
112 113 114 115 |
# File 'lib/ankoder/ext.rb', line 112 def include_ankoder_object(inc=nil) return self if inc.nil? self.each {|object| object.include_ankoder_object(inc)} end |
#limit(lim = nil) ⇒ Object
91 92 93 94 |
# File 'lib/ankoder/ext.rb', line 91 def limit(lim=nil) return self if lim.nil? self[0..lim-1] end |
#order(order_str = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ankoder/ext.rb', line 96 def order(order_str=nil) return self if order_str.nil? field, sort = order_str.match(/([a-z0-9_]+)([\s+]*[ASC|DESC]*)/i).to_a[1..-1] sort = "ASC" if sort.nil? or sort.empty? begin self.sort do |x,y| case sort when /ASC/i then x.send(field) <=> y.send(field) when /DESC/i then y.send(field) <=> x.send(field) end end rescue self end end |