Class: Tagmv::Tree
- Inherits:
-
Object
- Object
- Tagmv::Tree
- Defined in:
- lib/tagmv/tree.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
Class Method Summary collapse
- .false_tag_regex ⇒ Object
- .file_in_tag_dir ⇒ Object
- .path_has_file_regex ⇒ Object
- .scan_tree_entries ⇒ Object
-
.scan_tree_hash ⇒ Object
only gets existing tree, doesn’t build an accurate tree based on all the tag counts etc..
- .tags(file) ⇒ Object
- .tags_in_path_regex ⇒ Object
Instance Method Summary collapse
-
#initialize(entries = []) ⇒ Tree
constructor
A new instance of Tree.
- #tag_counts ⇒ Object
- #tag_order ⇒ Object
- #with(opts) ⇒ Object
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
#entries ⇒ Object
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_regex ⇒ Object
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_dir ⇒ Object
37 38 39 |
# File 'lib/tagmv/tree.rb', line 37 def self.file_in_tag_dir /.+(\-\/[^\/]+)$/ end |
.path_has_file_regex ⇒ Object
33 34 35 |
# File 'lib/tagmv/tree.rb', line 33 def self.path_has_file_regex /#{}.*[^\-]$/ end |
.scan_tree_entries ⇒ Object
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: (file)) end tree end |
.scan_tree_hash ⇒ Object
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
41 42 43 44 |
# File 'lib/tagmv/tree.rb', line 41 def self.(file) raise StandardError.new('Invalid file path given') unless file[/^#{Filesystem.root}/] file[Filesystem.root.length..-1].scan().reject {|x| x =~ /\//} end |
.tags_in_path_regex ⇒ Object
29 30 31 |
# File 'lib/tagmv/tree.rb', line 29 def self. /[^(\.|\-)\/]\K.+?(?=\-\/)/ end |
Instance Method Details
#tag_counts ⇒ Object
14 15 16 |
# File 'lib/tagmv/tree.rb', line 14 def tag_counts entries.map {|x| x. }.flatten.each_with_object(Hash.new(0)) { |word,counts| counts[word] += 1 } end |
#tag_order ⇒ Object
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 |