Sha256: b0a6c8351004309c2b07671af8a610d387bb308cea29a9ee892ecd5882e1a57b

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

module Troo
  module Retrieval
    class Local
      attr_reader :id

      class << self
        def all(klass)
          new(klass).all
        end

        def default(klass, options = {})
          new(klass, nil, options).default
        end

        def retrieve(klass, id = nil, options = {})
          new(klass, id, options).retrieve
        end
      end

      def initialize(klass, id = nil, options = {})
        @klass, @id, @options = klass, id, options
      end

      def all
        klass.all
      end

      def default
        klass.default
      end

      def retrieve
        return default unless id
        by_short_id || by_id || by_external_id || optional_remote
      end

      private

      attr_reader :klass

      def optional_remote
        remote if allow_remote?
      end

      def allow_remote?
        Troo.configuration.allow_remote &&
          options.fetch(:allow_remote)
      end

      def options
        defaults.merge!(@options)
      end

      def defaults
        { allow_remote: true }
      end

      def by_id
        klass[id]
      end

      def by_short_id
        klass.first(short_id_criteria)
      end

      def by_external_id
        klass.by_external_id(id)
      end

      def remote
        Retrieval::Remote.fetch(klass.remote, id, options).first
      end

      def short_id_criteria
        return special if card? && default_board_exists?
        normal
      end

      def normal
        { short_id: id }
      end

      def special
        normal.merge!(board_criteria)
      end

      def board_criteria
        { external_board_id: Troo::Board.default.external_id }
      end

      def card?
        klass.type.eql?(:card)
      end

      def default_board_exists?
        !!(Troo::Board.default)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
troo-0.0.9 lib/troo/retrieval/local.rb