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
- .after_save(*methods, options) ⇒ Object
-
.all ⇒ Object
Returns all persisted objects.
- .before_save(*methods, options) ⇒ Object
-
.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
- .repository? ⇒ Boolean
- .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?, #initialize, insert, #persisted?, #readonly?, remove, #to_param, validate_unique_id
Constructor Details
This class inherits a constructor from ActiveHash::Base
Class Method Details
.after_save(*methods, options) ⇒ Object
68 69 70 |
# File 'lib/active_repository/base.rb', line 68 def self.after_save(*methods, ) add_callbacks(__method__, methods, ) end |
.all ⇒ Object
Returns all persisted objects
60 61 62 |
# File 'lib/active_repository/base.rb', line 60 def self.all (repository? ? super : PersistenceAdapter.all(self).map { |object| serialize!(object.attributes) }) end |
.before_save(*methods, options) ⇒ Object
64 65 66 |
# File 'lib/active_repository/base.rb', line 64 def self.before_save(*methods, ) add_callbacks(__method__, methods, ) end |
.constantize ⇒ Object
Constantize class name
73 74 75 |
# File 'lib/active_repository/base.rb', line 73 def self.constantize self.to_s.constantize end |
.delete_all ⇒ Object
Deletes all persisted objects
78 79 80 |
# File 'lib/active_repository/base.rb', line 78 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
83 84 85 |
# File 'lib/active_repository/base.rb', line 83 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)
104 105 106 107 108 109 110 |
# File 'lib/active_repository/base.rb', line 104 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)
113 114 115 116 117 118 |
# File 'lib/active_repository/base.rb', line 113 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
98 99 100 101 |
# File 'lib/active_repository/base.rb', line 98 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
87 88 89 90 91 |
# File 'lib/active_repository/base.rb', line 87 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
135 136 137 |
# File 'lib/active_repository/base.rb', line 135 def self.persistence_class=(value) self.model_class = value end |
.repository? ⇒ Boolean
93 94 95 |
# File 'lib/active_repository/base.rb', line 93 def self.repository? self == persistence_class end |
.save_in_memory? ⇒ Boolean
145 146 147 |
# File 'lib/active_repository/base.rb', line 145 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
121 122 123 124 125 126 127 128 |
# File 'lib/active_repository/base.rb', line 121 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
131 132 133 |
# File 'lib/active_repository/base.rb', line 131 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
140 141 142 143 |
# File 'lib/active_repository/base.rb', line 140 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
151 152 153 154 |
# File 'lib/active_repository/base.rb', line 151 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:
* RelatedClass.where(:name => "Peter")
* RelatedClass.where("name = 'Peter'")
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/active_repository/base.rb', line 163 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
187 188 189 190 |
# File 'lib/active_repository/base.rb', line 187 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.
194 195 196 197 198 |
# File 'lib/active_repository/base.rb', line 194 def persist if self.valid? save_in_memory? ? save : self.convert.present? end end |
#persistence_class ⇒ Object
183 184 185 |
# File 'lib/active_repository/base.rb', line 183 def persistence_class self.class.persistence_class end |
#reload ⇒ Object
Gathers the persisted object from database and updates self with it’s attributes.
201 202 203 204 205 206 207 |
# File 'lib/active_repository/base.rb', line 201 def reload object = self.id.present? ? persistence_class.where(id: self.id).first_or_initialize : self serialize! object.attributes end |
#save(force = false) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/active_repository/base.rb', line 209 def save(force=false) execute_callbacks(before_save_methods) result = true if self.class == persistence_class object = persistence_class.where(id: self.id).first_or_initialize result = if force || self.id.nil? self.id = nil if self.id.nil? super elsif self.valid? object.attributes = self.attributes.select{ |key, value| self.class.serialized_attributes.include?(key.to_s) } object.save(true) end else result = self.persist end execute_callbacks(after_save_methods) # (after_save_methods || []).each { |method| self.send(method) } result end |
#serialize!(attributes) ⇒ Object
Updates attributes from self with the attributes from the parameters
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/active_repository/base.rb', line 234 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 |