Sha256: fad7d1a5068240c5f47eec975a8207169ba99260dd820588ecbe8bd080307300

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module Troo
  module Retrieval
    class Remote
      attr_reader :external_id

      class << self
        # @param  []
        # @param  []
        # @param  [Hash]
        # @return []
        def fetch(klass, external_id, options = {})
          new(klass, external_id, options).fetch
        end
      end

      # @param  []
      # @param  []
      # @param  [Hash]
      # @return [Troo::Retrieval::Remote]
      def initialize(klass, external_id, options = {})
        @klass       = klass
        @external_id = external_id
        @options     = options
      end

      # @return [Array]
      def fetch
        return []        if none?
        return persist   if persist?
        resources
      end

      private

      attr_reader :klass

      def none?
        resources.empty?
      end

      def persist
        @persisted ||= Troo::Persistence::Local
          .with_collection(resources)
      end

      def resources
        @resources ||= API::Client.perform(parameters)
      end

      def parameters
        {
          verb:          :get,
          endpoint:      nil,
          interpolation: interpolation,
          query:         {},
          model:         klass
        }.merge!(resource_parameters)
      end

      def resource_parameters
        modes.fetch(mode)
      end

      def modes
        {
          all:    klass.all,
          board:  klass.by_board_id,
          list:   klass.by_list_id,
          card:   klass.by_card_id,
          member: klass.by_member_id,
          none:   {}
        }
      end

      def mode
        options.fetch(:mode, :none)
      end

      def persist?
        options.fetch(:persist)
      end

      def options
        remote_options.merge!(@options)
      end

      def remote_options
        defaults.merge!(klass.remote_options)
      end

      def defaults
        { mode: :none, persist: true }
      end

      def interpolation
        { external_id: external_id }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
troo-0.0.15 lib/troo/retrieval/remote.rb
troo-0.0.14 lib/troo/retrieval/remote.rb
troo-0.0.13 lib/troo/retrieval/remote.rb