Sha256: ccec4656a19ede77e23e059339bb48bc6d09a54f98f2873927bf882f6bcb2353

Contents?: true

Size: 980 Bytes

Versions: 1

Compression:

Stored size: 980 Bytes

Contents

module Troo
  module Persistence
    class Resource
      attr_reader :resource, :options

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

        def with_collection(resources, options = {})
          resources.map do |resource|
            new(resource, options).persist
          end
        end
      end

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

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

      private

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

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

      def local_data
        local.external_attributes
      end

      def local_exists?
        !!local
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
troo-0.0.8 lib/troo/models/persistence/resource.rb