Class: Micdrop::ItemContext
- Inherits:
-
Object
- Object
- Micdrop::ItemContext
- Extended by:
- Forwardable
- Defined in:
- lib/micdrop/ext/sequel.rb,
lib/micdrop/ext/nokogiri.rb,
lib/micdrop/item_context.rb,
lib/micdrop/ext/microfocus.rb
Overview
Extend ItemContext with parse_microfocus
Constant Summary collapse
- @@registered_lookups =
{}
Instance Attribute Summary collapse
-
#original_value ⇒ Object
readonly
Returns the value of attribute original_value.
-
#record_context ⇒ Object
readonly
Returns the value of attribute record_context.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.register_lookup(name, lookup) ⇒ Object
Register a lookup, allowing it to be used in subsequent migrations.
Instance Method Summary collapse
-
#apply(pipeline) ⇒ Object
Run a predefined pipline on this Item.
-
#coalesce ⇒ Object
Filter for the first non-nil value in a list.
-
#compact ⇒ Object
Filter out all nil values from a list.
-
#convert(proc_or_symbol = nil, &block) ⇒ Object
Use plain Ruby code to modify this Item.
- #db_lookup(dataset, key_col, val_col = nil, pass_if_not_found: false, warn_if_not_found: nil, apply_if_not_found: nil) ⇒ Object
-
#decode_html ⇒ Object
Decode an HTML entity-encoded string to plain text.
-
#decode_html5 ⇒ Object
Decode an HTML5 entity-encoded string to plain text.
-
#decode_xml ⇒ Object
Decode an XML entity-encoded string to plain text.
-
#default(default_value) ⇒ Object
Provide a default value if the current value is nill.
-
#dump(prefix = nil) ⇒ Object
Debug tool to print the current value to the console.
-
#each_subrecord(flush: false, reset: false, &block) ⇒ Object
Iterate an array or array-like object and run a block in the subrecord context of each.
-
#empty_to_nil ⇒ Object
Treats empty strings as nil.
-
#encode(*encoding, **options) ⇒ Object
Re-encode a string in the given encoding.
-
#encode_html(nl2br: false) ⇒ Object
Encode a string using HTML entities.
-
#encode_html5(nl2br: false) ⇒ Object
Encode a string using HTML5 entities.
-
#encode_xml ⇒ Object
Encode a string using XML entities.
-
#enter(&block) ⇒ Object
Treat the current Item as a Record, allowing child objects to be Taken.
-
#extract(name) ⇒ Object
Similar to Take, but replaces the current value in the current scope.
-
#extract_dig(*keys) ⇒ Object
Extract using the :dig method instead of :[].
-
#filter(&predicate) ⇒ Object
Filter out values from a list based on a predicate.
-
#force_encoding(encoding) ⇒ Object
Change the encoding of the current string without transcoding.
-
#format_boolean(true_value = "Yes", false_value = "No") ⇒ Object
Format a boolean as a string.
-
#format_date(format = "%Y-%m-%d", zero_date: false) ⇒ Object
Format a date using a given format string.
-
#format_datetime(format = "%Y-%m-%d %H:%M:%S", zero_date: false) ⇒ Object
Format a datetime using a given format string.
-
#format_json ⇒ Object
Format the data as a JSON string.
-
#format_string(template = nil) ⇒ Object
Format the value into a string using sprintf-style formatting, or using
to_sif no template is provided. -
#initialize(record_context, value) ⇒ ItemContext
constructor
A new instance of ItemContext.
-
#join(delimiter) ⇒ Object
Join a list into a string.
-
#join_kv(kv_delimiter, item_delimiter = "\n") ⇒ Object
Join a hash into a string.
-
#lookup(mapping, pass_if_not_found: false, warn_if_not_found: nil, apply_if_not_found: nil) ⇒ Object
Lookup the value in a hash.
-
#map(&block) ⇒ Object
Map the values in an array using a block.
-
#map_apply(&block) ⇒ Object
Alternate version of map that takes a pipeline block which will be executed in an item context.
-
#pack(template) ⇒ Object
Pack binary data (using Array.pack).
-
#parse_boolean(true_values = [1, "1", "true", "True", "TRUE", "yes", "Yes", "YES", "on", "On", "ON", "Y", "y"], false_values = [0, "0", "false", "False", "FALSE", "no", "No", "NO", "off", "Off", "OFF", "N", "n", ""]) ⇒ Object
Parse a value into a boolean using a list of common values for true or false.
-
#parse_date(format = "%Y-%m-%d", zero_date: false) ⇒ Object
Parse a date using a given format string.
-
#parse_datetime(format = "%Y-%m-%d %H:%M:%S", zero_date: false) ⇒ Object
Parse a datetime using a given format string.
-
#parse_float ⇒ Object
Parse a value to a float.
-
#parse_html(&block) ⇒ Object
Parse HTML and enter a sub-record context for the root node.
-
#parse_html5(&block) ⇒ Object
Parse HTML5 and enter a sub-record context for the root node.
-
#parse_html5_fragment(&block) ⇒ Object
Parse an HTML5 fragment and enter a sub-record context for the root node.
-
#parse_html_fragment(&block) ⇒ Object
Parse an HTML fragment and enter a sub-record context for the root node.
-
#parse_int(base = 10) ⇒ Object
Parse a value to an integer.
-
#parse_json(&block) ⇒ Object
Parse a string as JSON.
-
#parse_microfocus(include_header: false, unpack_spec: nil, unpack_mapping: nil, &block) ⇒ Object
Parse a string as JSON.
-
#parse_xml(&block) ⇒ Object
Parse XML and enter a sub-record context for the root node.
-
#parse_xml_fragment(&block) ⇒ Object
Parse an XML fragment and enter a sub-record context for the root node.
-
#put(*args) ⇒ Object
Put the current value in the output record.
-
#put_bury(*keys) ⇒ Object
Put the value into a nested structure.
-
#regex(pattern, &block) ⇒ Object
Perform a regular expression match, setting the current value to the match data.
-
#scope(&block) ⇒ Object
Create a new item context with the same value as exists currently.
-
#send(*args) ⇒ Object
Common operations ###.
-
#split(delimiter, &block) ⇒ Object
Split a string according to a delimeter.
-
#split_kv(kv_delimiter, item_delimiter = "\n", &block) ⇒ Object
Split a string into a set of key/value pairs (as a hash) according to a set of delimiters.
-
#string_replace(find, replace) ⇒ Object
Perform a string replacement or regex replacement on the current value.
-
#strip ⇒ Object
Strip whitespace from a string.
-
#take(name, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.take.
-
#take_content(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.take_content.
-
#take_dig(*keys, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.take_dig.
-
#take_node_name(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.take_node_name.
-
#try_take(name, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.try_take.
-
#unpack(template, offset: 0) ⇒ Object
Unpack binary data (using String.unpack).
-
#update(value) ⇒ Object
Directly update the current value.
Constructor Details
#initialize(record_context, value) ⇒ ItemContext
Returns a new instance of ItemContext.
13 14 15 16 17 |
# File 'lib/micdrop/item_context.rb', line 13 def initialize(record_context, value) @record_context = record_context @value = value @original_value = value end |
Instance Attribute Details
#original_value ⇒ Object (readonly)
Returns the value of attribute original_value.
19 20 21 |
# File 'lib/micdrop/item_context.rb', line 19 def original_value @original_value end |
#record_context ⇒ Object (readonly)
Returns the value of attribute record_context.
19 20 21 |
# File 'lib/micdrop/item_context.rb', line 19 def record_context @record_context end |
#value ⇒ Object
Returns the value of attribute value.
20 21 22 |
# File 'lib/micdrop/item_context.rb', line 20 def value @value end |
Class Method Details
.register_lookup(name, lookup) ⇒ Object
Register a lookup, allowing it to be used in subsequent migrations
24 25 26 |
# File 'lib/micdrop/item_context.rb', line 24 def self.register_lookup(name, lookup) @@registered_lookups[name] = lookup end |
Instance Method Details
#apply(pipeline) ⇒ Object
Run a predefined pipline on this Item.
46 47 48 49 |
# File 'lib/micdrop/item_context.rb', line 46 def apply(pipeline) instance_eval(&pipeline) unless pipeline.nil? self end |
#coalesce ⇒ Object
Filter for the first non-nil value in a list
407 408 409 410 411 412 |
# File 'lib/micdrop/item_context.rb', line 407 def coalesce return self if @value.nil? @value = @value.compact.first self end |
#compact ⇒ Object
Filter out all nil values from a list
416 417 418 419 420 421 |
# File 'lib/micdrop/item_context.rb', line 416 def compact return self if @value.nil? @value = @value.compact self end |
#convert(proc_or_symbol = nil, &block) ⇒ Object
Use plain Ruby code to modify this Item.
37 38 39 40 41 42 |
# File 'lib/micdrop/item_context.rb', line 37 def convert(proc_or_symbol = nil, &block) proc_or_symbol = method(proc_or_symbol) if proc_or_symbol.is_a? Symbol @value = proc_or_symbol.call(@value) unless proc_or_symbol.nil? @value = block.call(@value) unless block.nil? self end |
#db_lookup(dataset, key_col, val_col = nil, pass_if_not_found: false, warn_if_not_found: nil, apply_if_not_found: nil) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/micdrop/ext/sequel.rb', line 111 def db_lookup(dataset, key_col, val_col = nil, pass_if_not_found: false, warn_if_not_found: nil, apply_if_not_found: nil) # TODO: allow registering db_lookups like we do normal lookups warn_if_not_found = true if warn_if_not_found.nil? && apply_if_not_found.nil? found = if val_col.nil? dataset.where(key_col => @value).first else dataset.where(key_col => @value).get(val_col) end if found.nil? warn format "Value %s not found in db_lookup", @value if warn_if_not_found if !apply_if_not_found.nil? apply apply_if_not_found elsif !pass_if_not_found @value = nil end else @value = found end self end |
#decode_html ⇒ Object
Decode an HTML entity-encoded string to plain text
65 66 67 68 69 70 71 |
# File 'lib/micdrop/ext/nokogiri.rb', line 65 def decode_html return self if @value.nil? frag = ::Nokogiri::HTML.fragment @value @value = frag.content self end |
#decode_html5 ⇒ Object
Decode an HTML5 entity-encoded string to plain text
87 88 89 90 91 92 93 |
# File 'lib/micdrop/ext/nokogiri.rb', line 87 def decode_html5 return self if @value.nil? frag = ::Nokogiri::HTML5.fragment @value @value = frag.content self end |
#decode_xml ⇒ Object
Decode an XML entity-encoded string to plain text
109 110 111 112 113 114 115 |
# File 'lib/micdrop/ext/nokogiri.rb', line 109 def decode_xml return self if @value.nil? frag = ::Nokogiri::XML.fragment @value @value = frag.content self end |
#default(default_value) ⇒ Object
Provide a default value if the current value is nill
314 315 316 317 |
# File 'lib/micdrop/item_context.rb', line 314 def default(default_value) @value = default_value if @value.nil? self end |
#dump(prefix = nil) ⇒ Object
Debug tool to print the current value to the console
138 139 140 141 142 143 |
# File 'lib/micdrop/item_context.rb', line 138 def dump(prefix = nil) puts prefix unless prefix.nil? puts @value puts "\n" self end |
#each_subrecord(flush: false, reset: false, &block) ⇒ Object
Iterate an array or array-like object and run a block in the subrecord context of each
Optionally flush and/or reset after each iteration. This is used to import multiple sink records from a single source record, such as, for example, a source record that contians a JSON list of multiple items.
462 463 464 465 466 467 468 469 470 471 |
# File 'lib/micdrop/item_context.rb', line 462 def each_subrecord(flush: false, reset: false, &block) rec_ctx = SubRecordContext.new self, @record_context @value.each do |v| item_ctx = ItemContext.new rec_ctx, v ctx = SubRecordContext.new item_ctx, rec_ctx ctx.instance_eval(&block) @record_context.flush reset: false if flush @record_context.reset if reset end end |
#empty_to_nil ⇒ Object
Treats empty strings as nil
307 308 309 310 |
# File 'lib/micdrop/item_context.rb', line 307 def empty_to_nil @value = nil if @value == "" self end |
#encode(*encoding, **options) ⇒ Object
Re-encode a string in the given encoding
Takes the same args and options as String.encode
339 340 341 342 |
# File 'lib/micdrop/item_context.rb', line 339 def encode(*encoding, **) @value = @value.encode(*encoding, **) unless value.nil? self end |
#encode_html(nl2br: false) ⇒ Object
Encode a string using HTML entities
75 76 77 78 79 80 81 82 83 |
# File 'lib/micdrop/ext/nokogiri.rb', line 75 def encode_html(nl2br: false) return self if @value.nil? frag = ::Nokogiri::HTML.fragment "" frag.content = @value @value = frag.to_s @value = @value.gsub "\n", "<br/>" if nl2br self end |
#encode_html5(nl2br: false) ⇒ Object
Encode a string using HTML5 entities
97 98 99 100 101 102 103 104 105 |
# File 'lib/micdrop/ext/nokogiri.rb', line 97 def encode_html5(nl2br: false) return self if @value.nil? frag = ::Nokogiri::HTML5.fragment "" frag.content = @value @value = frag.to_s @value = @value.gsub "\n", "<br/>" if nl2br self end |
#encode_xml ⇒ Object
Encode a string using XML entities
119 120 121 122 123 124 125 126 |
# File 'lib/micdrop/ext/nokogiri.rb', line 119 def encode_xml return self if @value.nil? frag = ::Nokogiri::XML.fragment "" frag.content = @value @value = frag.to_s self end |
#enter(&block) ⇒ Object
Treat the current Item as a Record, allowing child objects to be Taken.
53 54 55 56 57 |
# File 'lib/micdrop/item_context.rb', line 53 def enter(&block) ctx = SubRecordContext.new(self, @record_context) ctx.instance_eval(&block) unless block.nil? ctx end |
#extract(name) ⇒ Object
Similar to Take, but replaces the current value in the current scope
Can be used to take slices of arrays as well
90 91 92 93 94 95 |
# File 'lib/micdrop/item_context.rb', line 90 def extract(name) return self if @value.nil? @value = @value[name] self end |
#extract_dig(*keys) ⇒ Object
Extract using the :dig method instead of :[]
99 100 101 102 103 104 |
# File 'lib/micdrop/item_context.rb', line 99 def extract_dig(*keys) return self if @value.nil? @value = @value.dig(*keys) self end |
#filter(&predicate) ⇒ Object
Filter out values from a list based on a predicate
425 426 427 428 429 430 |
# File 'lib/micdrop/item_context.rb', line 425 def filter(&predicate) return self if @value.nil? @value = @value.filter(&predicate) self end |
#force_encoding(encoding) ⇒ Object
Change the encoding of the current string without transcoding
346 347 348 349 |
# File 'lib/micdrop/item_context.rb', line 346 def force_encoding(encoding) @value = @value.force_encoding(encoding) unless value.nil? self end |
#format_boolean(true_value = "Yes", false_value = "No") ⇒ Object
Format a boolean as a string
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/micdrop/item_context.rb', line 228 def format_boolean(true_value = "Yes", false_value = "No") if @value.nil? nil elsif @value @value = true_value else @value = false_value end self end |
#format_date(format = "%Y-%m-%d", zero_date: false) ⇒ Object
Format a date using a given format string
189 190 191 192 193 194 195 196 |
# File 'lib/micdrop/item_context.rb', line 189 def format_date(format = "%Y-%m-%d", zero_date: false) if @value.nil? && zero_date @value = make_zero_date format elsif !@value.nil? @value = @value.strftime(format) end self end |
#format_datetime(format = "%Y-%m-%d %H:%M:%S", zero_date: false) ⇒ Object
Format a datetime using a given format string
200 201 202 203 204 205 206 207 |
# File 'lib/micdrop/item_context.rb', line 200 def format_datetime(format = "%Y-%m-%d %H:%M:%S", zero_date: false) if @value.nil? && zero_date @value = make_zero_date format elsif !@value.nil? @value = @value.strftime(format) end self end |
#format_json ⇒ Object
Format the data as a JSON string
489 490 491 492 493 494 |
# File 'lib/micdrop/item_context.rb', line 489 def format_json return self if @value.nil? @value = JSON.generate @value self end |
#format_string(template = nil) ⇒ Object
Format the value into a string using sprintf-style formatting, or using to_s if no
template is provided.
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/micdrop/item_context.rb', line 242 def format_string(template = nil) return self if @value.nil? @value = if template.nil? @value.to_s else template % @value end self end |
#join(delimiter) ⇒ Object
Join a list into a string
367 368 369 370 |
# File 'lib/micdrop/item_context.rb', line 367 def join(delimiter) @value = @value.join(delimiter) unless @value.nil? self end |
#join_kv(kv_delimiter, item_delimiter = "\n") ⇒ Object
Join a hash into a string
391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/micdrop/item_context.rb', line 391 def join_kv(kv_delimiter, item_delimiter = "\n") return self if @value.nil? string = "" @value.each_pair do |k, v| string += item_delimiter if string != "" string += k.to_s + kv_delimiter + v end @value = string self end |
#lookup(mapping, pass_if_not_found: false, warn_if_not_found: nil, apply_if_not_found: nil) ⇒ Object
Lookup the value in a hash
pass_if_not_found, if true, will cause the value to pass through the lookup unchanged if no match is found. If false, the value will instead be set to nil.
apply_if_not_found, if provided, will be passed to an apply call if no match is found
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/micdrop/item_context.rb', line 269 def lookup(mapping, pass_if_not_found: false, warn_if_not_found: nil, apply_if_not_found: nil) return self if @value.nil? if mapping.is_a? Symbol mapping = @@registered_lookups.fetch mapping do |key| raise PipelineError, "No lookup '#{key}' found" end end warn_if_not_found = true if warn_if_not_found.nil? && apply_if_not_found.nil? @value = mapping.fetch @value do |v| warn format "Value %s not found in lookup", v if warn_if_not_found if !apply_if_not_found.nil? apply apply_if_not_found value elsif pass_if_not_found v end end self end |
#map(&block) ⇒ Object
Map the values in an array using a block
434 435 436 437 438 439 |
# File 'lib/micdrop/item_context.rb', line 434 def map(&block) return self if @value.nil? @value = @value.map(&block) self end |
#map_apply(&block) ⇒ Object
Alternate version of map that takes a pipeline block which will be executed in an item context.
This allows transforming individual items in a list using all of the micdrop operation methods.
445 446 447 448 449 450 451 452 453 454 |
# File 'lib/micdrop/item_context.rb', line 445 def map_apply(&block) return self if @value.nil? rec_ctx = SubRecordContext.new self, @record_context @value = @value.map do |v| item_ctx = ItemContext.new rec_ctx, v item_ctx.apply(block).value end self end |
#pack(template) ⇒ Object
Pack binary data (using Array.pack)
330 331 332 333 |
# File 'lib/micdrop/item_context.rb', line 330 def pack(template) @value = value.pack template unless @value.nil? self end |
#parse_boolean(true_values = [1, "1", "true", "True", "TRUE", "yes", "Yes", "YES", "on", "On", "ON", "Y", "y"], false_values = [0, "0", "false", "False", "FALSE", "no", "No", "NO", "off", "Off", "OFF", "N", "n", ""]) ⇒ Object
Parse a value into a boolean using a list of common values for true or false
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/micdrop/item_context.rb', line 211 def parse_boolean(true_values = [1, "1", "true", "True", "TRUE", "yes", "Yes", "YES", "on", "On", "ON", "Y", "y"], false_values = [0, "0", "false", "False", "FALSE", "no", "No", "NO", "off", "Off", "OFF", "N", "n", ""]) if true_values.include? @value @value = true elsif false_values.include? @value @value = false elsif @value.nil? nil else raise ValueError("Unrecognized value: {repr(value)}") end self end |
#parse_date(format = "%Y-%m-%d", zero_date: false) ⇒ Object
Parse a date using a given format string
167 168 169 170 171 172 173 174 |
# File 'lib/micdrop/item_context.rb', line 167 def parse_date(format = "%Y-%m-%d", zero_date: false) if zero_date zero = make_zero_date format @value = nil if @value == zero end @value = ::Date.strptime(@value, format) unless @value.nil? self end |
#parse_datetime(format = "%Y-%m-%d %H:%M:%S", zero_date: false) ⇒ Object
Parse a datetime using a given format string
178 179 180 181 182 183 184 185 |
# File 'lib/micdrop/item_context.rb', line 178 def parse_datetime(format = "%Y-%m-%d %H:%M:%S", zero_date: false) if zero_date zero = make_zero_date format @value = nil if @value == zero end @value = ::DateTime.strptime(@value, format) unless @value.nil? self end |
#parse_float ⇒ Object
Parse a value to a float
158 159 160 161 162 163 |
# File 'lib/micdrop/item_context.rb', line 158 def parse_float return self if @value.nil? @value = @value.to_f self end |
#parse_html(&block) ⇒ Object
Parse HTML and enter a sub-record context for the root node
23 24 25 26 |
# File 'lib/micdrop/ext/nokogiri.rb', line 23 def parse_html(&block) doc = @value.nil? ? nil : ::Nokogiri::HTML.parse(@value) nokogiri_node_subrecord_helper(doc, block) end |
#parse_html5(&block) ⇒ Object
Parse HTML5 and enter a sub-record context for the root node
30 31 32 33 |
# File 'lib/micdrop/ext/nokogiri.rb', line 30 def parse_html5(&block) doc = @value.nil? ? nil : ::Nokogiri::HTML5.parse(@value) nokogiri_node_subrecord_helper(doc, block) end |
#parse_html5_fragment(&block) ⇒ Object
Parse an HTML5 fragment and enter a sub-record context for the root node
51 52 53 54 |
# File 'lib/micdrop/ext/nokogiri.rb', line 51 def parse_html5_fragment(&block) doc = @value.nil? ? nil : ::Nokogiri::HTML5.fragment(@value) nokogiri_node_subrecord_helper(doc, block) end |
#parse_html_fragment(&block) ⇒ Object
Parse an HTML fragment and enter a sub-record context for the root node
44 45 46 47 |
# File 'lib/micdrop/ext/nokogiri.rb', line 44 def parse_html_fragment(&block) doc = @value.nil? ? nil : ::Nokogiri::HTML.fragment(@value) nokogiri_node_subrecord_helper(doc, block) end |
#parse_int(base = 10) ⇒ Object
Parse a value to an integer
149 150 151 152 153 154 |
# File 'lib/micdrop/item_context.rb', line 149 def parse_int(base = 10) return self if @value.nil? @value = @value.to_i(base) self end |
#parse_json(&block) ⇒ Object
Parse a string as JSON
If a block is provided, it will act as a record context where object properties can be taken.
479 480 481 482 483 484 485 |
# File 'lib/micdrop/item_context.rb', line 479 def parse_json(&block) return self if @value.nil? @value = JSON.parse @value enter(&block) unless block.nil? self end |
#parse_microfocus(include_header: false, unpack_spec: nil, unpack_mapping: nil, &block) ⇒ Object
Parse a string as JSON
If a block is provided, it will act as a record context where object properties can be taken.
If include_header is true, the value will be a hash containing both the header information and the actual records.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/micdrop/ext/microfocus.rb', line 217 def parse_microfocus(include_header: false, unpack_spec: nil, unpack_mapping: nil, &block) return self if @value.nil? reader = Micdrop::Ext::Microfocus::MicroFocusReader.new @value, unpack_spec: unpack_spec, unpack_mapping: unpack_mapping @value = if include_header { creation_time: reader.creation_time, compression: reader.compression, index_type: reader.index_type, variable_length: reader.variable_length, min_legth: reader.min_legth, max_length: reader.max_length, index_version: reader.index_version, records: reader.each.entries } else reader.each.entries end enter(&block) unless block.nil? self end |
#parse_xml(&block) ⇒ Object
Parse XML and enter a sub-record context for the root node
37 38 39 40 |
# File 'lib/micdrop/ext/nokogiri.rb', line 37 def parse_xml(&block) doc = @value.nil? ? nil : ::Nokogiri::XML.parse(@value) nokogiri_node_subrecord_helper(doc, block) end |
#parse_xml_fragment(&block) ⇒ Object
Parse an XML fragment and enter a sub-record context for the root node
58 59 60 61 |
# File 'lib/micdrop/ext/nokogiri.rb', line 58 def parse_xml_fragment(&block) doc = @value.nil? ? nil : Nokogiri::XML.fragment(@value) nokogiri_node_subrecord_helper(doc, block) end |
#put(*args) ⇒ Object
Put the current value in the output record.
Normally takes a single argument: the name to put the value under. However, a two-argument (name, value) form is also supported.
113 114 115 116 117 118 119 120 |
# File 'lib/micdrop/item_context.rb', line 113 def put(*args) if args.length == 1 @record_context.put args.first, @value else @record_context.put(*args) end self end |
#put_bury(*keys) ⇒ Object
Put the value into a nested structure.
This is the opposite of :dig, allowing you to build up structure on the fly without manually constructing arrays and hashes. (This does require the collector to be a simple array or hash.)
127 128 129 |
# File 'lib/micdrop/item_context.rb', line 127 def put_bury(*keys) @record_context.put_bury keys, @value end |
#regex(pattern, &block) ⇒ Object
Perform a regular expression match, setting the current value to the match data
If a block is provided, it will act as a record context where captured groups can be taken.
500 501 502 503 504 505 506 507 508 |
# File 'lib/micdrop/item_context.rb', line 500 def regex(pattern, &block) return self if @value.nil? v = pattern.match @value warn format "%s does not match %s", pattern.inspect, @value.inspect if v.nil? @value = v enter(&block) unless block.nil? self end |
#scope(&block) ⇒ Object
Create a new item context with the same value as exists currently. Allows operations in a scope that will not affect the value in the current scope.
80 81 82 83 84 |
# File 'lib/micdrop/item_context.rb', line 80 def scope(&block) ctx = ItemContext.new(@record_context, @value) ctx.apply block unless block.nil? ctx end |
#send(*args) ⇒ Object
Common operations ###
255 256 257 258 259 260 |
# File 'lib/micdrop/item_context.rb', line 255 def send(*args) return self if @value.nil? @value = @value.send(*args) self end |
#split(delimiter, &block) ⇒ Object
Split a string according to a delimeter.
Accepts an optional block in the record context of the newly created list of values.
357 358 359 360 361 362 363 |
# File 'lib/micdrop/item_context.rb', line 357 def split(delimiter, &block) return self if @value.nil? @value = @value.split(delimiter) enter(&block) unless block.nil? self end |
#split_kv(kv_delimiter, item_delimiter = "\n", &block) ⇒ Object
Split a string into a set of key/value pairs (as a hash) according to a set of delimiters.
Accepts an optional block in the record context of the newly created hash of values.
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/micdrop/item_context.rb', line 376 def split_kv(kv_delimiter, item_delimiter = "\n", &block) return self if @value.nil? kv = {} @value.each_line(item_delimiter, chomp: true) do |item| k, v = item.split(kv_delimiter, 2) kv[k] = v end @value = kv enter(&block) unless block.nil? self end |
#string_replace(find, replace) ⇒ Object
Perform a string replacement or regex replacement on the current value
293 294 295 296 |
# File 'lib/micdrop/item_context.rb', line 293 def string_replace(find, replace) @value = @value.gsub find, replace unless value.nil? self end |
#strip ⇒ Object
Strip whitespace from a string
300 301 302 303 |
# File 'lib/micdrop/item_context.rb', line 300 def strip @value = @value.strip unless value.nil? self end |
#take(name, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.take
61 62 63 |
# File 'lib/micdrop/item_context.rb', line 61 def take(name, put: nil, convert: nil, apply: nil, &block) scope.enter.take(name, put: put, convert: convert, apply: apply, &block) end |
#take_content(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.take_content
11 12 13 |
# File 'lib/micdrop/ext/nokogiri.rb', line 11 def take_content(put: nil, convert: nil, apply: nil, &block) scope.enter.take_content(put: put, convert: convert, apply: apply, &block) end |
#take_dig(*keys, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.take_dig
67 68 69 |
# File 'lib/micdrop/item_context.rb', line 67 def take_dig(*keys, put: nil, convert: nil, apply: nil, &block) scope.enter.take_dig(*keys, put: put, convert: convert, apply: apply, &block) end |
#take_node_name(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.take_node_name
17 18 19 |
# File 'lib/micdrop/ext/nokogiri.rb', line 17 def take_node_name(put: nil, convert: nil, apply: nil, &block) scope.enter.take_node_name(put: put, convert: convert, apply: apply, &block) end |
#try_take(name, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Alias for scope.enter.try_take
73 74 75 |
# File 'lib/micdrop/item_context.rb', line 73 def try_take(name, put: nil, convert: nil, apply: nil, &block) scope.enter.try_take(name, put: put, convert: convert, apply: apply, &block) end |
#unpack(template, offset: 0) ⇒ Object
Unpack binary data (using String.unpack)
323 324 325 326 |
# File 'lib/micdrop/item_context.rb', line 323 def unpack(template, offset: 0) @value = value.unpack template, offset: offset unless @value.nil? self end |
#update(value) ⇒ Object
Directly update the current value
30 31 32 33 |
# File 'lib/micdrop/item_context.rb', line 30 def update(value) @value = value self end |