Class: ActiveMocker::Mock::Base
- Inherits:
-
Object
- Object
- ActiveMocker::Mock::Base
- Extended by:
- Queries
- Defined in:
- lib/active_mocker/mock/base.rb
Defined Under Namespace
Modules: PropertiesGetterAndSetter Classes: ScopeRelation
Instance Attribute Summary collapse
-
#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
- .build_type(type) ⇒ Object
- .classes(klass) ⇒ Object
- .clear_mock ⇒ Object
-
.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
- .new_relation(collection) ⇒ Object
- .records ⇒ Object
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #_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)
-
#has_attribute?(attr_name) ⇒ Boolean
Returns
trueif the given attribute is in the attributes hash, otherwisefalse. - #hash ⇒ Object
-
#initialize(attributes = {}, &block) ⇒ Base
constructor
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names).
- #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!)
- #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, order, reverse_order, sum, update_all, where
Methods included from TemplateMethods
Methods included from MockAbilities
#clear_mocked_methods, included
Methods included from MockAbilities::InstanceAndClassMethods
#clear_mocked_methods, #mock_instance_method
Methods included from DoNothingActiveRecordMethods
#destroyed?, #errors, included, #marked_for_destruction?, #readonly?, #reload, #valid?
Constructor Details
#initialize(attributes = {}, &block) ⇒ Base
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table – hence you can’t have attributes that aren’t part of the table columns.
Example:
# Instantiates a single new object
UserMock.new(first_name: 'Jamie')
140 141 142 143 |
# File 'lib/active_mocker/mock/base.rb', line 140 def initialize(attributes = {}, &block) setup_instance_variables assign_attributes(attributes, &block) end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
130 131 132 |
# File 'lib/active_mocker/mock/base.rb', line 130 def associations @associations end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
130 131 132 |
# File 'lib/active_mocker/mock/base.rb', line 130 def attributes @attributes end |
#types ⇒ Object (readonly)
Returns the value of attribute types.
130 131 132 |
# File 'lib/active_mocker/mock/base.rb', line 130 def types @types end |
Class Method Details
.build_type(type) ⇒ Object
101 102 103 |
# File 'lib/active_mocker/mock/base.rb', line 101 def build_type(type) Virtus::Attribute.build(type) end |
.classes(klass) ⇒ Object
105 106 107 |
# File 'lib/active_mocker/mock/base.rb', line 105 def classes(klass) ActiveMocker::LoadedMocks.find(klass) end |
.clear_mock ⇒ Object
117 118 119 120 |
# File 'lib/active_mocker/mock/base.rb', line 117 def clear_mock clear_mocked_methods 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
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_mocker/mock/base.rb', line 38 def create(attributes = {}, &block) if attributes.is_a?(Array) attributes.collect { |attr| create(attr, &block) } else record = new record.save record.assign_attributes(attributes, &block) 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)
76 77 78 79 80 81 82 |
# File 'lib/active_mocker/mock/base.rb', line 76 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
89 90 91 92 |
# File 'lib/active_mocker/mock/base.rb', line 89 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)
84 85 86 87 88 89 90 |
# File 'lib/active_mocker/mock/base.rb', line 84 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.
97 98 99 |
# File 'lib/active_mocker/mock/base.rb', line 97 def from_limit? false end |
.inherited(subclass) ⇒ Object
10 11 12 13 |
# File 'lib/active_mocker/mock/base.rb', line 10 def self.inherited(subclass) return ActiveMocker::LoadedMocks.send(:add, subclass) if subclass.superclass == Base ActiveMocker::LoadedMocks.send(:add_subclass, subclass) end |
.new_relation(collection) ⇒ Object
109 110 111 |
# File 'lib/active_mocker/mock/base.rb', line 109 def new_relation(collection) ScopeRelation.new(collection) end |
Instance Method Details
#==(obj) ⇒ Object
263 264 265 266 267 |
# File 'lib/active_mocker/mock/base.rb', line 263 def ==(obj) return false if obj.nil? return hash == obj.attributes.hash if obj.respond_to?(:attributes) hash == obj.hash if obj.respond_to?(:hash) end |
#_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.
173 174 175 176 177 178 179 180 181 |
# File 'lib/active_mocker/mock/base.rb', line 173 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.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/active_mocker/mock/base.rb', line 158 def assign_attributes(new_attributes, &block) 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"]
251 252 253 |
# File 'lib/active_mocker/mock/base.rb', line 251 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
241 242 243 244 |
# File 'lib/active_mocker/mock/base.rb', line 241 def attribute_present?(attribute) value = read_attribute(attribute) !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end |
#delete ⇒ Object Also known as: destroy
199 200 201 |
# File 'lib/active_mocker/mock/base.rb', line 199 def delete records.delete(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
226 227 228 |
# File 'lib/active_mocker/mock/base.rb', line 226 def has_attribute?(attr_name) @attributes.has_key?(attr_name.to_s) end |
#hash ⇒ Object
259 260 261 |
# File 'lib/active_mocker/mock/base.rb', line 259 def hash attributes.hash end |
#inspect ⇒ Object
255 256 257 |
# File 'lib/active_mocker/mock/base.rb', line 255 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.
208 209 210 |
# File 'lib/active_mocker/mock/base.rb', line 208 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
216 217 218 |
# File 'lib/active_mocker/mock/base.rb', line 216 def persisted? records.persisted?(id) end |
#save(*args) ⇒ Object Also known as: save!
183 184 185 186 187 188 |
# File 'lib/active_mocker/mock/base.rb', line 183 def save(*args) unless self.class.exists?(self) self.class.send(:insert, self) end true end |
#update(attributes = {}) ⇒ Object
153 154 155 |
# File 'lib/active_mocker/mock/base.rb', line 153 def update(attributes={}) assign_attributes(attributes) end |