class Puppet::Util::FileParsing::FileRecord
Constants
- INVALID_FIELDS
Attributes
absent[RW]
block_eval[RW]
fields[R]
joiner[RW]
match[RW]
name[RW]
optional[R]
rollup[RW]
rts[RW]
separator[RW]
type[R]
Public Class Methods
new(type, absent: nil, block_eval: nil, fields: nil, joiner: nil, match: nil, optional: nil, post_parse: nil, pre_gen: nil, rollup: nil, rts: nil, separator: nil, to_line: nil, &block)
click to toggle source
# File lib/puppet/util/fileparsing.rb 48 def initialize(type, 49 absent: nil, 50 block_eval: nil, 51 fields: nil, 52 joiner: nil, 53 match: nil, 54 optional: nil, 55 post_parse: nil, 56 pre_gen: nil, 57 rollup: nil, 58 rts: nil, 59 separator: nil, 60 to_line: nil, 61 &block) 62 @type = type.intern 63 raise ArgumentError, _("Invalid record type %{record_type}") % { record_type: @type } unless [:record, :text].include?(@type) 64 65 @absent = absent 66 @block_eval = block_eval 67 @joiner = joiner 68 @match = match 69 @rollup = rollup if rollup 70 @rts = rts 71 @separator = separator 72 73 self.fields = fields if fields 74 self.optional = optional if optional 75 self.post_parse = post_parse if post_parse 76 self.pre_gen = pre_gen if pre_gen 77 self.to_line = to_line if to_line 78 79 if self.type == :record 80 # Now set defaults. 81 self.absent ||= "" 82 self.separator ||= /\s+/ 83 self.joiner ||= " " 84 self.optional ||= [] 85 @rollup = true unless defined?(@rollup) 86 end 87 88 if block_given? 89 @block_eval ||= :process 90 91 # Allow the developer to specify that a block should be instance-eval'ed. 92 if @block_eval == :instance 93 instance_eval(&block) 94 else 95 meta_def(@block_eval, &block) 96 end 97 end 98 end
Public Instance Methods
fields=(fields)
click to toggle source
Customize this so we can do a bit of validation.
# File lib/puppet/util/fileparsing.rb 40 def fields=(fields) 41 @fields = fields.collect do |field| 42 r = field.intern 43 raise ArgumentError.new(_("Cannot have fields named %{name}") % { name: r }) if INVALID_FIELDS.include?(r) 44 r 45 end 46 end
join(details)
click to toggle source
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 102 def join(details) 103 joinchar = self.joiner 104 105 fields.collect { |field| 106 # If the field is marked absent, use the appropriate replacement 107 if details[field] == :absent or details[field] == [:absent] or details[field].nil? 108 if self.optional.include?(field) 109 self.absent 110 else 111 raise ArgumentError, _("Field '%{field}' is required") % { field: field } 112 end 113 else 114 details[field].to_s 115 end 116 }.reject { |c| c.nil?}.join(joinchar) 117 end
optional=(optional)
click to toggle source
Customize this so we can do a bit of validation.
# File lib/puppet/util/fileparsing.rb 120 def optional=(optional) 121 @optional = optional.collect do |field| 122 field.intern 123 end 124 end
post_parse=(block)
click to toggle source
Create a hook that modifies the hash resulting from parsing.
# File lib/puppet/util/fileparsing.rb 127 def post_parse=(block) 128 meta_def(:post_parse, &block) 129 end
pre_gen=(block)
click to toggle source
Create a hook that modifies the hash just prior to generation.
# File lib/puppet/util/fileparsing.rb 132 def pre_gen=(block) 133 meta_def(:pre_gen, &block) 134 end
text?()
click to toggle source
Are we a text type?
# File lib/puppet/util/fileparsing.rb 137 def text? 138 type == :text 139 end
to_line=(block)
click to toggle source
# File lib/puppet/util/fileparsing.rb 141 def to_line=(block) 142 meta_def(:to_line, &block) 143 end