Class: ActiveRecordCompose::InnerModel
- Inherits:
-
Object
- Object
- ActiveRecordCompose::InnerModel
- Defined in:
- lib/active_record_compose/inner_model.rb
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if equivalent.
-
#__raw_model ⇒ Object
Returns a model instance of raw, but it should be noted that application developers are not expected to use this interface.
-
#destroy_context? ⇒ Boolean
Determines whether to save or delete the target object.
-
#initialize(model, destroy: false) ⇒ 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) ⇒ InnerModel
Returns a new instance of InnerModel.
10 11 12 13 |
# File 'lib/active_record_compose/inner_model.rb', line 10 def initialize(model, destroy: false) @model = model @destroy_context_type = destroy end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if equivalent.
62 63 64 65 66 67 |
# File 'lib/active_record_compose/inner_model.rb', line 62 def ==(other) return false unless self.class == other.class return false unless __raw_model == other.__raw_model # steep:ignore true end |
#__raw_model ⇒ Object
Returns a model instance of raw, but it should be noted that application developers are not expected to use this interface.
74 |
# File 'lib/active_record_compose/inner_model.rb', line 74 def __raw_model = model |
#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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/active_record_compose/inner_model.rb', line 26 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 |
#invalid? ⇒ Boolean
53 |
# File 'lib/active_record_compose/inner_model.rb', line 53 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?`.
45 |
# File 'lib/active_record_compose/inner_model.rb', line 45 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?`.
50 |
# File 'lib/active_record_compose/inner_model.rb', line 50 def save! = destroy_context? ? model.destroy! : model.save! |
#valid? ⇒ Boolean
56 |
# File 'lib/active_record_compose/inner_model.rb', line 56 def valid? = !invalid? |