Module: Completable

Includes:
Logging
Defined in:
lib/completable.rb

Overview

fields are searched and replaced by values.

Defined Under Namespace

Classes: Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#init_logger, #log_level=, #log_target=

Methods included from File_Checking

file_check, #file_check, last_mime_type, magic_check, mime_check

Instance Attribute Details

#backupObject (readonly)

Returns the value of attribute backup.



31
32
33
# File 'lib/completable.rb', line 31

def backup
  @backup
end

#summaryObject (readonly)

Returns the value of attribute summary.



31
32
33
# File 'lib/completable.rb', line 31

def summary
  @summary
end

Instance Method Details

#complete(subst_def, &b) ⇒ Object

The main function of this module. Expects as parameters a Hash of field/substituion- pairs and an optional block. If a block is given, the original file will be processed and a backup is created.



39
40
41
42
43
44
45
46
47
48
49
50
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
77
# File 'lib/completable.rb', line 39

def complete(subst_def, &b) 

  init_logger()
  @summary = Summary.new(subst_def)
  @field_count = 0
  if(self.respond_to?(:gets) )         
    if(self.respond_to?(:closed?) && !self.closed?)
      self.rewind
      if(subst_def && subst_def.respond_to?(:has_key?))
        cpos = 0
        if(!b)
          @backup="#{self.path}_backup"
          outfile = File.open(@backup, 'w'){|bf| bf.write(self.readlines() )}
          self.rewind
        end
        while(chunk = self.gets())
          chunk, msg = process(chunk, subst_def)
          if(!chunk && msg)
            return nil, msg
          end
          if(b)
            b.call(chunk)
          else
            self.seek(cpos, IO::SEEK_SET)
            self.write(chunk)
          end
          cpos = self.pos
        end
      else
        return nil, 'first parameter must be a Hash'
      end
    else
      return nil, 'completable object must at least be open for reading'
    end
  else
    return nil, 'completable object must also respond to :gets'
  end
  @field_count
end

#field_delimiterObject

returns the character sequence, which marks substitutable text



87
88
89
90
91
92
# File 'lib/completable.rb', line 87

def field_delimiter()
  if(!@field_delimiter)
    field_delimiter=(' ')
  end
  @field_delimiter
end

#field_delimiter=(d) ⇒ Object

sets the character sequence, which marks substitutable text



81
82
83
84
# File 'lib/completable.rb', line 81

def field_delimiter=(d)
  @field_delimiter = d
  @field_regex = Regexp.new("#{d}[^#{d}]*#{d.reverse}")
end