Sha256: 5df013727e57a94bfe0c0c62dda476b57091e1a1a5375ed1db29878aedf95edb

Contents?: true

Size: 1.92 KB

Versions: 14

Compression:

Stored size: 1.92 KB

Contents

require 'synchronisable/input/descriptor'

module Synchronisable
  module Input
    # Responsible for guessing the user input format.
    #
    # @api private
    #
    # @see Synchronisable::Input::Parser
    class Parser
      def initialize(model, synchronizer)
        @model = model
        @synchronizer = synchronizer
      end

      # Parses the user input.
      #
      # @param data [Array<Hash>, Array<String>, Array<Integer>, String, Integer]
      #   synchronization data to handle.
      #
      # @return [Array<Hash>] array of hashes with remote attributes
      def parse(data)
        input = Descriptor.new(data)

        result = case
                 when input.empty?
                   @synchronizer.fetch
                 when input.params?
                   find_or_fetch_by_params(input.data)
                 when input.remote_id?
                   @synchronizer.find(data)
                 when input.local_id?
                   find_by_local_id(data)
                 when input.array_of_ids?
                   find_by_array_of_ids(input)
                 else
                   result = data.dup
                 end

        [result].flatten.compact
      end

      private

      def find_or_fetch_by_params(params)
        sync_method = params.key?(:id) ? :find : :fetch
        @synchronizer.send(sync_method, params)
      end

      def find_by_array_of_ids(input)
        records = find_imports(input.element_class.name, input.data)
        records.map { |r| @synchronizer.find(r.remote_id) }
      end

      def find_by_local_id(id)
        import = @model.find_by(id: id).try(:import)
        import ? @synchronizer.find(import.remote_id) : nil
      end

      def find_imports(class_name, ids)
        case class_name
        when 'Fixnum'
          ids.map { |id| @model.find_by(id: id).try(&:import) }
        when 'String'
          ids.map { |id| Import.find_by(id: id) }
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
synchronisable-1.2.7 lib/synchronisable/input/parser.rb
synchronisable-1.2.6 lib/synchronisable/input/parser.rb
synchronisable-1.2.5 lib/synchronisable/input/parser.rb
synchronisable-1.2.4 lib/synchronisable/input/parser.rb
synchronisable-1.2.3 lib/synchronisable/input/parser.rb
synchronisable-1.2.2 lib/synchronisable/input/parser.rb
synchronisable-1.2.1 lib/synchronisable/input/parser.rb
synchronisable-1.2.0 lib/synchronisable/input/parser.rb
synchronisable-1.1.9 lib/synchronisable/input/parser.rb
synchronisable-1.1.8 lib/synchronisable/input/parser.rb
synchronisable-1.1.7 lib/synchronisable/input/parser.rb
synchronisable-1.1.6 lib/synchronisable/input/parser.rb
synchronisable-1.1.5 lib/synchronisable/input/parser.rb
synchronisable-1.1.4 lib/synchronisable/input/parser.rb