# File lib/puppet/util/fileparsing.rb, line 51 def initialize(type, options = {}, &block) @type = type.intern raise ArgumentError, "Invalid record type #{@type}" unless [:record, :text].include?(@type) set_options(options) if self.type == :record # Now set defaults. self.absent ||= "" self.separator ||= /\s+/ self.joiner ||= " " self.optional ||= [] @rollup = true unless defined?(@rollup) end if block_given? @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
Customize this so we can do a bit of validation.
# File lib/puppet/util/fileparsing.rb, line 43 def fields=(fields) @fields = fields.collect do |field| r = field.intern raise ArgumentError.new("Cannot have fields named #{r}") if INVALID_FIELDS.include?(r) r end end
Convert a record into a line by joining the fields together appropriately. This is pulled into a separate method so it can be called by the hooks.
# File lib/puppet/util/fileparsing.rb, line 80 def join(details) joinchar = self.joiner fields.collect { |field| # If the field is marked absent, use the appropriate replacement if details[field] == :absent or details[field] == [:absent] or details[field].nil? if self.optional.include?(field) self.absent else raise ArgumentError, "Field '#{field}' is required" end else details[field].to_s end }.reject { |c| c.nil?}.join(joinchar) end
Customize this so we can do a bit of validation.
# File lib/puppet/util/fileparsing.rb, line 98 def optional=(optional) @optional = optional.collect do |field| field.intern end end
Create a hook that modifies the hash resulting from parsing.
# File lib/puppet/util/fileparsing.rb, line 105 def post_parse=(block) meta_def(:post_parse, &block) end
Create a hook that modifies the hash just prior to generation.
# File lib/puppet/util/fileparsing.rb, line 110 def pre_gen=(block) meta_def(:pre_gen, &block) end
Are we a text type?
# File lib/puppet/util/fileparsing.rb, line 115 def text? type == :text end
# File lib/puppet/util/fileparsing.rb, line 119 def to_line=(block) meta_def(:to_line, &block) end