Sha256: 74423e7afce578a664f9e161dc483fa84dd4de20689bdfc48efcfa7e6cca5624

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module ObjectAttorney
  module ORM

    def id
      represented_object.try(:id)
    end
  
    def new_record?
      try_or_return(represented_object, :new_record?, true)
    end

    def persisted?
      try_or_return(represented_object, :persisted?, false)
    end

    def save
      save!(:save)
    end

    def save!(save_method = :save!)
      before_save
      save_result = valid? ? save_after_validations(save_method) : false
      after_save if valid? && save_result
      save_result
    end

    def destroy
      return true if represented_object.blank?
      evoke_method_on_object(represented_object, :destroy)
    end

    def call_save_or_destroy(object, save_method)
      if object == self
        represented_object.present? ? evoke_method_on_object(represented_object, save_method) : true
      else
        save_method = :destroy if check_if_marked_for_destruction?(object)
        evoke_method_on_object(object, save_method)
      end
    end

    protected #################### PROTECTED METHODS DOWN BELOW ######################

    def before_save; end
    def after_save; end

    def save_after_validations(save_method)
      submit(save_method)
    end

    def submit(save_method)
      save_result = save_represented_object(save_method)
      save_result = save_nested_objects(save_method) if save_result
      save_result
    end

    def save_represented_object(save_method)
      return true if represented_object.blank?
      call_save_or_destroy(represented_object, save_method)
    end

    private #################### PRIVATE METHODS DOWN BELOW ######################

    def check_if_marked_for_destruction?(object)
      object.respond_to?(:marked_for_destruction?) ? object.marked_for_destruction? : false
    end

    def evoke_method_on_object(object, method)
      object.send(method)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
object_attorney-1.1.2 lib/object_attorney/orm.rb