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
49 def initialize(type,
50                absent: nil,
51                block_eval: nil,
52                fields: nil,
53                joiner: nil,
54                match: nil,
55                optional: nil,
56                post_parse: nil,
57                pre_gen: nil,
58                rollup: nil,
59                rts: nil,
60                separator: nil,
61                to_line: nil,
62                &block)
63   @type = type.intern
64   raise ArgumentError, _("Invalid record type %{record_type}") % { record_type: @type } unless [:record, :text].include?(@type)
65 
66   @absent = absent
67   @block_eval = block_eval
68   @joiner = joiner
69   @match = match
70   @rollup = rollup if rollup
71   @rts = rts
72   @separator = separator
73 
74   self.fields = fields if fields
75   self.optional = optional if optional
76   self.post_parse = post_parse if post_parse
77   self.pre_gen = pre_gen if pre_gen
78   self.to_line = to_line if to_line
79 
80   if self.type == :record
81     # Now set defaults.
82     self.absent ||= ""
83     self.separator ||= /\s+/
84     self.joiner ||= " "
85     self.optional ||= []
86     @rollup = true unless defined?(@rollup)
87   end
88 
89   if block_given?
90     @block_eval ||= :process
91 
92     # Allow the developer to specify that a block should be instance-eval'ed.
93     if @block_eval == :instance
94       instance_eval(&block)
95     else
96       meta_def(@block_eval, &block)
97     end
98   end
99 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
41 def fields=(fields)
42   @fields = fields.collect do |field|
43     r = field.intern
44     raise ArgumentError.new(_("Cannot have fields named %{name}") % { name: r }) if INVALID_FIELDS.include?(r)
45     r
46   end
47 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
103 def join(details)
104   joinchar = self.joiner
105 
106   fields.collect { |field|
107     # If the field is marked absent, use the appropriate replacement
108     if details[field] == :absent or details[field] == [:absent] or details[field].nil?
109       if self.optional.include?(field)
110         self.absent
111       else
112         raise ArgumentError, _("Field '%{field}' is required") % { field: field }
113       end
114     else
115       details[field].to_s
116     end
117   }.reject { |c| c.nil?}.join(joinchar)
118 end
optional=(optional) click to toggle source

Customize this so we can do a bit of validation.

    # File lib/puppet/util/fileparsing.rb
121 def optional=(optional)
122   @optional = optional.collect do |field|
123     field.intern
124   end
125 end
post_parse=(block) click to toggle source

Create a hook that modifies the hash resulting from parsing.

    # File lib/puppet/util/fileparsing.rb
128 def post_parse=(block)
129   meta_def(:post_parse, &block)
130 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
133 def pre_gen=(block)
134   meta_def(:pre_gen, &block)
135 end
text?() click to toggle source

Are we a text type?

    # File lib/puppet/util/fileparsing.rb
138 def text?
139   type == :text
140 end
to_line=(block) click to toggle source
    # File lib/puppet/util/fileparsing.rb
142 def to_line=(block)
143   meta_def(:to_line, &block)
144 end