Class: ActiveRecordCompose::WrappedModel
- Inherits:
-
Object
- Object
- ActiveRecordCompose::WrappedModel
- Defined in:
- lib/active_record_compose/wrapped_model.rb
Defined Under Namespace
Modules: PackagePrivate
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if equivalent.
-
#destroy_context? ⇒ Boolean
Determines whether to save or delete the target object.
-
#ignore? ⇒ Boolean
Returns a boolean indicating whether or not to exclude the user from the update.
-
#initialize(model, destroy: false, if: nil) ⇒ WrappedModel
constructor
A new instance of WrappedModel.
- #invalid?(context = nil) ⇒ Boolean
-
#save(**options) ⇒ Boolean
Execute save or destroy.
-
#save!(**options) ⇒ Object
Execute save or destroy.
- #valid?(context = nil) ⇒ Boolean
Constructor Details
#initialize(model, destroy: false, if: nil) ⇒ WrappedModel
Returns a new instance of WrappedModel.
11 12 13 14 15 |
# File 'lib/active_record_compose/wrapped_model.rb', line 11 def initialize(model, destroy: false, if: nil) @model = model @destroy_context_type = destroy @if_option = binding.local_variable_get(:if) end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if equivalent.
104 105 106 107 108 109 |
# File 'lib/active_record_compose/wrapped_model.rb', line 104 def ==(other) return false unless self.class == other.class return false unless model == other.model true end |
#destroy_context? ⇒ Boolean
Determines whether to save or delete the target object. Depends on the destroy value of the WrappedModel object initialization option.
On the other hand, there are values mark_for_destruction and marked_for_destruction? in ActiveRecord. However, these values are not substituted here. These values only work if the autosave option is enabled for the parent model, and are not appropriate for other cases.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_record_compose/wrapped_model.rb', line 28 def destroy_context? d = destroy_context_type if d.is_a?(Proc) if d.arity == 0 # @type var d: ^() -> bool !!d.call else # @type var d: ^(_ARLike) -> bool !!d.call(model) end else !!d end end |
#ignore? ⇒ Boolean
Returns a boolean indicating whether or not to exclude the user from the update.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_record_compose/wrapped_model.rb', line 46 def ignore? i = if_option if i.nil? false elsif i.arity == 0 # @type var i: ^() -> bool !i.call else # @type var i: ^(_ARLike) -> bool !i.call(model) end end |
#invalid?(context = nil) ⇒ Boolean
95 |
# File 'lib/active_record_compose/wrapped_model.rb', line 95 def invalid?(context = nil) = !valid?(context) |
#save(**options) ⇒ Boolean
Execute save or destroy. Returns true on success, false on failure. Whether save or destroy is executed depends on the value of #destroy_context?.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_record_compose/wrapped_model.rb', line 63 def save(**) # While errors caused by the type check are avoided, # it is important to note that an error can still occur # if `#destroy_context?` returns true but ar_like does not implement `#destroy`. m = model if destroy_context? # @type var m: ActiveRecordCompose::_ARLikeWithDestroy m.destroy else # @type var m: ActiveRecordCompose::_ARLike m.save(**) end end |
#save!(**options) ⇒ Object
Execute save or destroy. Unlike #save, an exception is raises on failure. Whether save or destroy is executed depends on the value of #destroy_context?.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_record_compose/wrapped_model.rb', line 80 def save!(**) # While errors caused by the type check are avoided, # it is important to note that an error can still occur # if `#destroy_context?` returns true but ar_like does not implement `#destroy`. m = model if destroy_context? # @type var m: ActiveRecordCompose::_ARLikeWithDestroy m.destroy! else # @type var model: ActiveRecordCompose::_ARLike m.save!(**) end end |
#valid?(context = nil) ⇒ Boolean
98 |
# File 'lib/active_record_compose/wrapped_model.rb', line 98 def valid?(context = nil) = destroy_context? || model.valid?(context) |