Class: Julewire::Core::Records::Draft

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/julewire/core/records/draft.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, lineage:) ⇒ Draft

Returns a new instance of Draft.



109
110
111
112
# File 'lib/julewire/core/records/draft.rb', line 109

def initialize(data, lineage:)
  @data = data
  @lineage = lineage
end

Class Method Details

.build(input = nil, context:, scope:, attributes: nil, neutral: nil, carry: nil, static_labels: nil, error_backtrace_lines: DEFAULT_ERROR_BACKTRACE_LINES, invalid_severity_reporter: Diagnostics::InvalidSeverityReporter) ⇒ Object

rubocop:disable Metrics/ParameterLists -- Record construction has fixed public sections.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/julewire/core/records/draft.rb', line 14

def build( # rubocop:disable Metrics/ParameterLists -- Record construction has fixed public sections.
  input = nil,
  context:,
  scope:,
  attributes: nil,
  neutral: nil,
  carry: nil,
  static_labels: nil,
  error_backtrace_lines: DEFAULT_ERROR_BACKTRACE_LINES,
  invalid_severity_reporter: Diagnostics::InvalidSeverityReporter
)
  build_with(
    input,
    context: context,
    neutral: neutral,
    attributes: attributes,
    carry: carry,
    static_labels: static_labels,
    scope: scope,
    invalid_severity_reporter: invalid_severity_reporter,
    fields_owned: false,
    input_owned: false,
    error_backtrace_lines: error_backtrace_lines
  )
end

.build_pipeline_owned(input, context:, scope:, attributes: nil, neutral: nil, carry: nil, input_owned: false, error_backtrace_lines: DEFAULT_ERROR_BACKTRACE_LINES, invalid_severity_reporter: Diagnostics::InvalidSeverityReporter) ⇒ Object

rubocop:disable Metrics/ParameterLists -- Record construction has fixed public sections.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/julewire/core/records/draft.rb', line 40

def build_pipeline_owned( # rubocop:disable Metrics/ParameterLists -- Record construction has fixed public sections.
  input,
  context:,
  scope:,
  attributes: nil,
  neutral: nil,
  carry: nil,
  input_owned: false,
  error_backtrace_lines: DEFAULT_ERROR_BACKTRACE_LINES,
  invalid_severity_reporter: Diagnostics::InvalidSeverityReporter
)
  build_with(
    input,
    context: context,
    neutral: neutral,
    attributes: attributes,
    carry: carry,
    static_labels: nil,
    scope: scope,
    invalid_severity_reporter: invalid_severity_reporter,
    fields_owned: true,
    input_owned: input_owned,
    error_backtrace_lines: error_backtrace_lines
  )
end

.from_normalized_hash(data, lineage: nil) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/julewire/core/records/draft.rb', line 89

def from_normalized_hash(data, lineage: nil)
  Record.validate_normalized_hash!(data)
  normalized = Fields::FieldSet.deep_dup_owned(data)
  execution = normalized.fetch(:execution)
  lineage ||= Execution::Lineage.from_execution_hash(execution)
  normalized[:execution] = Execution::Lineage.clean_normalized_lazy_relationship_hash(execution)
  new(normalized, lineage: lineage)
end

.from_record(record) ⇒ Object



98
99
100
101
# File 'lib/julewire/core/records/draft.rb', line 98

def from_record(record)
  Record.validate_normalized!(record)
  from_normalized_hash(record.to_h, lineage: record.lineage)
end

Instance Method Details

#[](key) ⇒ Object



114
# File 'lib/julewire/core/records/draft.rb', line 114

def [](key) = @data[key]

#[]=(key, value) ⇒ Object



116
117
118
119
# File 'lib/julewire/core/records/draft.rb', line 116

def []=(key, value)
  @data[key] = value
  @lineage = nil if key == :execution
end

#digObject



123
# File 'lib/julewire/core/records/draft.rb', line 123

def dig(...) = @data.dig(...)

#eachObject



127
# File 'lib/julewire/core/records/draft.rb', line 127

def each(&) = @data.each(&)

#each_keyObject



129
# File 'lib/julewire/core/records/draft.rb', line 129

def each_key(&) = @data.each_key(&)

#fetchObject



121
# File 'lib/julewire/core/records/draft.rb', line 121

def fetch(...) = @data.fetch(...)

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


125
# File 'lib/julewire/core/records/draft.rb', line 125

def key?(key) = @data.key?(key)

#lineageObject



170
# File 'lib/julewire/core/records/draft.rb', line 170

def lineage = (@lineage ||= Execution::Lineage.from_execution_hash(fetch(:execution)))

#to_hObject



131
# File 'lib/julewire/core/records/draft.rb', line 131

def to_h = Fields::FieldSet.deep_dup_owned(@data)

#to_recordObject



172
173
174
175
176
177
178
179
180
# File 'lib/julewire/core/records/draft.rb', line 172

def to_record
  return @to_record if @to_record

  record = Record.from_owned_hash(@data, lineage: @lineage)
  @lineage = record.lineage
  @to_record = record
  freeze
  record
end

#transform_field!(key) ⇒ Object



133
134
135
136
137
138
# File 'lib/julewire/core/records/draft.rb', line 133

def transform_field!(key)
  ensure_unfinalized!
  validate_transform_key!(key, Record::REQUIRED_KEYS, kind: :field)
  replace_transformed_field!(key, yield(self[key]))
  self
end

#transform_record!Object



151
152
153
154
155
156
157
158
159
# File 'lib/julewire/core/records/draft.rb', line 151

def transform_record!
  ensure_unfinalized!
  previous_lineage = @lineage
  previous_identity = execution_lineage_identity(fetch(:execution))
  replacement = yield(@data)
  @lineage = replacement_lineage_for(previous_lineage, previous_identity, replacement)
  @data = replacement.dup
  self
end

#transform_section!(key) ⇒ Object

Raises:

  • (TypeError)


140
141
142
143
144
145
146
147
148
149
# File 'lib/julewire/core/records/draft.rb', line 140

def transform_section!(key)
  ensure_unfinalized!
  validate_transform_key!(key, Record::HASH_SECTIONS, kind: :section)
  section = self[key]
  replacement = yield(section)
  raise TypeError, "record #{key} must be a Hash" unless replacement.is_a?(Hash)

  replace_transformed_field!(key, replacement)
  self
end

#validate!Object



165
166
167
168
# File 'lib/julewire/core/records/draft.rb', line 165

def validate!
  Record.validate_normalized_hash!(@data)
  self
end