Module: ActiveRepository::Finders
- Included in:
- Base
- Defined in:
- lib/active_repository/finders.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#define_custom_find_all_by_field(field_name) ⇒ Object
Defines fiend_all_by_field methods for the Class.
-
#define_custom_find_by_field(field_name) ⇒ Object
Defines fiend_by_field methods for the Class.
-
#find(id) ⇒ Object
Searches for a object containing the id in #id.
-
#find_all_by_field(field_name, args) ⇒ Object
Searches all objects that matches #field_name field with the #args value(s).
-
#find_by_id(id) ⇒ Object
Searches for an object that has id with #id value, if none is found returns nil.
-
#first ⇒ Object
Returns first persisted object.
-
#last ⇒ Object
Returns last persisted object.
Instance Method Details
#define_custom_find_all_by_field(field_name) ⇒ Object
Defines fiend_all_by_field methods for the Class
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_repository/finders.rb', line 19 def define_custom_find_all_by_field(field_name) method_name = :"find_all_by_#{field_name}" .instance_eval do define_method(method_name) do |*args| objects = [] objects = self.find_all_by_field(field_name.to_sym, args) objects.empty? ? [] : objects.map{ |object| serialize!(object.attributes) } end end end |
#define_custom_find_by_field(field_name) ⇒ Object
Defines fiend_by_field methods for the Class
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_repository/finders.rb', line 5 def define_custom_find_by_field(field_name) method_name = :"find_by_#{field_name}" .instance_eval do define_method(method_name) do |*args| object = nil object = self.where(field_name.to_sym => args).first object.nil? ? nil : serialize!(object.attributes) end end end |
#find(id) ⇒ Object
Searches for a object containing the id in #id
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_repository/finders.rb', line 33 def find(id) begin if self == get_model_class super(id) else object = (id == :all) ? all : PersistenceAdapter.find(self, id) serialize!(object) end rescue Exception => e = "Couldn't find #{self} with ID=#{id}" = "Couldn't find all #{self} objects with IDs (#{id.join(', ')})" if id.is_a?(Array) raise ActiveHash::RecordNotFound.new() end end |
#find_all_by_field(field_name, args) ⇒ Object
Searches all objects that matches #field_name field with the #args value(s)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_repository/finders.rb', line 51 def find_all_by_field(field_name, args) objects = [] # raise "field: #{field_name}; values: #{args.first.inspect}; all: #{get_model_class.all.inspect}" if self == get_model_class objects = self.where(field_name.to_sym => args.first) else objects = PersistenceAdapter.where(self, field_name.to_sym => args.first) # if mongoid? # objects = get_model_class.where(field_name.to_sym => args.first) # else # method_name = :"find_all_by_#{field_name}" # objects = get_model_class.send(method_name, args) # end end objects end |
#find_by_id(id) ⇒ Object
Searches for an object that has id with #id value, if none is found returns nil
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/active_repository/finders.rb', line 77 def find_by_id(id) if self == get_model_class object = super(id) object.nil? ? nil : object.dup else object = PersistenceAdapter.where(self, :id => id).first # if mongoid? # object = get_model_class.where(:id => id).entries.first # else # object = get_model_class.find_by_id(id) # end object.nil? ? nil : serialize!(object.attributes) end end |
#first ⇒ Object
Returns first persisted object
96 97 98 |
# File 'lib/active_repository/finders.rb', line 96 def first self == get_model_class ? super : get(:first) end |
#last ⇒ Object
Returns last persisted object
101 102 103 |
# File 'lib/active_repository/finders.rb', line 101 def last self == get_model_class ? super : get(:last) end |