Sha256: 25f3f3cd0b1cdb7613214bbe6cce1666667d478f5f494427656fe90922e15ca5

Contents?: true

Size: 962 Bytes

Versions: 2

Compression:

Stored size: 962 Bytes

Contents

module Troo
  class ListPersistence
    class << self
      def for(resource, options = {})
        new(resource, options).persist
      end
    end

    def initialize(resource, options = {})
      @resource = resource
      @options = options
    end

    def persist
      return local   if local_identical?
      return updated if local_exists?
      return created
    end

    private
    attr_reader :resource, :options

    def created
      Troo::List.create(remote)
    end

    def updated
      local.update(remote) && local
    end

    def local_identical?
      return false unless local_exists?
      return false if local_data != remote
      true
    end

    def local_data
      local.external_attributes
    end

    def local_exists?
      !!local
    end

    def local
      @local ||= Troo::List.first(external_list_id: resource.id)
    end

    def remote
      @remote ||= Troo::External::ListAdaptor.adapt(resource)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
troo-0.0.3 lib/troo/models/list_persistence.rb
troo-0.0.2 lib/troo/models/list_persistence.rb