Class: Moka::OrderGivers::GroupOrderGiver
- Includes:
- SiteTree, Thor::Actions
- Defined in:
- lib/commands/order_groups.rb
Constant Summary
Constants included from SiteTree
Class Method Summary collapse
Instance Method Summary collapse
- #ask_for_new_order ⇒ Object
- #dump_manifest ⇒ Object
- #load_manifest ⇒ Object
- #notify_new_order ⇒ Object
- #provide_help_if_needed ⇒ Object
- #reorder ⇒ Object
Class Method Details
.counter_to_letters(c) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/commands/order_groups.rb', line 90 def self.counter_to_letters(c) letters = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z) if c < letters.size return letters[c] else return GroupOrderGiver.counter_to_letters(c/letters.size - 1) + letters[c%letters.size] end end |
.letters_to_counter(l) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/commands/order_groups.rb', line 99 def self.letters_to_counter(l) letters = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z) l.gsub(/[^a-z]/, "") if l.size == 0 return -1 end unless letters.index(l).nil? return letters.index(l) else return letters.index(l[-1]) + letters.size*(GroupOrderGiver.letters_to_counter(l[0..-2])) end end |
Instance Method Details
#ask_for_new_order ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/commands/order_groups.rb', line 41 def ask_for_new_order counter = 0 say "" say "The current group order is the following:\n" @site.groups.each do |group| say "#{GroupOrderGiver.counter_to_letters(counter)} - #{group.name}" counter += 1 end say "" @input = ask "Specify a new ordering as a string of space-separated indices, or press enter to do nothing (example: to switch the second and the third items insert 'a c b'):\n" end |
#dump_manifest ⇒ Object
71 72 73 74 75 76 |
# File 'lib/commands/order_groups.rb', line 71 def dump_manifest f = File.open( File.('manifest.yml', MOKA_ROOT), 'w' ) do |out| YAML.dump( @manifest, out ) end say_status "update", "manifest.yml", :green end |
#load_manifest ⇒ Object
36 37 38 39 |
# File 'lib/commands/order_groups.rb', line 36 def load_manifest @manifest = YAML.load_file(File.("manifest.yml", MOKA_ROOT)) @site = SiteNode.new("site", @manifest["site"]) end |
#notify_new_order ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/commands/order_groups.rb', line 78 def notify_new_order say "" say "The new group order is the following:\n" counter = 0 @site = SiteNode.new("", @manifest["site"]) @site.groups.each do |group| say "#{GroupOrderGiver.counter_to_letters(counter)} - #{group.name}" counter += 1 end say "" end |
#provide_help_if_needed ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/commands/order_groups.rb', line 19 def provide_help_if_needed if [:help] say <<-EOT Usage: moka order_groups Examples: moka order_groups launch a CLI utility to display and edit the order groups EOT exit end end |
#reorder ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/commands/order_groups.rb', line 53 def reorder input_array = @input.gsub(/[^a-z\s]/, "").split(/\s+/).map{|i| GroupOrderGiver.letters_to_counter(i)}.uniq.delete_if{|i| i < 0 or i > @site.groups.size - 1} if input_array.size <= 0 exit end counter = 0 counter_two = 0 @site.groups.each do |group| unless input_array.index(counter).nil? @manifest["site"][group.name]["order"] = input_array.index(counter) + 1 else @manifest["site"][group.name]["order"] = input_array.size + counter_two + 1 counter_two += 1 end counter += 1 end end |