Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/puppet/parser/functions/filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'puppet/parser/ast/lambda'

Puppet::Parser::Functions::newfunction(
:filter,
:type => :rvalue,
:arity => 2,
:doc => <<-'ENDHEREDOC') do |args|
Applies a parameterized block to each element in a sequence of entries from the first
argument and returns an array or hash (same type as left operand)
with the entries for which the block evaluates to true.

This function takes two mandatory arguments: the first should be an Array or a Hash, and the second
a parameterized block as produced by the puppet syntax:

$a.filter |$x| { ... }

When the first argument is an Array, the block is called with each entry in turn. When the first argument
is a Hash the entry is an array with `[key, value]`.

The returned filtered object is of the same type as the receiver.

*Examples*

# selects all that end with berry
$a = ["raspberry", "blueberry", "orange"]
$a.filter |$x| { $x =~ /berry$/ }

- Since 3.4
- requires `parser = future`.
ENDHEREDOC

receiver = args[0]
pblock = args[1]

raise ArgumentError, ("filter(): wrong argument type (#{pblock.class}; must be a parameterized block.") unless pblock.is_a? Puppet::Parser::AST::Lambda

case receiver
when Array
receiver.select {|x| pblock.call(self, x) }
when Hash
result = receiver.select {|x, y| pblock.call(self, [x, y]) }
# Ruby 1.8.7 returns Array
result = Hash[result] unless result.is_a? Hash
result
else
raise ArgumentError, ("filter(): wrong argument type (#{receiver.class}; must be an Array or a Hash.")
end
end
44 changes: 6 additions & 38 deletions lib/puppet/parser/functions/select.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
require 'puppet/parser/ast/lambda'

Puppet::Parser::Functions::newfunction(
:select,
:type => :rvalue,
:arity => 2,
:doc => <<-'ENDHEREDOC') do |args|
Applies a parameterized block to each element in a sequence of entries from the first
argument and returns an array with the entires for which the block evaluates to true.

This function takes two mandatory arguments: the first should be an Array or a Hash, and the second
a parameterized block as produced by the puppet syntax:

$a.select |$x| { ... }

When the first argument is an Array, the block is called with each entry in turn. When the first argument
is a hash the entry is an array with `[key, value]`.

The returned filtered object is of the same type as the receiver.
The 'select' function has been renamed to 'filter'. Please update your manifests.

*Examples*

# selects all that end with berry
$a = ["raspberry", "blueberry", "orange"]
$a.select |$x| { $x =~ /berry$/ }

- Since 3.2
The select function is reserved for future use.
- Removed as of 3.4
- requires `parser = future`.
ENDHEREDOC

receiver = args[0]
pblock = args[1]

raise ArgumentError, ("select(): wrong argument type (#{pblock.class}; must be a parameterized block.") unless pblock.is_a? Puppet::Parser::AST::Lambda

case receiver
when Array
receiver.select {|x| pblock.call(self, x) }
when Hash
result = receiver.select {|x, y| pblock.call(self, [x, y]) }
# Ruby 1.8.7 returns Array
result = Hash[result] unless result.is_a? Hash
result
else
raise ArgumentError, ("select(): wrong argument type (#{receiver.class}; must be an Array or a Hash.")
end
end
raise NotImplementedError,
"The 'select' function has been renamed to 'filter'. Please update your manifests."
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

require 'unit/parser/methods/shared'

describe 'the select method' do
describe 'the filter method' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we've renamed select.rb -> filter.rb, how about:

git mv spec/unit/parser/methods/select_spec.rb spec/unit/parser/methods/filter_spec.rb

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed that - fixing

include PuppetSpec::Compiler

before :each do
Puppet[:parser] = 'future'
end

it 'should select on an array (all berries)' do
it 'should filter on an array (all berries)' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = ['strawberry','blueberry','orange']
$a.select {|$x| $x =~ /berry$/}.each {|$v|
$a.filter {|$x| $x =~ /berry$/}.each {|$v|
file { "/file_$v": ensure => present }
}
MANIFEST
Expand All @@ -26,7 +26,7 @@
it 'should produce an array when acting on an array' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = ['strawberry','blueberry','orange']
$b = $a.select {|$x| $x =~ /berry$/}
$b = $a.filter {|$x| $x =~ /berry$/}
file { "/file_${b[0]}": ensure => present }
file { "/file_${b[1]}": ensure => present }
MANIFEST
Expand All @@ -35,10 +35,10 @@
catalog.resource(:file, "/file_blueberry")['ensure'].should == 'present'
end

it 'selects on a hash (all berries) by key' do
it 'filters on a hash (all berries) by key' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = {'strawberry'=>'red','blueberry'=>'blue','orange'=>'orange'}
$a.select {|$x| $x[0] =~ /berry$/}.each {|$v|
$a.filter {|$x| $x[0] =~ /berry$/}.each {|$v|
file { "/file_${v[0]}": ensure => present }
}
MANIFEST
Expand All @@ -50,7 +50,7 @@
it 'should produce a hash when acting on a hash' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = {'strawberry'=>'red','blueberry'=>'blue','orange'=>'orange'}
$b = $a.select {|$x| $x[0] =~ /berry$/}
$b = $a.filter {|$x| $x[0] =~ /berry$/}
file { "/file_${b['strawberry']}": ensure => present }
file { "/file_${b['blueberry']}": ensure => present }
file { "/file_${b['orange']}": ensure => present }
Expand All @@ -62,10 +62,10 @@
catalog.resource(:file, "/file_")['ensure'].should == 'present'
end

it 'selects on a hash (all berries) by value' do
it 'filters on a hash (all berries) by value' do
catalog = compile_to_catalog(<<-MANIFEST)
$a = {'strawb'=>'red berry','blueb'=>'blue berry','orange'=>'orange fruit'}
$a.select {|$x| $x[1] =~ /berry$/}.each {|$v|
$a.filter {|$x| $x[1] =~ /berry$/}.each {|$v|
file { "/file_${v[0]}": ensure => present }
}
MANIFEST
Expand All @@ -74,6 +74,6 @@
catalog.resource(:file, "/file_blueb")['ensure'].should == 'present'
end

it_should_behave_like 'all iterative functions argument checks', 'select'
it_should_behave_like 'all iterative functions hash handling', 'select'
it_should_behave_like 'all iterative functions argument checks', 'filter'
it_should_behave_like 'all iterative functions hash handling', 'filter'
end