Class: YamlExporter::Nodes::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_exporter/nodes/attribute.rb

Overview

A single declared column on the current record.

Phase: :pre_save (assigns a value on record before record.save!).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, owner_class: nil) ⇒ Attribute

owner_class may be a Class or a 0-arity callable. The callable form matches how the rest of the node tree defers reflection lookups — needed by the anonymous AR classes in dsl_validation_test.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yaml_exporter/nodes/attribute.rb', line 14

def initialize(name:, owner_class: nil)
  @name = name.to_sym
  @owner_class_resolver =
    if owner_class.nil?
      nil
    elsif owner_class.respond_to?(:call)
      owner_class
    else
      -> { owner_class }
    end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/yaml_exporter/nodes/attribute.rb', line 9

def name
  @name
end

Instance Method Details

#export(record, exporter:) ⇒ Object

text columns export as YAML literal block scalars (|); we signal that by wrapping the string value in LiteralString. string/varchar columns stay inline regardless of length.



45
46
47
48
49
# File 'lib/yaml_exporter/nodes/attribute.rb', line 45

def export(record, exporter:)
  value = record.public_send(@name)
  value = LiteralString.new(value) if value.is_a?(::String) && text_column?
  [@name.to_s, value]
end

#import(record, data, path:, importer:) ⇒ Object

Missing key, explicit null, and provided value all normalize to the same AR setter call — so "no author" and "author: null" mean the same thing downstream.



37
38
39
40
# File 'lib/yaml_exporter/nodes/attribute.rb', line 37

def import(record, data, path:, importer:)
  value = data.key?(@name.to_s) ? data[@name.to_s] : nil
  record.public_send("#{@name}=", value)
end

#phaseObject



26
27
28
# File 'lib/yaml_exporter/nodes/attribute.rb', line 26

def phase
  :pre_save
end

#schema_fragmentObject



51
52
53
# File 'lib/yaml_exporter/nodes/attribute.rb', line 51

def schema_fragment
  { @name => { type: TypeInference.schema_type_for(owner_class, @name) } }
end

#yaml_keysObject



30
31
32
# File 'lib/yaml_exporter/nodes/attribute.rb', line 30

def yaml_keys
  [@name.to_s]
end