Skip to content

Commit

Permalink
Add grep/grep_v to instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Jan 27, 2024
1 parent 6c02314 commit 4b7489b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 70 deletions.
22 changes: 16 additions & 6 deletions mrblib/rf/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def hash?
_.instance_of?(Hash)
end

def array?
_.instance_of?(Array)
end

def puts(*)
$output.write(generate_line_prefix)
$output.puts(*)
Expand All @@ -62,6 +66,18 @@ def generate_line_prefix
end
end

%i[dig].each do |sym|
define_method(sym) do |*args|
_.__send__(sym, *args) if hash?
end
end

%i[grep grep_v].each do |sym|
define_method(sym) do |*args, &block|
_.__send__(sym, *args) if array?
end
end

def match(condition)
regexp = if condition.is_a?(Regexp)
condition
Expand All @@ -83,12 +99,6 @@ def match?(condition)
end
alias m? match?

%i[dig].each do |sym|
define_method(sym) do |*args|
_.__send__(sym, *args) if hash?
end
end

def respond_to_missing?(sym, *)
# check for _0, _1, _2, _3, ...
sym.to_s =~ /\A_(0|[1-9]\d*)\z/ || super
Expand Down
82 changes: 18 additions & 64 deletions spec/container/instance_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@
it_behaves_like 'a successful exec'
end

where(:method, :expect_output) do
'grep' | %w[bar].join("\n")
'grep_v' | %w[foo baz].join("\n")
end

with_them do
let(:input) do
<<~INPUT
foo
bar
baz
INPUT
end
let(:args) { %(-s '#{method}(/bar/)') }

it_behaves_like 'a successful exec'
end

%w[match m].each do |method|
describe "Container##{method}" do
where do
Expand Down Expand Up @@ -214,67 +232,3 @@
end
end
end

# describe 'Method' do
#
#
# describe '#sub' do
# let(:input) { 'foofoo' }
# let(:output) do
# <<~OUTPUT
# barfoo
# foofoo
# OUTPUT
# end
#
# before { run_rf(%q('puts sub(/foo/, "bar"); _'), input) }
#
# it { expect(last_command_started).to be_successfully_executed }
# it { expect(last_command_started).to have_output output_string_eq output }
# end
#
# describe '#sub!' do
# let(:input) { 'foofoo' }
# let(:output) do
# <<~OUTPUT
# barfoo
# barfoo
# OUTPUT
# end
#
# before { run_rf(%q('puts sub!(/foo/, "bar"); _'), input) }
#
# it { expect(last_command_started).to be_successfully_executed }
# it { expect(last_command_started).to have_output output_string_eq output }
# end
#
# describe '#tr' do
# let(:input) { 'foo' }
# let(:output) do
# <<~OUTPUT
# FOO
# foo
# OUTPUT
# end
#
# before { run_rf(%q('puts tr("a-z", "A-Z"); _'), input) }
#
# it { expect(last_command_started).to be_successfully_executed }
# it { expect(last_command_started).to have_output output_string_eq output }
# end
#
# describe '#tr!' do
# let(:input) { 'foo' }
# let(:output) do
# <<~OUTPUT
# FOO
# FOO
# OUTPUT
# end
#
# before { run_rf(%q('puts tr!("a-z", "A-Z"); _'), input) }
#
# it { expect(last_command_started).to be_successfully_executed }
# it { expect(last_command_started).to have_output output_string_eq output }
# end
# end

0 comments on commit 4b7489b

Please sign in to comment.