Class: Micdrop::RecordContext
- Inherits:
-
Object
- Object
- Micdrop::RecordContext
- Defined in:
- lib/micdrop/ext/nokogiri.rb,
lib/micdrop/record_context.rb
Overview
Common functions for all record contexts
Direct Known Subclasses
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #at_css(*args, &block) ⇒ Object
- #at_xpath(*args, &block) ⇒ Object
-
#collect_format_string(template, *items, put: nil, convert: nil, apply: nil, &block) ⇒ Object
collect multiple values into a format string.
-
#collect_kv(hash, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Create a new key/value record which collections multiple
takes into a single hash. -
#collect_list(*items, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Create a new list record which collections multiple
takes into a single list. - #css(*args, &block) ⇒ Object
-
#each_subrecord(flush: false, reset: false, &block) ⇒ Object
alias for take_whole.each_subrecord.
-
#index(put: nil, convert: nil, apply: nil, &block) ⇒ Object
indexis a special form oftakewhich takes the record index rather than an actual value from the record. -
#passthru(*names) ⇒ Object
A combined take/put shorthand, for migrations where many of the column names are the same.
-
#skip ⇒ Object
Skip the current record.
-
#static(value, put: nil, convert: nil, apply: nil, &block) ⇒ Object
staticis a variant oftakethat, instead of actually taking data from the source record, allows you to specify your own value. -
#stop ⇒ Object
Stop processing values from the source.
-
#take(name, put: nil, convert: nil, apply: nil, &block) ⇒ Object
takeextracts a single item from a record (e.g. a column from a row) and allows it to be operated upon. -
#take_content(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take the text content of the XML or HTML node.
-
#take_dig(*keys, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take a value using the :dig method instead of :[].
-
#take_node_name(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take the node name of the XML or HTML node.
-
#take_whole(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take the entire record as a single item.
-
#try_take(name, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take a value if possible, or take nil otherwise.
- #xpath(*args, &block) ⇒ Object
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
10 11 12 |
# File 'lib/micdrop/record_context.rb', line 10 def record @record end |
Instance Method Details
#at_css(*args, &block) ⇒ Object
167 168 169 |
# File 'lib/micdrop/ext/nokogiri.rb', line 167 def at_css(*args, &block) nokogiri_node_subrecord_helper(@record.at_css(*args), block) end |
#at_xpath(*args, &block) ⇒ Object
159 160 161 |
# File 'lib/micdrop/ext/nokogiri.rb', line 159 def at_xpath(*args, &block) nokogiri_node_subrecord_helper(@record.at_xpath(*args), block) end |
#collect_format_string(template, *items, put: nil, convert: nil, apply: nil, &block) ⇒ Object
collect multiple values into a format string
99 100 101 102 |
# File 'lib/micdrop/record_context.rb', line 99 def collect_format_string(template, *items, put: nil, convert: nil, apply: nil, &block) value = format template, *items.map(&:value) process_item_helper(value, put, convert, apply, block) end |
#collect_kv(hash, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Create a new key/value record which collections multiple takes into a single hash.
Accepts all the same arguments as take. Then taken value will be a list of all constituent
taken values. This is often used to join or concatenate items in the source in some way.
92 93 94 95 |
# File 'lib/micdrop/record_context.rb', line 92 def collect_kv(hash, put: nil, convert: nil, apply: nil, &block) value = hash.transform_values(&:value) process_item_helper(value, put, convert, apply, block) end |
#collect_list(*items, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Create a new list record which collections multiple takes into a single list.
Accepts all the same arguments as take. Then taken value will be a list of all constituent
taken values. This is often used to join or concatenate items in the source in some way.
82 83 84 85 |
# File 'lib/micdrop/record_context.rb', line 82 def collect_list(*items, put: nil, convert: nil, apply: nil, &block) value = items.map(&:value) process_item_helper(value, put, convert, apply, block) end |
#css(*args, &block) ⇒ Object
163 164 165 |
# File 'lib/micdrop/ext/nokogiri.rb', line 163 def css(*args, &block) nokogiri_node_subrecord_helper(@record.css(*args), block) end |
#each_subrecord(flush: false, reset: false, &block) ⇒ Object
alias for take_whole.each_subrecord
49 50 51 |
# File 'lib/micdrop/record_context.rb', line 49 def each_subrecord(flush: false, reset: false, &block) take_whole.each_subrecord(flush: flush, reset: reset, &block) end |
#index(put: nil, convert: nil, apply: nil, &block) ⇒ Object
index is a special form of take which takes the record index rather than an actual value
from the record. You can use this as a unique identifier if the source does not have an
explicit identifier.
73 74 75 |
# File 'lib/micdrop/record_context.rb', line 73 def index(put: nil, convert: nil, apply: nil, &block) process_item_helper(loop_index, put, convert, apply, block) end |
#passthru(*names) ⇒ Object
A combined take/put shorthand, for migrations where many of the column names are the same
55 56 57 58 59 |
# File 'lib/micdrop/record_context.rb', line 55 def passthru(*names) names.each do take(name, put: name) end end |
#skip ⇒ Object
Skip the current record. This is similar to a plain-ruby next statement.
106 107 108 |
# File 'lib/micdrop/record_context.rb', line 106 def skip raise Skip end |
#static(value, put: nil, convert: nil, apply: nil, &block) ⇒ Object
static is a variant of take that, instead of actually taking data from the source record,
allows you to specify your own value. This is usually used to supply values which are not
provided in the source, but required in the sink.
65 66 67 |
# File 'lib/micdrop/record_context.rb', line 65 def static(value, put: nil, convert: nil, apply: nil, &block) process_item_helper(value, put, convert, apply, block) end |
#stop ⇒ Object
Stop processing values from the source. This is similar to a plain-ruby break statement.
112 113 114 |
# File 'lib/micdrop/record_context.rb', line 112 def stop raise Stop end |
#take(name, put: nil, convert: nil, apply: nil, &block) ⇒ Object
take extracts a single item from a record (e.g. a column from a row) and allows it to be
operated upon. This is one of the most common operations you will use. It takes the following
additional options:
putspecifies where the taken value will go in the sink, after all transformations are applied.converttakes a proc or function that will be called on the taken value. The new value will be the return value of the function.applytakes a proc or function that will be used as a pipeline to transform the value. SeeItemContextfor details. (Passing a block also has the same effect.)
23 24 25 26 |
# File 'lib/micdrop/record_context.rb', line 23 def take(name, put: nil, convert: nil, apply: nil, &block) value = @record.nil? ? nil : @record[name] process_item_helper(value, put, convert, apply, block) end |
#take_content(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take the text content of the XML or HTML node
143 144 145 146 |
# File 'lib/micdrop/ext/nokogiri.rb', line 143 def take_content(put: nil, convert: nil, apply: nil, &block) value = @record&.content process_item_helper(value, put, convert, apply, block) end |
#take_dig(*keys, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take a value using the :dig method instead of :[]
29 30 31 32 |
# File 'lib/micdrop/record_context.rb', line 29 def take_dig(*keys, put: nil, convert: nil, apply: nil, &block) value = @record&.dig(*keys) process_item_helper(value, put, convert, apply, block) end |
#take_node_name(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take the node name of the XML or HTML node
150 151 152 153 |
# File 'lib/micdrop/ext/nokogiri.rb', line 150 def take_node_name(put: nil, convert: nil, apply: nil, &block) value = @record&.node_name process_item_helper(value, put, convert, apply, block) end |
#take_whole(put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take the entire record as a single item
43 44 45 |
# File 'lib/micdrop/record_context.rb', line 43 def take_whole(put: nil, convert: nil, apply: nil, &block) process_item_helper(record, put, convert, apply, block) end |
#try_take(name, put: nil, convert: nil, apply: nil, &block) ⇒ Object
Take a value if possible, or take nil otherwise
36 37 38 39 |
# File 'lib/micdrop/record_context.rb', line 36 def try_take(name, put: nil, convert: nil, apply: nil, &block) value = @record&.fetch(name, nil) process_item_helper(value, put, convert, apply, block) end |
#xpath(*args, &block) ⇒ Object
155 156 157 |
# File 'lib/micdrop/ext/nokogiri.rb', line 155 def xpath(*args, &block) nokogiri_node_subrecord_helper(@record.xpath(*args), block) end |