Class: ActiveRecordCompose::InnerModel

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

Defined Under Namespace

Modules: PackagePrivate

Instance Method Summary collapse

Constructor Details

#initialize(model, destroy: false, if: nil) ⇒ 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.

  • if (Proc) (defaults to: nil)

    evaluation result is false, it will not be included in the renewal.



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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)

    returns true on destroy, false on save.



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.

Returns:

  • (Boolean)

    if true, exclude from 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

Returns:

  • (Boolean)


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

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.



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

Returns:

  • (Boolean)


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

def valid? = !invalid?