Class: Tap::Tasks::Load
Overview
:startdoc::manifest the default load task
Load YAML-formatted data, as may be produced using Tap::Dump, and makes this data available for other tasks. Load is often used as a gateway task to other tasks.
% tap run -- load FILEPATH --: [task]
Load can select items from Hash or Array objects using one or more keys when only a subset is desired. By default items are selected using []. For more flexible selection, use match.
Match converts each of the keys to a Regexp. For hashes, all values with a matching key will be selected. For arrays, any item matching a regexp will be selected.
Use the flags to flatten, compact, sort (etc) results before passing them on to the next task.
Constant Summary
Constants inherited from Tap::Task
Tap::Task::DEFAULT_HELP_TEMPLATE
Instance Attribute Summary
Attributes inherited from Tap::Task
Attributes included from Support::Executable
#_method_name, #app, #batch, #dependencies, #on_complete_block
Attributes included from Support::Configurable
Instance Method Summary collapse
-
#process(input, *keys) ⇒ Object
Loads the input as YAML and selects objects using keys.
Methods inherited from Tap::Task
execute, help, inherited, #initialize, #initialize_batch_obj, #inspect, instance, intern, lazydoc, #log, parse, parse!, #to_s
Methods included from Support::Executable
#_execute, #batch_index, #batch_with, #batched?, #check_terminate, #depends_on, #enq, #execute, #fork, initialize, #initialize_batch_obj, #inspect, #merge, #on_complete, #reset_dependencies, #resolve_dependencies, #sequence, #switch, #sync_merge, #unbatched_depends_on, #unbatched_enq, #unbatched_on_complete
Methods included from Support::Configurable
included, #initialize_copy, #reconfigure
Constructor Details
This class inherits a constructor from Tap::Task
Instance Method Details
#process(input, *keys) ⇒ Object
Loads the input as YAML and selects objects using keys. Input may be an IO, StringIO, or a filepath. If keys are empty, the loaded object is returned directly.
Key Selection
Keys select items from the loaded object using [] (ie obj). If match==true, the behavior is different; each string key is converted into a Regexp and then arrays select items that match key:
results = []
array.each {|i| results << i if i =~ key}
While hashes select values where the key matches key:
results = []
hash.each_pair {|k,v| results << v if k =~ key}
Other objects raise an error when match is true.
Post Processing
After loading/key selection, the results may be processed (in this order) using flatten, compact, unique, and sort, each performing as you would expect.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/tap/tasks/load.rb', line 55 def process(input, *keys) # load the input obj = case input when StringIO, IO YAML.load(input.read) else log :load, input YAML.load_file(input) end # select results by key results = case when keys.empty? then obj when match regexps = keys.collect {|key| Regexp.new(key.to_s) } select_matching(obj, regexps) else keys.collect do |key| index = key.kind_of?(String) ? YAML.load(key) : key obj[index] end end # post-process results results = results.flatten if flatten results = results.compact if compact results = results.uniq if unique results = results.sort if sort #if preview # should be a log or something #puts results.inspect #end results end |