Module: RPCMapper::FinderMethods
- Defined in:
- lib/rpc_mapper/relation/finder_methods.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #apply_finder_options(options) ⇒ Object
- #find(ids_or_mode, options = {}) ⇒ Object
- #first(options = {}) ⇒ Object
- #search(options = {}) ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
20 21 22 |
# File 'lib/rpc_mapper/relation/finder_methods.rb', line 20 def all(={}) self.().to_a end |
#apply_finder_options(options) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rpc_mapper/relation/finder_methods.rb', line 36 def () relation = clone return relation unless [:joins, :limit, :offset, :order, :select, :group, :having, :from, :fresh, :includes].each do |finder| relation = relation.send(finder, [finder]) if [finder] end relation = relation.where([:conditions]) if .has_key?(:conditions) relation = relation.where([:where]) if .has_key?(:where) relation = relation.includes([:include]) if .has_key?(:include) relation = relation.search([:search]) if .has_key?(:search) relation = relation.sql([:sql]) if .has_key?(:sql) relation end |
#find(ids_or_mode, options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rpc_mapper/relation/finder_methods.rb', line 3 def find(ids_or_mode, ={}) case ids_or_mode when Fixnum, String self.where(:id => ids_or_mode.to_i).first() || raise(RPCMapper::RecordNotFound, "Could not find #{@klass} with :id = #{ids_or_mode}") when Array self.where(:id => ids_or_mode).all().tap do |result| raise RPCMapper::RecordNotFound, "Couldn't find all #{@klass} with ids (#{ids_or_mode.join(',')}) (expected #{ids_or_mode.size} records but got #{result.size})." unless result.size == ids_or_mode.size end when :all self.all() when :first self.first() else raise ArgumentError, "Unknown arguments for method find" end end |
#first(options = {}) ⇒ Object
24 25 26 |
# File 'lib/rpc_mapper/relation/finder_methods.rb', line 24 def first(={}) self.().limit(1).to_a.first end |
#search(options = {}) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/rpc_mapper/relation/finder_methods.rb', line 28 def search(={}) relation = self .each do |search, *args| relation = relation.send(search, *args) if @klass.send(:condition_details, search) end relation end |