Class: CheapFile

Inherits:
Object
  • Object
show all
Defined in:
lib/cheap_file.rb

Direct Known Subclasses

CheapBigFile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir, xlat_ext, seed, xlat_lambda) ⇒ CheapFile

Returns a new instance of CheapFile.



27
28
29
30
31
32
# File 'lib/cheap_file.rb', line 27

def initialize(base_dir, xlat_ext, seed, xlat_lambda)
  @base_dir = base_dir
  @xlat_ext = xlat_ext
  @seed = seed
  @xlat = xlat_lambda
end

Class Method Details

.file?(afn) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/cheap_file.rb', line 3

def self.file?(afn)
  File.file? afn
end

.from_file(afn) ⇒ Object



7
8
9
10
11
# File 'lib/cheap_file.rb', line 7

def self.from_file(afn)
  f = File.new afn, 'rb'
  s = f.read
  s.force_encoding 'ASCII-8BIT'
end

.is_do_afn_new_afn(base_dir, fn, xlat_ext) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/cheap_file.rb', line 19

def self.is_do_afn_new_afn(base_dir, fn, xlat_ext)
  afn = "#{base_dir}/#{fn}"
  xlat_match = afn =~ Regexp.new("\\#{xlat_ext}$")
  new_afn = afn[0, xlat_match] if xlat_match
  new_afn = afn + xlat_ext unless xlat_match
  [!xlat_match, afn, new_afn]
end

.to_file(afn, s) ⇒ Object



13
14
15
16
17
# File 'lib/cheap_file.rb', line 13

def self.to_file(afn, s)
  f = File.new afn, "wb"
  f.write s
  f.close
end

Instance Method Details

#xlat_small(is_do, s) ⇒ Object



34
35
36
# File 'lib/cheap_file.rb', line 34

def xlat_small(is_do, s)
  @xlat.call is_do, @seed, s
end

#xlat_small_file(fn, should_write = true) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/cheap_file.rb', line 38

def xlat_small_file(fn, should_write = true)
  is_do, afn, new_afn = self.class.is_do_afn_new_afn @base_dir, fn, @xlat_ext
  s = self.class.from_file afn
  perm = xlat_small is_do, s
  self.class.to_file new_afn, s if should_write
  perm
end