Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Hashr | ||
module Delegate | ||
module Conditional | ||
def method_missing(name, *args, &block) | ||
if (args.any? || block) && @data.respond_to?(name) | ||
@data.send(name, *args, &block) | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Hashr | ||
module Delegation | ||
module Hash | ||
METHODS = [ | ||
:all?, | ||
:any?, | ||
:clear, | ||
:delete, | ||
:delete_if, | ||
:detect, | ||
:drop, | ||
:drop_while, | ||
:each, | ||
:empty?, | ||
:fetch, | ||
:find, | ||
:flat_map, | ||
:grep, | ||
:group_by, | ||
:hash, | ||
:inject, | ||
:invert, | ||
:is_a?, | ||
:keep_if, | ||
:key, | ||
:key?, | ||
:keys, | ||
:length, | ||
:map, | ||
:merge, | ||
:nil?, | ||
:none?, | ||
:one?, | ||
:reduce, | ||
:reject, | ||
:select, | ||
:size, | ||
:value?, | ||
:values, | ||
:values_at | ||
] | ||
|
||
METHODS.each do |name| | ||
define_method(name) { |*args, &block| @data.send(name, *args, &block } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
describe Hashr::Delegate::Conditional do | ||
let(:klass) { Class.new(Hashr) { include Hashr::Delegate::Conditional } } | ||
|
||
it 'delegates key?' do | ||
hashr = klass.new(foo: 'foo') | ||
expect(hashr.key?(:foo)).to eq(true) | ||
end | ||
|
||
it 'delegates select' do | ||
hashr = klass.new(foo: 'foo', bar: 'bar') | ||
expect(hashr.select { |key, value| key == :bar }.to_h).to eq(bar: 'bar') | ||
end | ||
|
||
it 'delegates delete' do | ||
hashr = klass.new(foo: 'foo', bar: 'bar') | ||
hashr.delete(:foo) | ||
expect(hashr.to_h).to eq(bar: 'bar') | ||
end | ||
end |