Class: Tagmv::Tree

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries = []) ⇒ Tree

Returns a new instance of Tree.



6
7
8
# File 'lib/tagmv/tree.rb', line 6

def initialize(entries = [])
  @entries = entries
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



5
6
7
# File 'lib/tagmv/tree.rb', line 5

def entries
  @entries
end

Class Method Details

.false_tag_regexObject



23
24
25
26
27
# File 'lib/tagmv/tree.rb', line 23

def self.false_tag_regex
  # detect when there's a false tag i.e. tag2. in path/to/tag1./not_a_tag/tag2./
  /\/.+-\/[^-]+\/.+-/
  #/\/(.*[^-]\/|[^\/]{0,1}?-)/
end

.file_in_tag_dirObject



37
38
39
# File 'lib/tagmv/tree.rb', line 37

def self.file_in_tag_dir
  /.+(\-\/[^\/]+)$/
end

.path_has_file_regexObject



33
34
35
# File 'lib/tagmv/tree.rb', line 33

def self.path_has_file_regex
  /#{tags_in_path_regex}.*[^\-]$/
end

.scan_tree_entriesObject



46
47
48
49
50
51
52
53
54
# File 'lib/tagmv/tree.rb', line 46

def self.scan_tree_entries
  files = Find.find(Filesystem.root).select {|x| x =~ path_has_file_regex }.select {|x| x =~ file_in_tag_dir }
  tree = Tree.new
  files.each do |file|
    next if file =~ false_tag_regex  # break when /dev./oh/blah./foo
    tree.entries << Entry.new(files: [file], tags: tags(file))
  end
  tree
end

.scan_tree_hashObject

only gets existing tree, doesn’t build an accurate tree based on all the tag counts etc.. Find.find(‘.’).select {|x| x =~ /([^/]+/)*([^/]+/)*./.*/} “dev“dev.”=>{“book“dev.”=>{“book.”=>{“javascript“dev.”=>{“book.”=>{“javascript.”=>{“Secrets_of_the_Javascript_Ninja“dev.”=>{“book.”=>{“javascript.”=>{“Secrets_of_the_Javascript_Ninja.pdf”=>{}, “ruby.”=>“rails_antipatterns“rails_antipatterns.pdf”=>{}}, “ruby.”=>“oh”=>{, “tagmv”=>{}}}}



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tagmv/tree.rb', line 59

def self.scan_tree_hash
  Dir.chdir(Filesystem.root)
  
  # reject /dev-/-, /dev-/j-/ and /dev-/notag/tag-
  paths = Dir["**/*"].delete_if {|x| /\/(.*[^-]\/|[^\/]{0,1}?-)/ =~ x}
  paths.inject({}) do |hash,path|
    tree = hash
    path.split("/").each do |n|
      tree[n] ||= {}
      tree = tree[n]
      break if n[-1] != "-"
    end
    hash
  end
end

.tags(file) ⇒ Object

Raises:

  • (StandardError)


41
42
43
44
# File 'lib/tagmv/tree.rb', line 41

def self.tags(file)
  raise StandardError.new('Invalid file path given') unless file[/^#{Filesystem.root}/]
  file[Filesystem.root.length..-1].scan(tags_in_path_regex).reject {|x| x =~ /\//}
end

.tags_in_path_regexObject



29
30
31
# File 'lib/tagmv/tree.rb', line 29

def self.tags_in_path_regex
  /[^(\.|\-)\/]\K.+?(?=\-\/)/
end

Instance Method Details

#tag_countsObject



14
15
16
# File 'lib/tagmv/tree.rb', line 14

def tag_counts
  entries.map {|x| x.tags }.flatten.each_with_object(Hash.new(0)) { |word,counts| counts[word] += 1 }
end

#tag_orderObject



18
19
20
# File 'lib/tagmv/tree.rb', line 18

def tag_order
  tag_counts.to_a.sort_by{|x| [-x.last, x.first]}.map {|x| x.first }
end

#with(opts) ⇒ Object



10
11
12
# File 'lib/tagmv/tree.rb', line 10

def with(opts)
  entries << Entry.new(opts)
end