Skip to content

Commit

Permalink
add pluck command to dorian
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianmariecom committed Sep 10, 2024
1 parent 8867967 commit 276c9a8
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/dorian/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ def run
arguments.delete("merge")
@command = :merge
command_merge
when :pluck
arguments.delete("pluck")
@command = :pluck
command_pluck
else
arguments.delete("read")
@command = :read
Expand Down Expand Up @@ -421,6 +425,10 @@ def command_merge
outputs(map(everything) { |thing| lines(reads(thing)) }.inject(&:+))
end

def command_pluck
outputs(map(everything) { |thing| pluck(lines(reads(thing))) }.inject(&:+))
end

def command_tally
each(everything) do |input|
outputs(
Expand Down Expand Up @@ -1058,6 +1066,28 @@ def match_filetypes?(path)
/\A#!.*ruby\z/.match?(first_line)
end

def pluck(object)
map(wrap(object).from_deep_struct) do |element|
results = arguments.map do |argument|
if element.is_a?(Array) && argument.to_i.to_s == argument
element[argument.to_i]
elsif element.is_a?(Hash) && element.keys.include?(argument)
{ argument => element[argument] }
else
evaluates(ruby: argument, it: element.to_deep_struct).returned
end
end

if results.all? { |result| result.is_a?(Hash) }
results.inject(&:merge).to_deep_struct
else
results.map do |result|
result.is_a?(Hash) ? result.values.first : result
end.to_deep_struct
end
end
end

def evaluates(
ruby: @ruby,
it: nil,
Expand Down

0 comments on commit 276c9a8

Please sign in to comment.