Class: YamlExporter::Nodes::ManyReference
- Inherits:
-
Object
- Object
- YamlExporter::Nodes::ManyReference
- Defined in:
- lib/yaml_exporter/nodes/many_reference.rb
Overview
many :assoc, find_by: :column — standalone reference list (HABTM or
has_many). No block, no entry attributes: each YAML string resolves to
an existing target record via find_by. Target records are never
created or mutated; only the association is updated.
Phase: :post_save (join rows / FK need parent.id).
Instance Attribute Summary collapse
-
#find_by ⇒ Object
readonly
Returns the value of attribute find_by.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#owner_class ⇒ Object
readonly
Returns the value of attribute owner_class.
Instance Method Summary collapse
- #export(record, exporter:) ⇒ Object
- #import(record, data, path:, importer:) ⇒ Object
-
#initialize(name:, owner_class:, find_by:) ⇒ ManyReference
constructor
A new instance of ManyReference.
- #phase ⇒ Object
- #schema_fragment ⇒ Object
- #target_class ⇒ Object
- #yaml_keys ⇒ Object
Constructor Details
#initialize(name:, owner_class:, find_by:) ⇒ ManyReference
Returns a new instance of ManyReference.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 14 def initialize(name:, owner_class:, find_by:) @name = name.to_sym @owner_class = owner_class @find_by = find_by.to_sym reflection = owner_class.reflect_on_association(@name) unless reflection raise ArgumentError, "`many #{name.inspect}`: #{owner_class} has no association `#{name}`" end end |
Instance Attribute Details
#find_by ⇒ Object (readonly)
Returns the value of attribute find_by.
12 13 14 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 12 def find_by @find_by end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 12 def name @name end |
#owner_class ⇒ Object (readonly)
Returns the value of attribute owner_class.
12 13 14 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 12 def owner_class @owner_class end |
Instance Method Details
#export(record, exporter:) ⇒ Object
55 56 57 58 59 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 55 def export(record, exporter:) targets = Array(record.public_send(@name)) keys = targets.map { |t| t.public_send(@find_by) }.sort [@name.to_s, keys] end |
#import(record, data, path:, importer:) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 37 def import(record, data, path:, importer:) raw = data.key?(@name.to_s) ? data[@name.to_s] : nil values = Array(raw) unless values.all? { |v| v.is_a?(String) || v.is_a?(Symbol) || v.is_a?(Numeric) } raise UnknownAttributeError, "#{describe_path(path)}: expected a list of #{@find_by} values, got #{raw.inspect}" end targets = values.map do |value| target_class.find_by(@find_by => value) || (raise ActiveRecord::RecordNotFound, "no #{target_class} with #{@find_by}=#{value.inspect}") end record.public_send("#{@name}=", targets) end |
#phase ⇒ Object
25 26 27 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 25 def phase :post_save end |
#schema_fragment ⇒ Object
61 62 63 64 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 61 def schema_fragment item_type = TypeInference.schema_type_for(target_class, @find_by) { @name => { type: 'array', items: { type: item_type } } } end |
#target_class ⇒ Object
33 34 35 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 33 def target_class @target_class ||= @owner_class.reflect_on_association(@name).klass end |
#yaml_keys ⇒ Object
29 30 31 |
# File 'lib/yaml_exporter/nodes/many_reference.rb', line 29 def yaml_keys [@name.to_s] end |