class Synchronisable::Input::Parser
Responsible for guessing the user input format.
@api private
Public Class Methods
new(model, synchronizer)
click to toggle source
# File lib/synchronisable/input/parser.rb, line 11 def initialize(model, synchronizer) @model = model @synchronizer = synchronizer end
Public Instance Methods
parse(data)
click to toggle source
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
# File lib/synchronisable/input/parser.rb, line 22 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 Instance Methods
find_by_array_of_ids(input)
click to toggle source
# File lib/synchronisable/input/parser.rb, line 50 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
find_by_local_id(id)
click to toggle source
# File lib/synchronisable/input/parser.rb, line 55 def find_by_local_id(id) import = @model.where(id: id).first.try(:import) import ? @synchronizer.find(import.remote_id) : nil end
find_imports(class_name, ids)
click to toggle source
# File lib/synchronisable/input/parser.rb, line 60 def find_imports(class_name, ids) case class_name when 'Fixnum' ids.map { |id| @model.where(id: id).first.try(&:import) } when 'String' ids.map { |id| Import.where(id: id).first } end end
find_or_fetch_by_params(params)
click to toggle source
# File lib/synchronisable/input/parser.rb, line 45 def find_or_fetch_by_params(params) sync_method = params.key?(:id) ? :find : :fetch @synchronizer.send(sync_method, params) end