Class: ActiveMocker::Base
- Inherits:
-
Object
- Object
- ActiveMocker::Base
- Extended by:
- AliasAttribute, Queries
- Defined in:
- lib/active_mocker/mock/base.rb,
lib/active_mocker/deprecated_components/mock_abilities.rb
Defined Under Namespace
Modules: PropertiesGetterAndSetter, Scopes Classes: ScopeRelation
Instance Attribute Summary collapse
- #_create_caller_locations ⇒ Object
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Class Method Summary collapse
-
.__new_relation__(collection) ⇒ ScopeRelation
For the given mock so that it will include any scoped methods.
- ._find_associations_by_class(klass_name) ⇒ Object
- .abstract_class? ⇒ Boolean
- .build_type(type) ⇒ Object
- .classes(klass) ⇒ Object
- .clear_mock ⇒ Object deprecated Deprecated.
-
.create(attributes = {}, &block) ⇒ Object
(also: create!)
Creates an object (or multiple objects) and saves it to memory.
-
.delete(id) ⇒ Object
Delete an object (or multiple objects) that has the given id.
-
.delete_all(conditions = nil) ⇒ Object
(also: destroy_all)
Deletes the records matching
conditions. -
.destroy ⇒ Object
Delete an object (or multiple objects) that has the given id.
- .from_limit? ⇒ Boolean private
- .inherited(subclass) ⇒ Object
- .records ⇒ Object
Instance Method Summary collapse
- #_assign_attribute(k, v) ⇒ Object private
- #assign_attributes(new_attributes) {|_self| ... } ⇒ Object (also: #attributes=) private
-
#attribute_names ⇒ Object
Returns an array of names for the attributes available on this object.
-
#attribute_present?(attribute) ⇒ Boolean
Returns
trueif the specifiedattributehas been set and is neithernilnorempty?(the latter only applies to objects that respond toempty?, most notably Strings). - #delete ⇒ Object (also: #destroy)
-
#freeze ⇒ Object
Will not allow attributes to be changed.
-
#has_attribute?(attr_name) ⇒ Boolean
Returns
trueif the given attribute is in the attributes hash, otherwisefalse. -
#initialize(attributes = {}, &block) ⇒ Base
constructor
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes.
- #inspect ⇒ Object
-
#new_record? ⇒ Boolean
Returns true if this object hasn’t been saved yet; otherwise, returns false.
-
#persisted? ⇒ Boolean
Indicates if the model is persisted.
- #save(*_args) ⇒ Object (also: #save!)
-
#slice(*methods) ⇒ Object
Returns a hash of the given methods with their names as keys and returned values as values.
- #update(attributes = {}) ⇒ Object
Methods included from Queries
all, average, count, delete_all, find, find_by, find_by!, find_or_create_by, find_or_initialize_by, limit, maximum, minimum, none, order, reverse_order, sum, update_all, where
Methods included from AliasAttribute
attribute_alias, attribute_alias?
Methods included from TemplateMethods
Methods included from DoNothingActiveRecordMethods
#destroyed?, #errors, included, #marked_for_destruction?, #readonly?, #reload, #valid?
Methods included from MockAbilities
#clear_mocked_methods, included, prepended
Methods included from MockAbilities::InstanceAndClassMethods
#clear_mock, #clear_mocked_methods, #mock_instance_method
Constructor Details
#initialize(attributes = {}, &block) ⇒ Base
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes.
Example:
# Instantiates a single new object
UserMock.new(first_name: 'Jamie')
171 172 173 174 175 176 177 |
# File 'lib/active_mocker/mock/base.rb', line 171 def initialize(attributes = {}, &block) if self.class.abstract_class? raise NotImplementedError, "#{self.class.name} is an abstract class and cannot be instantiated." end setup_instance_variables assign_attributes(attributes, &block) end |
Instance Attribute Details
#_create_caller_locations ⇒ Object
163 164 165 |
# File 'lib/active_mocker/mock/base.rb', line 163 def _create_caller_locations @_create_caller_locations end |
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
161 162 163 |
# File 'lib/active_mocker/mock/base.rb', line 161 def associations @associations end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
161 162 163 |
# File 'lib/active_mocker/mock/base.rb', line 161 def attributes @attributes end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
161 162 163 |
# File 'lib/active_mocker/mock/base.rb', line 161 def types @types end |
Class Method Details
.__new_relation__(collection) ⇒ ScopeRelation
Returns for the given mock so that it will include any scoped methods.
114 115 116 |
# File 'lib/active_mocker/mock/base.rb', line 114 def __new_relation__(collection) ScopeRelation.new(collection) end |
._find_associations_by_class(klass_name) ⇒ Object
127 128 129 |
# File 'lib/active_mocker/mock/base.rb', line 127 def _find_associations_by_class(klass_name) associations_by_class[klass_name.to_s] end |
.abstract_class? ⇒ Boolean
99 100 101 |
# File 'lib/active_mocker/mock/base.rb', line 99 def abstract_class? true end |
.build_type(type) ⇒ Object
103 104 105 106 |
# File 'lib/active_mocker/mock/base.rb', line 103 def build_type(type) @@built_types ||= {} @@built_types[type] ||= Virtus::Attribute.build(type) end |
.classes(klass) ⇒ Object
108 109 110 |
# File 'lib/active_mocker/mock/base.rb', line 108 def classes(klass) ActiveMocker::LoadedMocks.find(klass) end |
.clear_mock ⇒ Object
123 124 125 |
# File 'lib/active_mocker/mock/base.rb', line 123 def clear_mock delete_all end |
.create(attributes = {}, &block) ⇒ Object Also known as: create!
Creates an object (or multiple objects) and saves it to memory.
The attributes parameter can be either a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.
Examples
# Create a single new object
User.create(first_name: 'Jamie')
# Create an Array of new objects
User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }])
# Create a single object and pass it into a block to set other attributes.
User.create(first_name: 'Jamie') do |u|
u.is_admin = false
end
# Creating an Array of new objects using a block, where the block is executed for each object:
User.create([{ first_name: 'Jamie' }, { first_name: 'Jeremy' }]) do |u|
u.is_admin = false
end
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_mocker/mock/base.rb', line 35 def create(attributes = {}, &block) if attributes.is_a?(Array) attributes.collect { |attr| create(attr, &block) } else record = new(id: attributes.delete(:id) || attributes.delete("id")) record.save record.assign_attributes(attributes, &block) record._create_caller_locations = caller_locations record end end |
.delete(id) ⇒ Object
Delete an object (or multiple objects) that has the given id.
This essentially finds the object (or multiple objects) with the given id and then calls delete on it.
Parameters
-
id- Can be either an Integer or an Array of Integers.
Examples
# Destroy a single object
TodoMock.delete(1)
# Destroy multiple objects
todos = [1,2,3]
TodoMock.delete(todos)
74 75 76 77 78 79 80 |
# File 'lib/active_mocker/mock/base.rb', line 74 def delete(id) if id.is_a?(Array) id.map { |one_id| delete(one_id) } else find(id).delete end end |
.delete_all(conditions = nil) ⇒ Object Also known as: destroy_all
Deletes the records matching conditions.
Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all
87 88 89 90 |
# File 'lib/active_mocker/mock/base.rb', line 87 def delete_all(conditions = nil) return records.reset if conditions.nil? super end |
.destroy ⇒ Object
Delete an object (or multiple objects) that has the given id.
This essentially finds the object (or multiple objects) with the given id and then calls delete on it.
Parameters
-
id- Can be either an Integer or an Array of Integers.
Examples
# Destroy a single object
TodoMock.delete(1)
# Destroy multiple objects
todos = [1,2,3]
TodoMock.delete(todos)
82 83 84 85 86 87 88 |
# File 'lib/active_mocker/mock/base.rb', line 82 def delete(id) if id.is_a?(Array) id.map { |one_id| delete(one_id) } else find(id).delete end end |
.from_limit? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
95 96 97 |
# File 'lib/active_mocker/mock/base.rb', line 95 def from_limit? false end |
.inherited(subclass) ⇒ Object
9 10 11 |
# File 'lib/active_mocker/mock/base.rb', line 9 def self.inherited(subclass) return ActiveMocker::LoadedMocks.send(:add, subclass) if subclass.superclass == Base end |
.records ⇒ Object
49 50 51 |
# File 'lib/active_mocker/mock/base.rb', line 49 def records @records ||= Records.new end |
Instance Method Details
#_assign_attribute(k, v) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
207 208 209 210 211 212 213 214 215 |
# File 'lib/active_mocker/mock/base.rb', line 207 def _assign_attribute(k, v) public_send("#{k}=", v) rescue NoMethodError if respond_to?("#{k}=") raise else raise UnknownAttributeError.new(self, k) end end |
#assign_attributes(new_attributes) {|_self| ... } ⇒ Object Also known as: attributes=
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/active_mocker/mock/base.rb', line 192 def assign_attributes(new_attributes) yield self if block_given? unless new_attributes.respond_to?(:stringify_keys) raise ArgumentError, "When assigning attributes, you must pass a hash as an argument." end return nil if new_attributes.blank? attributes = new_attributes.stringify_keys attributes.each do |k, v| _assign_attribute(k, v) end end |
#attribute_names ⇒ Object
Returns an array of names for the attributes available on this object.
person = Person.new
person.attribute_names
# => ["id", "created_at", "updated_at", "name", "age"]
287 288 289 |
# File 'lib/active_mocker/mock/base.rb', line 287 def attribute_names self.class.attribute_names end |
#attribute_present?(attribute) ⇒ Boolean
Returns true if the specified attribute has been set and is neither nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings). Otherwise, false. Note that it always returns true with boolean attributes.
person = Task.new(title: '', is_done: false)
person.attribute_present?(:title) # => false
person.attribute_present?(:is_done) # => true
person.name = 'Francesco'
person.is_done = true
person.attribute_present?(:title) # => true
person.attribute_present?(:is_done) # => true
272 273 274 275 |
# File 'lib/active_mocker/mock/base.rb', line 272 def attribute_present?(attribute) value = read_attribute(attribute) !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end |
#delete ⇒ Object Also known as: destroy
230 231 232 |
# File 'lib/active_mocker/mock/base.rb', line 230 def delete records.delete(self) end |
#freeze ⇒ Object
Will not allow attributes to be changed
Will freeze attributes forever. Querying for the record again will not unfreeze it because records exist in memory and are not initialized upon a query. This behaviour differs from ActiveRecord, beware of any side effect this may have when using this method.
300 301 302 |
# File 'lib/active_mocker/mock/base.rb', line 300 def freeze @attributes.freeze; self end |
#has_attribute?(attr_name) ⇒ Boolean
Returns true if the given attribute is in the attributes hash, otherwise false.
person = Person.new
person.has_attribute?(:name) # => true
person.has_attribute?('age') # => true
person.has_attribute?(:nothing) # => false
257 258 259 |
# File 'lib/active_mocker/mock/base.rb', line 257 def has_attribute?(attr_name) @attributes.key?(attr_name.to_s) end |
#inspect ⇒ Object
291 292 293 |
# File 'lib/active_mocker/mock/base.rb', line 291 def inspect ObjectInspect.new(self.class.name, attributes).to_s end |
#new_record? ⇒ Boolean
Returns true if this object hasn’t been saved yet; otherwise, returns false.
239 240 241 |
# File 'lib/active_mocker/mock/base.rb', line 239 def new_record? records.new_record?(self) end |
#persisted? ⇒ Boolean
Indicates if the model is persisted. Default is false.
person = Person.new(id: 1, name: 'bob')
person.persisted? # => false
247 248 249 |
# File 'lib/active_mocker/mock/base.rb', line 247 def persisted? records.persisted?(id) end |
#save(*_args) ⇒ Object Also known as: save!
217 218 219 220 |
# File 'lib/active_mocker/mock/base.rb', line 217 def save(*_args) self.class.send(:insert, self) unless self.class.exists?(self) true end |
#slice(*methods) ⇒ Object
Returns a hash of the given methods with their names as keys and returned values as values.
278 279 280 |
# File 'lib/active_mocker/mock/base.rb', line 278 def slice(*methods) Hash[methods.map! { |method| [method, public_send(method)] }].with_indifferent_access end |
#update(attributes = {}) ⇒ Object
187 188 189 |
# File 'lib/active_mocker/mock/base.rb', line 187 def update(attributes = {}) assign_attributes(attributes) end |