Class: INatGet::Data::Parser::Part::Ancestry Private

Inherits:
Links show all
Defined in:
lib/inat-get/data/parsers/defs/ancestry.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary

Attributes inherited from INatGet::Data::Parser::Part

#parser

Instance Method Summary collapse

Constructor Details

#initialize(parser, name, source: nil, source_ids: nil) ⇒ Ancestry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Ancestry.



7
8
9
# File 'lib/inat-get/data/parsers/defs/ancestry.rb', line 7

def initialize parser, name, source: nil, source_ids: nil
  super parser, name, model: nil, source: source, source_ids: source_ids
end

Instance Method Details

#parse(target, source) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/inat-get/data/parsers/defs/ancestry.rb', line 11

def parse target, source
  @model ||= parser.model
  data = source[@source]
  values = if data
    @model.parser.parse! data.reject { |v| v[:id] == target.id }
  else
    ids = source[@source_ids] || []
    ids.delete target.id
    @model.manager.get(*ids)
  end
  ids = values.map(&:id)
  ids << target.id unless ids.include?(target.id)
  field = @parser.model.association_reflection(@name)[:pks_setter_method].to_s.chomp("=").gsub('_setter', '').to_sym
  { field => ids }
end