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.set_model_class(ORMOrODMModelClass)
SaveInORMOrODMTest.set_save_in_memory(false)
end
- Author
-
Caio Torres ([email protected])
- License
-
MIT
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.
-
.get_model_class ⇒ Object
Returns the Class responsible for persisting the objects.
-
.serialize!(other) ⇒ Object
Converts Persisted object(s) to it’s ActiveRepository counterpart.
-
.serialized_attributes ⇒ Object
Returns a 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
-
#persist ⇒ Object
Persists the object using the class defined on the model_class attribute, if none defined it is saved in memory.
-
#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
define_custom_find_all_by_field, define_custom_find_by_field, find, find_all_by_field, find_by_id, first, last
Methods included from Writers
create, find_or_create, find_or_initialize
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, #update_attribute, validate_unique_id
Class Method Details
.all ⇒ Object
Returns all persisted objects
71 72 73 74 |
# File 'lib/active_repository/base.rb', line 71 def self.all (self == get_model_class ? super : PersistenceAdapter.all(self).map { |object| serialize!(object.attributes) }) # self == get_model_class ? super : get_model_class.all.map { |object| serialize!(object.attributes) } end |
.constantize ⇒ Object
Constantize class name
77 78 79 |
# File 'lib/active_repository/base.rb', line 77 def self.constantize self.to_s.constantize end |
.delete_all ⇒ Object
Deletes all persisted objects
82 83 84 |
# File 'lib/active_repository/base.rb', line 82 def self.delete_all self == get_model_class ? super : PersistenceAdapter.delete_all(self) end |
.exists?(id) ⇒ Boolean
Checks the existence of a persisted object with the specified id
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_repository/base.rb', line 87 def self.exists?(id) self == get_model_class ? find_by_id(id).present? : PersistenceAdapter.exists?(self, id) # if self == get_model_class # !find_by_id(id).nil? # else # if mongoid? # find_by_id(id).present? # else # get_model_class.exists?(id) # end # end end |
.get_model_class ⇒ Object
Returns the Class responsible for persisting the objects
101 102 103 104 |
# File 'lib/active_repository/base.rb', line 101 def self.get_model_class return self if self.model_class.nil? || self.save_in_memory? save_in_memory? ? self : self.model_class end |
.serialize!(other) ⇒ Object
Converts Persisted object(s) to it’s ActiveRepository counterpart
107 108 109 110 111 112 113 114 |
# File 'lib/active_repository/base.rb', line 107 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" then self.new.serialize!(other) else self.new.serialize!(other.attributes) end end |
.serialized_attributes ⇒ Object
Returns a array with the field names of the Class
117 118 119 |
# File 'lib/active_repository/base.rb', line 117 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
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/active_repository/base.rb', line 122 def self.set_model_class(value) self.model_class = value if model_class.nil? self.set_save_in_memory(self.model_class == self) field_names.each do |field_name| define_custom_find_by_field(field_name) define_custom_find_all_by_field(field_name) end 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
135 136 137 |
# File 'lib/active_repository/base.rb', line 135 def self.set_save_in_memory(value) 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'")
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/active_repository/base.rb', line 146 def self.where(*args) raise ArgumentError.new("wrong number of arguments (0 for 1)") if args.empty? if self == get_model_class query = ActiveHash::SQLQueryExecutor.args_to_query(args) super(query) else objects = [] args = args.first.is_a?(Hash) ? args.first : (args.first.is_a?(Array) ? args.first : args) PersistenceAdapter.where(self, args).each do |object| objects << self.serialize!(object.attributes) end objects end end |
Instance Method Details
#persist ⇒ Object
Persists the object using the class defined on the model_class attribute, if none defined it is saved in memory.
165 166 167 168 169 |
# File 'lib/active_repository/base.rb', line 165 def persist if self.valid? save_in_memory? ? save : self.convert.present? end end |
#reload ⇒ Object
Gathers the persisted object from database and updates self with it’s attributes.
172 173 174 175 176 |
# File 'lib/active_repository/base.rb', line 172 def reload object = self.id.present? ? self.class.get_model_class.find(self.id) : self serialize! object.attributes end |
#save(force = false) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/active_repository/base.rb', line 178 def save(force=false) if self.class == self.class.get_model_class object = self.class.get_model_class.find(self.id) if force || self.id.nil? self.id = nil if self.id.nil? super else object.attributes = self.attributes object.save(true) end true else self.persist end end |
#serialize!(attributes) ⇒ Object
Updates attributes from self with the attributes from the parameters
197 198 199 200 201 202 203 |
# File 'lib/active_repository/base.rb', line 197 def serialize!(attributes) unless attributes.nil? self.attributes = attributes end self.dup end |