diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-04-23 18:01:36 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-04-23 18:01:36 +0000 |
| commit | 8d3673d5df4f09909f092c5cb42ef8870762db1a (patch) | |
| tree | 0451e5bfe2c6bbbbc2b62a4c6a80807ff403f200 | |
| parent | a478ed2b5beadd1124c375ced21a9343dc6c591a (diff) | |
| download | puppet-8d3673d5df4f09909f092c5cb42ef8870762db1a.tar.gz puppet-8d3673d5df4f09909f092c5cb42ef8870762db1a.tar.xz puppet-8d3673d5df4f09909f092c5cb42ef8870762db1a.zip | |
Adding a :block_eval option to FileRecords in FileParsing, so ParsedFile providers can have records just define a bunch of methods at once, rather than using lots of hooks. This is cleaner when custom parse and generate methods are used.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2407 980ebf18-57e1-0310-9a29-db15c13687c0
| -rw-r--r-- | CHANGELOG | 3 | ||||
| -rw-r--r-- | lib/puppet/util/fileparsing.rb | 12 | ||||
| -rwxr-xr-x | test/util/fileparsing.rb | 24 |
3 files changed, 36 insertions, 3 deletions
@@ -1,3 +1,6 @@ + FileParsing classes can now use instance_eval to add + many methods at once to a record type. + Modules no longer return directories in the list of found manifests (#588). diff --git a/lib/puppet/util/fileparsing.rb b/lib/puppet/util/fileparsing.rb index c11645bfb..169320ae5 100644 --- a/lib/puppet/util/fileparsing.rb +++ b/lib/puppet/util/fileparsing.rb @@ -33,8 +33,7 @@ module Puppet::Util::FileParsing class FileRecord include Puppet::Util include Puppet::Util::MethodHelper - attr_accessor :absent, :joiner, :rts, - :separator, :rollup, :name, :match + attr_accessor :absent, :joiner, :rts, :separator, :rollup, :name, :match, :block_eval attr_reader :fields, :optional, :type @@ -71,7 +70,14 @@ module Puppet::Util::FileParsing end if block_given? - meta_def(:process, &block) + @block_eval ||= :process + + # Allow the developer to specify that a block should be instance-eval'ed. + if @block_eval == :instance + instance_eval(&block) + else + meta_def(@block_eval, &block) + end end end diff --git a/test/util/fileparsing.rb b/test/util/fileparsing.rb index c81c9e006..9a892c383 100755 --- a/test/util/fileparsing.rb +++ b/test/util/fileparsing.rb @@ -647,6 +647,30 @@ class TestUtilFileRecord < Test::Unit::TestCase assert_equal(line.upcase, record.process(line), "Record did not process line correctly") end + + # Make sure we can declare that we want the block to be instance-eval'ed instead of + # defining the 'process' method. + def test_instance_block + record = nil + assert_nothing_raised("Could not pass a block when creating record") do + record = Record.new(:record, :block_eval => :instance, :fields => %w{one}) do + def process(line) + line.upcase + end + + def to_line(details) + details.upcase + end + end + end + + assert(record.respond_to?(:process), "Block was not instance-eval'ed and process was not defined") + assert(record.respond_to?(:to_line), "Block was not instance-eval'ed and to_line was not defined") + + line = "some text" + assert_equal(line.upcase, record.process(line), "Instance-eval'ed record did not call :process correctly") + assert_equal(line.upcase, record.to_line(line), "Instance-eval'ed record did not call :to_line correctly") + end end # $Id$ |
