Module: Stubberry::ActiveRecord::ClassMethods
- Defined in:
- lib/stubberry/active_record.rb
Instance Method Summary collapse
- #__define_stub_method(object, method, val_or_callable, *block_args, **block_kwargs) ⇒ Object
- #__extend_any(_obj) ⇒ Object
- #__revert_all_methods(id, method) ⇒ Object
- #__stub_method_name(method, obj: nil, id: nil) ⇒ Object
- #__stubbed_objects(method_name) ⇒ Object
-
#stub_orm_attr(id, obj_or_attributes, &block) ⇒ Object
This method could be used whenever there is a need for stubbing the exact ActiveRecord object attributes inside some execution flow __WITHOUT__!! underlying record change.
-
#stub_orm_method(id, method, val_or_callable, *block_args, **block_kwargs, &block) ⇒ Object
This method could be used whenever there is a need for stubbing the specific Active Record object’s methods inside some flow piece with ANY way of object retrieval.
Instance Method Details
#__define_stub_method(object, method, val_or_callable, *block_args, **block_kwargs) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/stubberry/active_record.rb', line 48 def __define_stub_method(object, method, val_or_callable, *block_args, **block_kwargs) method_new_name = __stub_method_name(method, obj: object) Stubberry.__define_method_mimic_replacement(object, method) object.singleton_class.alias_method(method_new_name, method) object.define_singleton_method(method) do |*args, **kwargs, &blk| if val_or_callable.respond_to?(:call) val_or_callable.call(*args, **kwargs, &blk) else blk&.call(*block_args, **block_kwargs) val_or_callable end end __stubbed_objects(method_new_name) << object end |
#__extend_any(_obj) ⇒ Object
85 86 87 |
# File 'lib/stubberry/active_record.rb', line 85 def __extend_any(_obj) :do_nothing end |
#__revert_all_methods(id, method) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/stubberry/active_record.rb', line 71 def __revert_all_methods(id, method) method_new_name = __stub_method_name(method, id: id) __stubbed_objects(method_new_name).map(&:singleton_class).each do || .send(:undef_method, method) .send(:alias_method, method, method_new_name) .send(:undef_method, method_new_name) end end |
#__stub_method_name(method, obj: nil, id: nil) ⇒ Object
66 67 68 69 |
# File 'lib/stubberry/active_record.rb', line 66 def __stub_method_name(method, obj: nil, id: nil) # __stub_class_method_id "__stub_#{self}_#{method}_#{obj&.id || id}" end |
#__stubbed_objects(method_name) ⇒ Object
81 82 83 |
# File 'lib/stubberry/active_record.rb', line 81 def __stubbed_objects(method_name) (@@__extended_objects ||= {})[method_name] ||= [] # rubocop:disable Style/ClassVars end |
#stub_orm_attr(id, obj_or_attributes, &block) ⇒ Object
This method could be used whenever there is a need for stubbing the exact ActiveRecord object attributes inside some execution flow __WITHOUT__!! underlying record change
27 28 29 30 31 32 33 |
# File 'lib/stubberry/active_record.rb', line 27 def stub_orm_attr(id, obj_or_attributes, &block) stub(:__extend_any, ->(obj) { return unless obj.id == id && obj.is_a?(self) obj.assign_attributes(obj_or_attributes.try(:attributes) || obj_or_attributes) }, &block) end |
#stub_orm_method(id, method, val_or_callable, *block_args, **block_kwargs, &block) ⇒ Object
This method could be used whenever there is a need for stubbing the specific Active Record object’s methods inside some flow piece with ANY way of object retrieval
38 39 40 41 42 43 44 45 46 |
# File 'lib/stubberry/active_record.rb', line 38 def stub_orm_method(id, method, val_or_callable, *block_args, **block_kwargs, &block) stub(:__extend_any, ->(obj) { return unless obj.id == id && obj.is_a?(self) __define_stub_method(obj, method, val_or_callable, *block_args, **block_kwargs) }, &block) ensure __revert_all_methods(id, method) end |