Class: ActiveRecordCompose::InnerModel
- Inherits:
-
Object
- Object
- ActiveRecordCompose::InnerModel
- Defined in:
- lib/active_record_compose/inner_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) ⇒ InnerModel
constructor
A new instance of InnerModel.
- #invalid? ⇒ Boolean
-
#save ⇒ Boolean
Execute save or destroy.
-
#save! ⇒ Object
Execute save or destroy.
- #valid? ⇒ Boolean
Constructor Details
#initialize(model, destroy: false, if: nil) ⇒ InnerModel
Returns a new instance of InnerModel.
11 12 13 14 15 |
# File 'lib/active_record_compose/inner_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.
80 81 82 83 84 85 |
# File 'lib/active_record_compose/inner_model.rb', line 80 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 InnerModel 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/inner_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/inner_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? ⇒ Boolean
71 |
# File 'lib/active_record_compose/inner_model.rb', line 71 def invalid? = destroy_context? ? false : model.invalid? |
#save ⇒ 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 |
# File 'lib/active_record_compose/inner_model.rb', line 63 def save = destroy_context? ? model.destroy : model.save |
#save! ⇒ 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?`.
68 |
# File 'lib/active_record_compose/inner_model.rb', line 68 def save! = destroy_context? ? model.destroy! : model.save! |
#valid? ⇒ Boolean
74 |
# File 'lib/active_record_compose/inner_model.rb', line 74 def valid? = !invalid? |