Class: Puppet::Util::FileParsing::FileRecord

Inherits:
Object
  • Object
show all
Includes:
Puppet::Util, MethodHelper
Defined in:
lib/vendor/puppet/util/fileparsing.rb

Constant Summary collapse

INVALID_FIELDS =
[:record_type, :target, :on_disk]

Constants included from Puppet::Util

AbsolutePathPosix, AbsolutePathWindows

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Puppet::Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Constructor Details

#initialize(type, options = {}, &block) ⇒ FileRecord

Returns a new instance of FileRecord.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vendor/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

Instance Attribute Details

#absentObject

Returns the value of attribute absent.



36
37
38
# File 'lib/vendor/puppet/util/fileparsing.rb', line 36

def absent
  @absent
end

#block_evalObject

Returns the value of attribute block_eval.



36
37
38
# File 'lib/vendor/puppet/util/fileparsing.rb', line 36

def block_eval
  @block_eval
end

#fieldsObject

Returns the value of attribute fields.



38
39
40
# File 'lib/vendor/puppet/util/fileparsing.rb', line 38

def fields
  @fields
end

#joinerObject

Returns the value of attribute joiner.



36
37
38
# File 'lib/vendor/puppet/util/fileparsing.rb', line 36

def joiner
  @joiner
end

#matchObject

Returns the value of attribute match.



36
37
38
# File 'lib/vendor/puppet/util/fileparsing.rb', line 36

def match
  @match
end

#nameObject

Returns the value of attribute name.



36
37
38
# File 'lib/vendor/puppet/util/fileparsing.rb', line 36

def name
  @name
end

#optionalObject

Returns the value of attribute optional.



38
39
40
# File 'lib/vendor/puppet/util/fileparsing.rb', line 38

def optional
  @optional
end

#rollupObject

Returns the value of attribute rollup.



36
37
38
# File 'lib/vendor/puppet/util/fileparsing.rb', line 36

def rollup
  @rollup
end

#rtsObject

Returns the value of attribute rts.



36
37
38
# File 'lib/vendor/puppet/util/fileparsing.rb', line 36

def rts
  @rts
end

#separatorObject

Returns the value of attribute separator.



36
37
38
# File 'lib/vendor/puppet/util/fileparsing.rb', line 36

def separator
  @separator
end

#typeObject (readonly)

Returns the value of attribute type.



38
39
40
# File 'lib/vendor/puppet/util/fileparsing.rb', line 38

def type
  @type
end

Instance Method Details

#join(details) ⇒ Object

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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vendor/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

#post_parse=(block) ⇒ Object

Create a hook that modifies the hash resulting from parsing.



105
106
107
# File 'lib/vendor/puppet/util/fileparsing.rb', line 105

def post_parse=(block)
  meta_def(:post_parse, &block)
end

#pre_gen=(block) ⇒ Object

Create a hook that modifies the hash just prior to generation.



110
111
112
# File 'lib/vendor/puppet/util/fileparsing.rb', line 110

def pre_gen=(block)
  meta_def(:pre_gen, &block)
end

#text?Boolean

Are we a text type?

Returns:

  • (Boolean)


115
116
117
# File 'lib/vendor/puppet/util/fileparsing.rb', line 115

def text?
  type == :text
end

#to_line=(block) ⇒ Object



119
120
121
# File 'lib/vendor/puppet/util/fileparsing.rb', line 119

def to_line=(block)
  meta_def(:to_line, &block)
end