Class: ActiveRepository::Base
- Inherits:
-
ActiveHash::Base
- Object
- ActiveHash::Base
- ActiveRepository::Base
- Includes:
- ActiveModel::Validations, ActiveModel::Validations::Callbacks, Associations, Writers::InstanceMethods
- Defined in:
- lib/active_repository/base.rb
Overview
Base class for ActiveRepository gem. Extends it in order to use it.
Options
There are 2 class attributes to help configure your ActiveRepository class:
* +class_model+: Use it to specify the class that is responsible for the
persistence of the objects. Default is self, so it is always saving in
memory by default.
* +save_in_memory+: Used to ignore the class_model attribute, you can use
it in your test suite, this way all your tests will be saved in memory.
Default is set to true so it saves in memory by default.
Examples
Using ActiveHash to persist objects in memory:
class SaveInMemoryTest < ActiveRepository::Base
end
Using ActiveRecord/Mongoid to persist objects:
class SaveInORMOrODMTest < ActiveRepository::Base
SaveInORMOrODMTest.persistence_class = ORMOrODMModelClass
SaveInORMOrODMTest.save_in_memory = false
end
- Author
-
Caio Torres ([email protected])
- License
-
GPL
Class Method Summary collapse
-
.all ⇒ Object
Returns all persisted objects.
-
.constantize ⇒ Object
Constantize class name.
-
.delete_all ⇒ Object
Deletes all persisted objects.
-
.exists?(id) ⇒ Boolean
Checks the existence of a persisted object with the specified id.
-
.find_by(args) ⇒ Object
Searches all objects that matches #field_name field with the #args value(s).
-
.find_by!(args) ⇒ Object
Searches all objects that matches #field_name field with the #args value(s).
-
.get_model_class ⇒ Object
Returns the Class responsible for persisting the objects.
- .persistence_class ⇒ Object
- .persistence_class=(value) ⇒ Object
- .save_in_memory? ⇒ Boolean
-
.serialize!(other) ⇒ Object
Converts Persisted object(s) to it’s ActiveRepository counterpart.
-
.serialized_attributes ⇒ Object
Returns an array with the field names of the Class.
-
.set_model_class(value) ⇒ Object
Sets the class attribute model_class, responsible to persist the ActiveRepository objects.
-
.set_save_in_memory(value) ⇒ Object
Sets the class attribute save_in_memory, set it to true to ignore model_class attribute and persist objects in memory.
-
.where(*args) ⇒ Object
Searches persisted objects that matches the criterias in the parameters.
Instance Method Summary collapse
- #get_model_class ⇒ Object
-
#persist ⇒ Object
Persists the object using the class defined on the model_class attribute, if none defined it is saved in memory.
- #persistence_class ⇒ Object
-
#reload ⇒ Object
Gathers the persisted object from database and updates self with it’s attributes.
- #save(force = false) ⇒ Object
-
#serialize!(attributes) ⇒ Object
Updates attributes from self with the attributes from the parameters.
Methods included from Finders
Methods included from Writers
Methods included from Writers::InstanceMethods
#attributes=, #delete, #update_attribute, #update_attributes
Methods included from Associations
Methods inherited from ActiveHash::Base
#delete, #eql?, insert, #persisted?, #readonly?, remove, #to_param, validate_unique_id
Class Method Details
.all ⇒ Object
Returns all persisted objects
59 60 61 |
# File 'lib/active_repository/base.rb', line 59 def self.all (repository? ? super : PersistenceAdapter.all(self).map { |object| serialize!(object.attributes) }) end |
.constantize ⇒ Object
Constantize class name
64 65 66 |
# File 'lib/active_repository/base.rb', line 64 def self.constantize self.to_s.constantize end |
.delete_all ⇒ Object
Deletes all persisted objects
69 70 71 |
# File 'lib/active_repository/base.rb', line 69 def self.delete_all repository? ? super : PersistenceAdapter.delete_all(self) end |
.exists?(id) ⇒ Boolean
Checks the existence of a persisted object with the specified id
74 75 76 |
# File 'lib/active_repository/base.rb', line 74 def self.exists?(id) repository? ? find_by(id: id).present? : PersistenceAdapter.exists?(self, id) end |
.find_by(args) ⇒ Object
Searches all objects that matches #field_name field with the #args value(s)
91 92 93 94 95 96 97 |
# File 'lib/active_repository/base.rb', line 91 def self.find_by(args) raise ArgumentError("Argument must be a Hash") unless args.is_a?(Hash) objects = where(args) objects.first end |
.find_by!(args) ⇒ Object
Searches all objects that matches #field_name field with the #args value(s)
100 101 102 103 104 105 |
# File 'lib/active_repository/base.rb', line 100 def self.find_by!(args) object = find_by(args) raise ActiveHash::RecordNotFound unless object object end |
.get_model_class ⇒ Object
Returns the Class responsible for persisting the objects
85 86 87 88 |
# File 'lib/active_repository/base.rb', line 85 def self.get_model_class puts '[deprecation warning] This method is going to be deprecated, use "persistence_class" instead.' persistence_class end |
.persistence_class ⇒ Object
78 79 80 81 82 |
# File 'lib/active_repository/base.rb', line 78 def self.persistence_class return self if save_in_memory? || (postfix.nil? && self.model_class.nil?) return "#{self}#{postfix.classify}".constantize if postfix.present? self.model_class.to_s.constantize end |
.persistence_class=(value) ⇒ Object
122 123 124 |
# File 'lib/active_repository/base.rb', line 122 def self.persistence_class=(value) self.model_class = value end |
.save_in_memory? ⇒ Boolean
132 133 134 |
# File 'lib/active_repository/base.rb', line 132 def self.save_in_memory? self.save_in_memory == nil ? true : self.save_in_memory end |
.serialize!(other) ⇒ Object
Converts Persisted object(s) to it’s ActiveRepository counterpart
108 109 110 111 112 113 114 115 |
# File 'lib/active_repository/base.rb', line 108 def self.serialize!(other) case other.class.to_s when "Hash", "ActiveSupport::HashWithIndifferentAccess" then self.new.serialize!(other) when "Array" then other.map { |o| serialize!(o.attributes) } when "Moped::BSON::Document", "BSON::Document" then self.new.serialize!(other) else self.new.serialize!(other.attributes) end end |
.serialized_attributes ⇒ Object
Returns an array with the field names of the Class
118 119 120 |
# File 'lib/active_repository/base.rb', line 118 def self.serialized_attributes field_names.map &:to_s end |
.set_model_class(value) ⇒ Object
Sets the class attribute model_class, responsible to persist the ActiveRepository objects
127 128 129 130 |
# File 'lib/active_repository/base.rb', line 127 def self.set_model_class(value) puts '[deprecation warning] This method is going to be deprecated, use "persistence_class=" instead.' persistence_class = value end |
.set_save_in_memory(value) ⇒ Object
Sets the class attribute save_in_memory, set it to true to ignore model_class attribute and persist objects in memory
138 139 140 141 |
# File 'lib/active_repository/base.rb', line 138 def self.set_save_in_memory(value) puts '[deprecation warning] This method is going to be deprecated, use "save_in_memory=" instead.' self.save_in_memory = value end |
.where(*args) ⇒ Object
Searches persisted objects that matches the criterias in the parameters. Can be used in ActiveRecord/Mongoid way or in SQL like way.
Example:
* .where(:name => "Peter")
* .where("name = 'Peter'")
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/active_repository/base.rb', line 150 def self.where(*args) raise ArgumentError.new("must pass at least one argument") if args.empty? result_set = ActiveRepository::ResultSet.new(self) result_set.where(args) # if repository? # args = args.first if args.respond_to?(:size) && args.size == 1 # query_executor = SqlQueryExecutor::Base.new(all) # query_executor.where(args) # else # objects = PersistenceAdapter.where(self, sanitize_args(args)).map do |object| # self.serialize!(object.attributes) # end # objects # end end |
Instance Method Details
#get_model_class ⇒ Object
174 175 176 177 |
# File 'lib/active_repository/base.rb', line 174 def get_model_class puts '[deprecation warning] This method is going to be deprecated, use "persistence_class" instead.' self.class.persistence_class end |
#persist ⇒ Object
Persists the object using the class defined on the model_class attribute, if none defined it is saved in memory.
181 182 183 184 185 |
# File 'lib/active_repository/base.rb', line 181 def persist if self.valid? save_in_memory? ? save : self.convert.present? end end |
#persistence_class ⇒ Object
170 171 172 |
# File 'lib/active_repository/base.rb', line 170 def persistence_class self.class.persistence_class end |
#reload ⇒ Object
Gathers the persisted object from database and updates self with it’s attributes.
188 189 190 191 192 193 194 |
# File 'lib/active_repository/base.rb', line 188 def reload object = self.id.present? ? persistence_class.where(id: self.id).first_or_initialize : self serialize! object.attributes end |
#save(force = false) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/active_repository/base.rb', line 196 def save(force=false) if self.class == persistence_class object = persistence_class.where(id: self.id).first_or_initialize if force || self.id.nil? self.id = nil if self.id.nil? super elsif self.valid? object.attributes = self.attributes object.save(true) end self.valid? else self.persist end end |
#serialize!(attributes) ⇒ Object
Updates attributes from self with the attributes from the parameters
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/active_repository/base.rb', line 215 def serialize!(attributes) unless attributes.nil? attributes.each do |key, value| key = "id" if key == "_id" self.send("#{key}=", (value.dup rescue value)) end end self.dup end |