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.



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

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
103
# File 'lib/julewire/core/records/draft.rb', line 92

def from_normalized_hash(data, lineage: nil, freeze_sections: true)
  Record.validate_normalized_hash!(data)
  normalized = data.dup
  lineage ||= Execution::Lineage.from_execution_hash(normalized[:execution])
  normalized[:execution] = Execution::Lineage.clean_normalized_lazy_relationship_hash(normalized[:execution])
  normalized = Fields::Internal.frozen_owned_copy(normalized) if freeze_sections
  new(
    normalized,
    lineage: lineage,
    freeze_sections: freeze_sections
  )
end

.from_record(record, freeze_sections: true) ⇒ Object



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

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



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

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

#[]=(key, value) ⇒ Object



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

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

#digObject



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

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

#eachObject



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

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

#each_keyObject



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

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

#fetchObject



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lineageObject



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

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

#to_hObject



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

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

#to_recordObject



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

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

#transform_field!(key) ⇒ Object



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

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

#transform_record!Object



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

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)


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

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



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

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