Class: YamlExporter::Nodes::OneOwned
- Inherits:
-
Object
- Object
- YamlExporter::Nodes::OneOwned
- Defined in:
- lib/yaml_exporter/nodes/one_owned.rb
Overview
one :assoc do ... end — owned has_one child. The current record owns
the child row; missing/null in YAML means "destroy the child".
Phase: :post_save (the child carries parent_id, so parent must be
persisted first).
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#owner_class ⇒ Object
readonly
Returns the value of attribute owner_class.
-
#sub_structure ⇒ Object
readonly
Returns the value of attribute sub_structure.
Instance Method Summary collapse
- #export(record, exporter:) ⇒ Object
- #import(record, data, path:, importer:) ⇒ Object
-
#initialize(name:, owner_class:, &block) ⇒ OneOwned
constructor
A new instance of OneOwned.
- #phase ⇒ Object
- #schema_fragment ⇒ Object
- #target_class ⇒ Object
- #yaml_keys ⇒ Object
Constructor Details
#initialize(name:, owner_class:, &block) ⇒ OneOwned
Returns a new instance of OneOwned.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 13 def initialize(name:, owner_class:, &block) raise ArgumentError, "`one #{name.inspect}`: block required" unless block @name = name.to_sym @owner_class = owner_class reflection = owner_class.reflect_on_association(@name) unless reflection raise ArgumentError, "`one #{name.inspect}`: #{owner_class} has no association `#{name}`" end @sub_structure = Builder.new(-> { target_class }, &block).build end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 11 def name @name end |
#owner_class ⇒ Object (readonly)
Returns the value of attribute owner_class.
11 12 13 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 11 def owner_class @owner_class end |
#sub_structure ⇒ Object (readonly)
Returns the value of attribute sub_structure.
11 12 13 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 11 def sub_structure @sub_structure end |
Instance Method Details
#export(record, exporter:) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 52 def export(record, exporter:) child = record.public_send(@name) return [@name.to_s, nil] if child.nil? [@name.to_s, exporter.build_hash(child, @sub_structure)] end |
#import(record, data, path:, importer:) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 39 def import(record, data, path:, importer:) child_data = data.key?(@name.to_s) ? data[@name.to_s] : nil if child_data.nil? destroy_existing(record) return end child = find_or_build_child(record) importer.apply(child, @sub_structure, child_data, path: child_path(path)) cache_target(record, child) end |
#phase ⇒ Object
27 28 29 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 27 def phase :post_save end |
#schema_fragment ⇒ Object
59 60 61 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 59 def schema_fragment { @name => Schema.generate(@sub_structure) } end |
#target_class ⇒ Object
35 36 37 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 35 def target_class @target_class ||= @owner_class.reflect_on_association(@name).klass end |
#yaml_keys ⇒ Object
31 32 33 |
# File 'lib/yaml_exporter/nodes/one_owned.rb', line 31 def yaml_keys [@name.to_s] end |