Class: ActiveRecordCompose::InnerModel

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_compose/inner_model.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, destroy: false) ⇒ InnerModel

Returns a new instance of InnerModel.

Parameters:

  • model (Object)

    the model instance.

  • destroy (Boolean) (defaults to: false)

    given true, destroy model.

  • destroy (Proc) (defaults to: false)

    when proc returning true, destroy model.



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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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_modelObject

Returns a model instance of raw, but it should be noted that application developers are not expected to use this interface.

Returns:

  • (Object)

    raw model instance



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.

Returns:

  • (Boolean)

    returns true on destroy, false on save.



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

Returns:

  • (Boolean)


53
# File 'lib/active_record_compose/inner_model.rb', line 53

def invalid? = destroy_context? ? false : model.invalid?

#saveBoolean

Execute save or destroy. Returns true on success, false on failure. Whether save or destroy is executed depends on the value of ‘#destroy_context?`.

Returns:

  • (Boolean)

    returns true on success, false on failure.



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

Returns:

  • (Boolean)


56
# File 'lib/active_record_compose/inner_model.rb', line 56

def valid? = !invalid?