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: nil, freeze_sections: true) ⇒ Draft

Returns a new instance of Draft.



159
160
161
162
163
164
# File 'lib/julewire/core/records/draft.rb', line 159

def initialize(data, lineage: nil, freeze_sections: true)
  @data = data
  @freeze_sections = freeze_sections
  @lineage = lineage || Execution::Lineage.from_execution_hash(@data[:execution])
  validate!
end

Class Method Details

.build(input = {}, context:, scope:, attributes: {}, neutral: {}, carry: {}, static_labels: {}, freeze_sections: true, error_backtrace_lines: Core::MAX_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
39
40
# File 'lib/julewire/core/records/draft.rb', line 14

def build( # rubocop:disable Metrics/ParameterLists -- Record construction has fixed public sections.
  input = {},
  context:,
  scope:,
  attributes: {},
  neutral: {},
  carry: {},
  static_labels: {},
  freeze_sections: true,
  error_backtrace_lines: Core::MAX_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,
    options: BuildOptions.defensive(
      freeze_sections: freeze_sections,
      error_backtrace_lines: error_backtrace_lines
    )
  )
end

.build_pipeline_owned(input = {}, context:, scope:, attributes: {}, neutral: {}, carry: {}, static_labels: {}, input_owned: false, freeze_sections: true, error_backtrace_lines: Core::MAX_BACKTRACE_LINES, invalid_severity_reporter: Diagnostics::InvalidSeverityReporter) ⇒ Object

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



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

def build_pipeline_owned( # rubocop:disable Metrics/ParameterLists -- Record construction has fixed public sections.
  input = {},
  context:,
  scope:,
  attributes: {},
  neutral: {},
  carry: {},
  static_labels: {},
  input_owned: false,
  freeze_sections: true,
  error_backtrace_lines: Core::MAX_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,
    options: BuildOptions.pipeline_owned(
      input_owned: input_owned,
      freeze_sections: freeze_sections,
      error_backtrace_lines: error_backtrace_lines
    )
  )
end

.from_normalized_hash(data, lineage: nil, freeze_sections: true) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/julewire/core/records/draft.rb', line 92

def from_normalized_hash(data, lineage: nil, freeze_sections: true)
  normalized = Fields::FieldSet.deep_symbolize_keys(data)
  lineage ||= Execution::Lineage.from_execution_hash(normalized[:execution])
  normalized[:execution] = Execution::Lineage.clean_lazy_relationship_hash(normalized[:execution])
  normalized = Fields::Internal.frozen_deep_symbolize_keys(normalized) if freeze_sections
  new(
    normalized,
    lineage: lineage,
    freeze_sections: freeze_sections
  )
end

.from_record(record, freeze_sections: true) ⇒ Object



104
105
106
107
# File 'lib/julewire/core/records/draft.rb', line 104

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

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



168
169
170
171
172
173
# File 'lib/julewire/core/records/draft.rb', line 168

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

#digObject



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

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

#eachObject



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

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

#each_keyObject



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

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

#fetchObject



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lineageObject



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

def lineage = (@lineage ||= Execution::Lineage.from_execution_hash(@data[:execution]))

#to_hObject



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

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

#to_recordObject



224
225
226
# File 'lib/julewire/core/records/draft.rb', line 224

def to_record
  @to_record ||= Record.from_owned_hash(@data, lineage: @lineage, trust_frozen: @freeze_sections)
end

#transform_field!(key) ⇒ Object



187
188
189
190
191
# File 'lib/julewire/core/records/draft.rb', line 187

def transform_field!(key)
  key = Fields::Internal.normalize_key(key)
  replace_transformed_field!(key, yield(@data[key]))
  self
end

#transform_record!Object



203
204
205
206
207
208
209
210
211
# File 'lib/julewire/core/records/draft.rb', line 203

def transform_record!
  previous_lineage = @lineage
  previous_identity = execution_lineage_identity(@data[:execution])
  replacement = yield(@data)
  @lineage = replacement_lineage_for(previous_lineage, previous_identity, replacement)
  @data = replacement
  @to_record = nil
  self
end

#transform_section!(key) ⇒ Object

Raises:

  • (TypeError)


193
194
195
196
197
198
199
200
201
# File 'lib/julewire/core/records/draft.rb', line 193

def transform_section!(key)
  key = Fields::Internal.normalize_key(key)
  section = @data[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



217
218
219
220
# File 'lib/julewire/core/records/draft.rb', line 217

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