Class: Bunch::DirectoryNode

Inherits:
AbstractNode show all
Defined in:
lib/bunch/directory_node.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractNode

#options

Instance Method Summary collapse

Constructor Details

#initialize(fn) ⇒ DirectoryNode

Returns a new instance of DirectoryNode.



5
6
7
# File 'lib/bunch/directory_node.rb', line 5

def initialize(fn)
  @root = Pathname.new(fn)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#childrenObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bunch/directory_node.rb', line 13

def children
  @children ||= begin
    children = filenames.map do |filename|
      Bunch.tree_for(filename, @options)
    end

    ordering_file = @root.join('_.yml')

    if File.exist?(ordering_file)
      ordering = YAML.load_file(ordering_file)
      ordered, unordered = children.partition { |c| ordering.include?(c.name) }

      (ordering - ordered.map(&:name)).each do |f|
        $stderr.puts "WARNING: directory #{ full_name } has no object #{ f }"
      end

      ordered.sort_by { |c| ordering.index(c.name) } + unordered.sort_by(&:name)
    else
      children.sort_by(&:name)
    end
  end
end

#contentObject



49
50
51
# File 'lib/bunch/directory_node.rb', line 49

def content
  @content ||= children.map(&:content).join
end

#filenamesObject



9
10
11
# File 'lib/bunch/directory_node.rb', line 9

def filenames
  Dir[@root.join("*")].select { |f| f !~ /_\.yml$/ }
end

#full_nameObject



57
58
59
# File 'lib/bunch/directory_node.rb', line 57

def full_name
  @root
end

#inspectObject



78
79
80
# File 'lib/bunch/directory_node.rb', line 78

def inspect
  "#<DirectoryNode @root=#{@root.inspect} @children=#{children.inspect}>"
end

#nameObject



53
54
55
# File 'lib/bunch/directory_node.rb', line 53

def name
  File.basename(@root)
end

#target_extensionObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bunch/directory_node.rb', line 36

def target_extension
  @target_extension ||= begin
    exts = children.map(&:target_extension).compact.uniq
    if exts.count == 1
      exts.first
    elsif exts.count > 1
      raise "Directory #{name} contains non-homogeneous nodes: #{exts.inspect}"
    else
      nil
    end
  end
end

#write_to_dir(dir) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bunch/directory_node.rb', line 66

def write_to_dir(dir)
  super

  if @options[:recurse]
    directory_name = File.join(dir, name)
    FileUtils.mkdir(directory_name)
    children.each do |child|
      child.write_to_dir(directory_name)
    end
  end
end

#write_to_file(dir) ⇒ Object



61
62
63
64
# File 'lib/bunch/directory_node.rb', line 61

def write_to_file(dir)
  return if filenames.count == 0
  super
end