Sha256: b5f79f6211f44831f92ef0bd2777f976258631953f684b396f885c3f4bbc0cd8

Contents?: true

Size: 997 Bytes

Versions: 2

Compression:

Stored size: 997 Bytes

Contents

module Troo
  module Persistence
    class Local
      class << self
        def with_collection(resources = [])
          resources.map do |resource|
            new(resource).preprocess
          end
        end

        def persist(resource)
          new(resource).persist
        end
      end

      def initialize(resource)
        @resource = resource
      end

      def preprocess
        resource.preprocess
      end

      def persist
        set_default
        delete
        create
      end

      private

      attr_reader :resource

      def set_default
        remote.merge!(default: true) if default?
      end

      def delete
        local.delete if exists?
      end

      def create
        resource.local_model.create(remote)
      end

      def remote
        resource.adapted
      end

      def default?
        exists? && local.default?
      end

      def exists?
        !!local
      end

      def local
        resource.local
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
troo-0.0.10 lib/troo/persistence/local.rb
troo-0.0.9 lib/troo/persistence/local.rb