Class: Micdrop::RootRecordContext

Inherits:
RecordContext show all
Defined in:
lib/micdrop/record_context.rb

Overview

Record context for root-level

Instance Attribute Summary collapse

Attributes inherited from RecordContext

#record

Instance Method Summary collapse

Methods inherited from RecordContext

#at_css, #at_xpath, #collect_format_string, #collect_kv, #collect_list, #css, #each_subrecord, #index, #passthru, #skip, #static, #stop, #take, #take_content, #take_dig, #take_node_name, #take_whole, #try_take, #xpath

Constructor Details

#initialize(source, sink, loop_item, loop_index = nil) ⇒ RootRecordContext

Returns a new instance of RootRecordContext.



131
132
133
134
135
136
137
138
139
140
# File 'lib/micdrop/record_context.rb', line 131

def initialize(source, sink, loop_item, loop_index = nil)
  @source = source
  @sink = sink
  @loop_item = loop_item
  @record = loop_item
  @loop_index = loop_index
  @before_flush = nil
  @after_flush = nil
  reset
end

Instance Attribute Details

#collectorObject (readonly)

Returns the value of attribute collector.



142
143
144
# File 'lib/micdrop/record_context.rb', line 142

def collector
  @collector
end

#loop_indexObject (readonly)

Returns the value of attribute loop_index.



142
143
144
# File 'lib/micdrop/record_context.rb', line 142

def loop_index
  @loop_index
end

#loop_itemObject (readonly)

Returns the value of attribute loop_item.



142
143
144
# File 'lib/micdrop/record_context.rb', line 142

def loop_item
  @loop_item
end

#sinkObject (readonly)

Returns the value of attribute sink.



142
143
144
# File 'lib/micdrop/record_context.rb', line 142

def sink
  @sink
end

#sourceObject (readonly)

Returns the value of attribute source.



142
143
144
# File 'lib/micdrop/record_context.rb', line 142

def source
  @source
end

Instance Method Details

#after_flush(&block) ⇒ Object

Allows specifying a hook which will run after flush. The block will receive the record and the collector.

Note that this must be called before any manual flush occurs to have any effect.



206
207
208
# File 'lib/micdrop/record_context.rb', line 206

def after_flush(&block)
  @after_flush = block
end

#before_flush(&block) ⇒ Object

Allows specifying a hook which will run before flush. The block will receive the record and the collector.

Note that this must be called before any manual flush occurs to have any effect.



198
199
200
# File 'lib/micdrop/record_context.rb', line 198

def before_flush(&block)
  @before_flush = block
end

#dump_collector(prefix = nil) ⇒ Object

Debug tool to print the current sink collector to the console



176
177
178
179
180
181
# File 'lib/micdrop/record_context.rb', line 176

def dump_collector(prefix = nil)
  puts prefix unless prefix.nil?
  puts @collector.inspect
  puts "\n"
  self
end

#flush(reset: true) ⇒ Object

Flush all currently put values to the sink, optionally resetting as well.



165
166
167
168
169
170
171
172
# File 'lib/micdrop/record_context.rb', line 165

def flush(reset: true)
  return unless @dirty

  @before_flush&.call self, @collector
  @sink << @collector
  @after_flush&.call self, @collector
  self.reset if reset
end

#put(name, value) ⇒ Object

Put a value in the sink.

You typically won't use this directly.



148
149
150
151
# File 'lib/micdrop/record_context.rb', line 148

def put(name, value)
  @collector[name] = value
  @dirty = true
end

#put_bury(keys, value) ⇒ Object

Put a value in the sink, using nested keys. (This is an inverse of :dig)

You typically won't use this directly.



157
158
159
160
161
# File 'lib/micdrop/record_context.rb', line 157

def put_bury(keys, value)
  sb = StructureBuilder.new @collector
  sb.bury value, *keys
  @dirty = true
end

#resetObject

Clear the collection of currently-put values.



185
186
187
188
189
190
191
192
# File 'lib/micdrop/record_context.rb', line 185

def reset
  @dirty = false
  @collector = if @sink.respond_to? :make_collector
                 @sink.make_collector
               else
                 {}
               end
end