Class: Lbt::PCS

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

Overview

When you create an instance of the PCS class, it keeps the structure of Part-Chapter-Section files. For example,

+-part1-+-chap1-+-sec1.tex
|       |       +-sec2.tex`
|       +-chap2-+-sec1.tex`
|       |       +-sec2.tex`
+-part2-+-chap1-+-sec1.tex
        |       +-sec2.tex`

If the files are as above, the PCS instance keeps the six filenames whch are sorted alphabetically.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir = ".") ⇒ PCS

Create a new PCS instance. You can give a top directory as an argument.



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lbt/utils.rb', line 44

def initialize dir="."
  @dir = dir.dup
  @dir.freeze
  @files = Dir.glob("part*/chap*/sec*", base: @dir).select{|f| f.match?(/^part\d+(\.\d+)?\/chap\d+(\.\d+)?\/sec\d+(\.\d+)?\.\w+$/)}
  unless @files.empty?
    @type = :PCS
    @files.sort! do |a, b|
      m = /^part(\d+(\.\d+)?)\/chap(\d+(\.\d+)?)\/sec(\d+(\.\d+)?)\.\w*$/.match(a)
      ap = m[1].to_f; ac= m[3].to_f; as = m[5].to_f
      m = /^part(\d+(\.\d+)?)\/chap(\d+(\.\d+)?)\/sec(\d+(\.\d+)?)\.\w*$/.match(b)
      bp = m[1].to_f; bc= m[3].to_f; bs = m[5].to_f
      if ap != bp
        ap <=> bp
      elsif ac != bc
        ac <=> bc
      else
        as <=> bs
      end
    end
    return
  end
  @files = Dir.glob("chap*/sec*", base: @dir).select{|f| f.match?(/^chap\d+(\.\d+)?\/sec\d+(\.\d+)?\.\w+$/)}
  unless @files.empty?
    @type = :CS
    @files.sort! do |a, b|
      m = /^chap(\d+(\.\d+)?)\/sec(\d+(\.\d+)?)\.\w*$/.match(a)
      ac= m[1].to_f; as = m[3].to_f
      m = /^chap(\d+(\.\d+)?)\/sec(\d+(\.\d+)?)\.\w*$/.match(b)
      bc= m[1].to_f; bs = m[3].to_f
      if ac != bc
        ac <=> bc
      else
        as <=> bs
      end
    end
    return
  end
  @files = Dir.glob("sec*", base: @dir).select{|f| f.match?(/^sec\d+(\.\d+)?\.\w+$/)}
  unless @files.empty?
    @type = :S
    @files.sort! do |a, b|
      m = /^sec(\d+(\.\d+)?)\.\w*$/.match(a)
      as = m[1].to_f
      m = /^sec(\d+(\.\d+)?)\.\w*$/.match(b)
      bs = m[1].to_f
      as <=> bs
    end
    return
  end
  raise "No [[part/]chap/]sec files exist.\n" if @files.empty?
end

Instance Attribute Details

#dirObject (readonly)

The name of the top direcotory



41
42
43
# File 'lib/lbt/utils.rb', line 41

def dir
  @dir
end

#typeObject (readonly)

Type is one of :PCS, :CS and :S

  • :PCS => The file structure has part, chap directories.

  • :CS => The file structure has chap directories.

  • :S => The file structure doesn’t have any directories.



39
40
41
# File 'lib/lbt/utils.rb', line 39

def type
  @type
end

Instance Method Details

#eachObject

Executes the block with each pathname.



103
104
105
# File 'lib/lbt/utils.rb', line 103

def each
  @files.each {|f| yield(f)}
end

#to_aObject

Return an array of pathnames which are relative from the top directory. The returned pathnames are the copies. So, even if a user changes them, the original pathnames in the PCS instance are not changed.



99
100
101
# File 'lib/lbt/utils.rb', line 99

def to_a
  @files.map {|s| s.dup}
end